日志记录:在Spring Boot中使用log4j2.properties文件进行Log4j2实现

时间:2019-04-10 05:56:21

标签: java spring-boot log4j2

如果我指定,则记录工作正常 'logging.config = src / main / resources / log4j2.properties' 在我的application.properties文件中。

在Spring Boot自动检测到log4j2.properties并且不需要在application.properties文件中指定'logging.config = src / main / resources / log4j2.properties'之外,是否还有其他解决方法?

Spring Boot自动检测类路径中的log4j2.xml和log4j2.json文件,但就我而言,不是log4j2.properties文件

我的pom.xml:

<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-log4j2</artifactId>
</dependency>

log4j2.properties:

name=PropertiesConfig
appenders = console, file

appender.console.type = Console
appender.console.name = ConsoleAppender
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{MM:dd HH:mm:ss.SSS} [%t] 
                                  [%level] [%logger{36}] - %msg%n

appender.file.type = File
appender.file.name = FileAppender
appender.file.fileName=/home/ubuntu/application.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern= %d{MM:dd HH:mm:ss.SSS} [%t] [%level] 
                              [%logger{36}] - %msg%n

loggers=file
logger.file.name=com.project
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = FileAppender

rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = ConsoleAppender

注意:我正在使用的Spring Boot版本是2.1.3RELEASE

参考:https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html

2 个答案:

答案 0 :(得分:1)

我不知道Spring Boot会修改Log4j 2用来定位配置文件的逻辑。实际上,我一直在研究使用log4j2.yml的Spring Boot服务。我用log4j2.properties替换了它,并且效果很好。 Log4j的常规发现过程在类路径上找到它。

令我惊讶的是,将logging.config=src/main/resources/log4j2.properties指定为Spring Boot jar为您工作通常不会在其中包含“ src”目录。

答案 1 :(得分:0)

也可以使用这种方式:

java  -Dlog4j.configurationFile=log4j2.xml -jar xxxx-app.jar

参考: https://logging.apache.org/log4j/2.x/manual/configuration.html