简体   繁体   English

如何通过spring boot将日志保存在所有记录器的数据库中

[英]How to save logs in DB on all loggers via spring boot

Hi I am trying to save the logs of my app in DB via AOP.嗨,我正在尝试通过 AOP 将我的应用程序的日志保存在数据库中。 any suggestions.有什么建议。

Ex:前任:

 logger.info("In Business - {}", value);

I want this log in business to save in database as well after printing here.我希望此登录业务在此处打印后也保存在数据库中。

Looking for some AOP which I may apply on Logger.info寻找一些我可以在 Logger.info 上应用的 AOP

https://www.springboottutorial.com/spring-boot-and-aop-with-spring-boot-starter-aop https://www.springboottutorial.com/spring-boot-and-aop-with-spring-boot-starter-aop

according to my understanding, people usually don't do that because of DB overhead.根据我的理解,由于数据库开销,人们通常不会这样做。

What I did previously is to create an entity directly and call that service to save whichever you wanted to log.我之前所做的是直接创建一个实体并调用该服务以保存您想要记录的任何内容。 You can find the following for your reference.您可以找到以下内容供您参考。

public class MyLogger{
    private String message;

    // Getter
    // Setter
}

public class MyLoggerService(){
    @Autowired
    private MyLoggerRepo repo;

    public void save(String message){
        MyLogger log = new MyLogger();
        log.setMessage(message);
        repo.save(log);
    }
}

The log4j API provides the org.apache.log4j.jdbc.JDBCAppender object, which can put logging information in a specified database. log4j API 提供了 org.apache.log4j.jdbc.JDBCAppender 对象,可以将日志信息放到指定的数据库中。 You may have a try of this, no need to code by yourself.你可以试试这个,不需要自己编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM