gpt4 book ai didi

java - Spring日志和用户自定义日志分离

转载 作者:行者123 更新时间:2023-12-01 12:15:28 26 4
gpt4 key购买 nike

我正在使用 Tomcat 服务器构建一个 Spring boot 项目,其中 Spring 自动处理所有日志机制,所有日志都存在于“catalina.out”文件中。

现在,我有一个要求,我需要仅将特定信息记录到单独的日志文件中。

我已经完成了以下配置,但现在包括主日志在内的所有日志都附加在同一个文件中。

我只需要将特定日志放入单独的日志文件中。

有人可以建议我解决这个问题吗?

我的 pom.xml 看起来像这样。

 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>

我的 log4j.properties 看起来像这样。

# Root logger option
log4j.rootLogger=INFO,file

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender

#Redirect to Tomcat logs folder
#log4j.appender.file.File=${catalina.home}/logs/logging.log

log4j.appender.file.File=/my_log.log
log4j.appender.file.Append=true
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

java 文件如下所示。

static org.apache.log4j.Logger log4jLogger = org.apache.log4j.Logger.getLogger(MyClass.class.getName());

log4jLogger.info("My specific logs here");

最佳答案

根据FAQ LoggingCATALINA_BASE/logs/catalina.out 包含 System.outSystem.err 的输出。

因此,需要重定向Spring的输出到System.out。这可以使用 org.apache.log4j.ConsoleAppender 来完成.

使用此附加程序,您的配置文件可能类似:

# Root logger option
log4j.rootLogger=info, stdout

# Direct log messages to the console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/my_log.log
log4j.appender.file.Append=true
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Output to the file only from certain packages (e.g. your application)
log4j.logger.com.foo=trace, file
log4j.logger.org.company=trace, file

如果您不希望应用程序也登录到 stdout 1,请使用:

# Output to the file only from certain packages (e.g. your application)
log4j.logger.com.foo=trace, file
log4j.additivity.com.foo=false
log4j.logger.org.company=trace, file
log4j.additivity.org.company=false

查看更多Short introduction to log4jApache log4j 1.2网页。

<小时/>

注释

  1. Log4j Tutorial: Additivity – what and why?

关于java - Spring日志和用户自定义日志分离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27041564/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com