简体   繁体   English

Spring日志和用户定义的日志分离

[英]Separation of Spring logs and User - defined logs

I am building a Spring boot project with Tomcat server where in Spring automatically takes care of all the logging mechanism, all the logs are present in "catalina.out" file. 我正在使用Tomcat服务器构建一个Spring引导项目,其中Spring自动处理所有日志记录机制,所有日志都存在于“ catalina.out”文件中。

Now that, I have a requirement where I need to log only specific information to a separate log file. 现在,我有一个要求,我只需要将特定信息记录到单独的日志文件中。

I have done the below configuration, but now all the logs including the main logs are appending in the same file. 我已经完成了以下配置,但是现在包括主日志在内的所有日志都附加在同一文件中。

I need only specific logs into a separate log file. 我只需要将特定日志记录到一个单独的日志文件中。

Can anyone suggest me a solution for this? 有人可以建议我解决这个问题吗?

My pom.xml looks like this. 我的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>

My log4j.properties look like this. 我的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

The java file looks like this. Java文件如下所示。

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

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

According to FAQ Logging , the CATALINA_BASE/logs/catalina.out contains the output of System.out and System.err . 根据FAQ日志记录CATALINA_BASE/logs/catalina.out包含System.outSystem.err的输出。

Therefore, you need to redirect the output of Spring to System.out . 因此,您需要将Spring的输出重定向到System.out That can be done using org.apache.log4j.ConsoleAppender . 可以使用org.apache.log4j.ConsoleAppender完成。

Using this appender, your configuration file may be similar: 使用此附加程序,您的配置文件可能类似于:

# 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

If you do not want that your application also log to stdout 1 , use: 如果您不希望您的应用程序也登录到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

See more in Short introduction to log4j on Apache log4j 1.2 webpage. 请参阅Apache log4j 1.2网页上的log4j 简介中的更多内容。


Notes 笔记

  1. Log4j Tutorial: Additivity – what and why? Log4j教程:可加性–什么以及为什么?

You should create multiple loggers that point to different files (now you have just one, that why all ends in one file). 您应该创建指向不同文件的多个记录器(现在只有一个,这就是为什么所有记录器都以一个文件结尾)。 Then you can distinguish the loggers with some prefix to the package. 然后,您可以使用一些软件包前缀来区分记录器。 Something like 就像是

log4j.logger.file1.com.spring.something=.../define the logger
log4j.logger.file2.com.spring.somethingelse=.../second logger

and then when obtaining the logger use something like 然后在获取记录器时使用类似

org.apache.log4j.Logger.getLogger("file1" + MyClass.class.getName());

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

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