【问题标题】:Save custom level logs into file in Spring Boot在 Spring Boot 中将自定义级别日志保存到文件中
【发布时间】:2022-01-13 16:33:02
【问题描述】:

我想将成功登录的信息保存到日志文件中,我想创建一个新级别的日志记录并仅将具有该特定级别的日志保存到日志文件中。 这是我尝试过的方法,但我无法正常工作:

log4j2-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                    pattern="%style{%d{ISO8601}}{white} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
        </Console>

        <RollingFile name="RollingFile"
                     fileName="./logs/historicLog.log"
                     filePattern="./logs/$${date:yyyy-MM}/spring-boot-logger-log4j2-%d{-dd-MMMM-yyyy}-%i.log.gz">
            <PatternLayout>
                <pattern>%d %p %C{1.} [%t] %m%n</pattern>
            </PatternLayout>
            <Policies>
                
                <OnStartupTriggeringPolicy />
                <SizeBasedTriggeringPolicy
                        size="10 MB" />
                <TimeBasedTriggeringPolicy />
            </Policies>
        </RollingFile>
    </Appenders>


    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console" />
            <AppenderRef ref="RollingFile" />
        </Root>

        <Logger name="login" level="info">
            <AppenderRef ref="RollingFile" />
        </Logger>
    </Loggers>

</Configuration>

build.gradle 依赖项:

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-config-server:3.0.5'
    implementation 'org.springframework.boot:spring-boot-starter-web:2.6.1'
    implementation 'org.springframework.boot:spring-boot-starter-log4j2'
    implementation 'org.springframework.boot:spring-boot-starter-actuator:2.6.1'
    testImplementation 'org.springframework.boot:spring-boot-starter-test:2.6.1'
}

configurations.implementation {
    exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}

AuthenticationService.java

private static final org.apache.logging.log4j.Logger logger = LogManager.getLogger(AuthenticationServiceApplication.class);
....
logger.log(Level.forName("login", 850), user.getUsername() + " logged in as admin");

提前谢谢你。

【问题讨论】:

    标签: java spring-boot logging log4j log4j2


    【解决方案1】:

    您的根记录器仍设置在INFO,而您的com.authenticationservice 记录器位于TRACE,两者的级别ID 均低于850,因此不会写入任何更高级别的日志语句。

    但是,我建议不要使用自定义日志级别,而是使用特定的记录器名称,然后配置该名称。例如。像这样:

    在您的 AuthenticationService.java 中:

    private static final org.apache.logging.log4j.Logger logger = LogManager.getLogger("login");
    

    在您的 log4j2-spring.xml 中:

    <Logger name="login" level="info"> // Note, that you should have the same level configured than what you use in the code to log the messages
      <AppenderRef ref="RollingFile" />
    </Logger>
    

    这会将所有与登录相关的日志写入在RollingFile 配置的日志文件中。使用该解决方案,您比使用自定义级别灵活得多,因为您可以将其重定向到单独的文件,将语句与其他记录器等结合起来。

    【讨论】:

    • 感谢您的回答,我已使用更新的 .xml 代码编辑了帖子,您能否检查一下我是否正确完成了?同样在 .java 文件中 logger.log(Level.forName("login", 850), user.getUsername() + " logged in as admin");还对吗?
    • 你能回复@dunni吗?
    猜你喜欢
    • 2020-02-25
    • 2020-08-02
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 2012-08-18
    • 2020-03-30
    • 2015-03-11
    • 1970-01-01
    相关资源
    最近更新 更多