Spring Boot中的记录器问题

时间:2018-08-06 06:28:56

标签: java spring-boot log4j2

我已经使用log4j2.properties文件实现了“在Spring Boot应用程序中登录”。

请在下面的参考网址中找到代码:

Spring Boot - log4j2.properties creating log files but not writing the logs in file

控制台日志记录正在工作,并且还在创建文件,其中一个日志正在写入文件,而其他日志未在写入文件。

请在控制台中找到记录器的区别,如下所示:

[INFO ] 2018-08-06 11:48:05.609 [restartedMain] DemoApplication                  - Logger enabled: Entering main //this log is writing into file.

2018-08-06 11:48:14.211  INFO 10788 --- [  restartedMain] c.j.c.DemoApplication                  : **** Demo Application Started *****// this is not writing into file.

但是对于两个都使用log.info来打印记录器的人。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

您必须仔细检查log4j2.properties文件。我已经对您的示例进行了测试,以下配置可以正常工作:

name=PropertiesConfig
appenders = console, file
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=demo.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
loggers=file
logger.file.name=com.testsexample.test5
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE
rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT   

主类如下(程序包名称应与log4j2.properties中的logger.file.name属性相对应):

package com.testsexample.test5;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Test5Application {

    private final static Logger LOG = LogManager.getLogger(Test5Application.class);

    public static void main(String[] args) {

        LOG.info("Logger enabled: Entering main \n\n");
        SpringApplication.run(Test5Application.class, args);
        LOG.info("**** Demo Application Started *****");
    }
}

项目结构为:

enter image description here

很奇怪,但是经过多次重新发布,我已经转载了您的问题。请尝试用以下log4j2.xml替换log4j2.properties-xml配置已解决问题

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Properties>
        <Property name="log-path">logs</Property>
        <Property name="LOG_PATTERN">
            [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
        </Property>
    </Properties>
    <Appenders>
        <Console name="ConsoleAppender" target="SYSTEM_OUT">
            <PatternLayout pattern="${LOG_PATTERN}"/>
        </Console>
        <File name="FileAppender" fileName="demo.log">
            <PatternLayout>
                <pattern>
                    [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
                </pattern>
            </PatternLayout>
        </File>
    </Appenders>
    <Loggers>
        <Root level="INFO">
            <AppenderRef ref="FileAppender"/>
            <AppenderRef ref="ConsoleAppender"/>
        </Root>
    </Loggers>
</Configuration>

对于每日滚动追加器,它将是:

<RollingFile name="FileAppender" fileName="demo.log" filePattern="demo %d{yyyy-MM-dd}.log">
        <PatternLayout>
            <pattern>
                [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
            </pattern>
        </PatternLayout>
        <Policies>
            <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
        </Policies>
    <DefaultRolloverStrategy></DefaultRolloverStrategy>
    </RollingFile>