简体   繁体   English

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

[英]Logging : Log4j2 Implementation using log4j2.properties file in Spring Boot

Logging is working fine if i specify 'logging.config = src/main/resources/log4j2.properties' in my application.properties file.如果我在 application.properties 文件中指定“logging.config = src/main/resources/log4j2.properties”,则日志记录工作正常。

Is there any other work around where spring boot automatically detects log4j2.properties and doesnot require to specify 'logging.config = src/main/resources/log4j2.properties' inside application.properties file..?在 spring boot 自动检测 log4j2.properties 并且不需要在 application.properties 文件中指定 'logging.config = src/main/resources/log4j2.properties' 的地方还有其他工作吗..?

Spring Boot automatically detects log4j2.xml, log4j2.json files in classpath, but not in case log4j2.properties file, in my case Spring Boot 自动检测类路径中的 log4j2.xml、log4j2.json 文件,但在我的例子中不检测 log4j2.properties 文件

my pom.xml:我的 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: 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

Note: Spring boot version i am using is 2.1.3.RELEASE注意:我使用的 Spring Boot 版本是 2.1.3.RELEASE

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

I am not aware that Spring Boot modifies the logic Log4j 2 uses to locate configuration files.我不知道 Spring Boot 修改了 Log4j 2 用于定位配置文件的逻辑。 In fact, I have been working on a Spring Boot service that was using a log4j2.yml.事实上,我一直在研究使用 log4j2.yml 的 Spring Boot 服务。 I replaced that with a log4j2.properties and it worked fine.我用 log4j2.properties 替换了它,它工作正常。 Log4j's normal discovery process finds it on the class path. Log4j 的正常发现过程在类路径上找到它。

I am actually surprised that specifying logging.config=src/main/resources/log4j2.properties worked for you as a Spring Boot jar wouldn't normally have the "src" directory in it.我真的很惊讶指定logging.config=src/main/resources/log4j2.properties对你有用,因为 Spring Boot jar 通常不会在其中包含“src”目录。

also can use this way:也可以使用这种方式:

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

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

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

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