Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to save logback logs to mongoDB in SpringBoot

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to save logback logs to mongoDB in SpringBoot". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Customizing Appender is very simple, just inherit the AppenderBase class.

You can see that there is an AppenderBase, a UnsynchronizedAppenderBase, and an AsyncAppenderBase that inherits UnsynchronizedAppenderBase. You can tell the difference by the name, asynchronous, normal, unlocked.

We define a MongoDBAppender inheritance UnsynchronizedAppenderBase

Public class MongoDBAppender extends UnsynchronizedAppenderBase {@ 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 have to implement an append method, which is where the logback outputs the log, which is stored in the eventObject object. We just need to get the value in the object and do our own processing.

We can imagine that the ConsoleAppender of the system is a non-stop System.out.print (eventObject.getXXX), and FileAppender is output to a file using OutpuptStream.

All we need to do is save the log to mongo. Springboot has provided the MongoTemplate template. We need to note that the log output starts with the startup of the system. At the beginning, the MongoTemplate has not been initialized, so we need to wait for Spring to assign a value to MongoTemplate. So it starts with null, and you need to wait until spring is initialized before MongoTemplate has a value.

Since this Appender is not managed by spring, I use a separate way to get the bean. 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 getBean (Class tClass) {return context.getBean (tClass);} public static T getBean (String name, Class tClass) {return context.getBean (name, tClass);}}

The storage of the above mongo operation log is relatively simple, and the fields have not been used up. You can design the structure of mongo according to the properties you want to save, and then enter the library.

Pom.xml

Org.mongodb mongo-java-driver 3.4.2 ch.qos.logback logback-core 1.1.11 ch.qos.logback logback-classic 1.1.11

It is also easy to use in Spring-logback.xml.

You only need to specify the class of appender.

This is the end of the content of "how to save logback logs to mongoDB in SpringBoot". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report