Customizing Appender is very simple, just inherit the AppenderBase class.
You can see that there is an AppenderBase, an UnsynchronizedAppenderBase, and an AsyncAppenderBase that inherits UnsynchronizedAppenderBase. The difference can be seen from the name, asynchronous, ordinary, and unlocked.
We define a MongoDBAppender to inherit UnsynchronizedAppenderBase
public class MongoDBAppender extends UnsynchronizedAppenderBase<ILoggingEvent> { @Override protected void append(ILoggingEvent eventObject) { MongoTemplate mongoTemplate = ApplicationContextProvider.getBean(MongoTemplate.class); if (mongoTemplate != null) { final BasicDBObject doc = new BasicDBObject(); doc.append("level", eventObject.getLevel().toString()); doc.append("logger", eventObject.getLoggerName()); doc.append("thread", eventObject.getThreadName()); doc.append("message", eventObject.getFormattedMessage()); mongoTemplate.insert(doc, "log"); } } }
We must implement an append method. This method is where logback outputs logs. The logs are stored in the eventObject object. We only need to obtain Just take the value in the object and do your own processing.
We can imagine that the system's ConsoleAppender is constantly System.out.print (eventObject.getXXX), and the FileAppender uses OutpuptStream to output to the file.
What we have to do is to save the log to mongo. Springboot has provided the MongoTemplate template. It should be noted that the log output starts when the system starts, and the MongoTemplate has not been initialized at the beginning. , you need to wait for Spring to assign a value to MongoTemplate. So it is null at the beginning, and you need to wait until spring is initialized before MongoTemplate has a value.
Since this Appender is not managed by spring, I use the method of obtaining the bean separately. The ApplicationContextProvider is as follows:
@Component public class ApplicationContextProvider implements ApplicationContextAware { private static ApplicationContext context; public static ApplicationContext getApplicationContext() { return context; } @Override public void setApplicationContext(ApplicationContext ac) throws BeansException { context = ac; } public static <T> T getBean(Class<T> tClass) { return context.getBean(tClass); } public static <T> T getBean(String name, Class<T> tClass) { return context.getBean(name, tClass); } }
The above mongo operation log storage is relatively simple, and the fields are not used up. You can design the mongo structure according to the attributes you want to save, and then store it in the database.
pom.xml
<dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.4.2</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.1.11</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.11</version> </dependency>
It is also very simple to use in Spring-logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml" /> <appender name="MY_FILE" class="com.example.demo.mongo.MongoDBAppender"> </appender> <!-- 测试环境+开发环境. 多个使用逗号隔开. --> <springProfile name="test,dev"> <logger name="org.springframework.web" level="INFO"> <appender-ref ref="MY_FILE"/> </logger> <logger name="com.example" level="INFO" /> </springProfile> </configuration>
You only need to specify the appender class.
The above is the detailed content of How to save logback logs to mongoDB in SpringBoot. For more information, please follow other related articles on the PHP Chinese website!