简体   繁体   English

Log4j2 不会在 Spring 引导中登录文件

[英]Log4j2 won't log into file in Spring boot

I'm using Spring boot and Gradle build tools.我正在使用 Spring 引导和 Gradle 构建工具。 I want to add a logger which logging my events.我想添加一个记录我的事件的记录器。 I chose Log4j2.我选择了 Log4j2。 first of all, I have added these dependencies:首先,我添加了这些依赖项:

implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.14.1'

implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.14.1'

then added this configuration file, to my resources folder然后将此配置文件添加到我的资源文件夹

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
    <Properties>
        <Property name="LOG_PATTERN">
            %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} : %m%n%ex
        </Property>
    </Properties>
    <Appenders>
        <Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
            <PatternLayout pattern="${LOG_PATTERN}"/>
        </Console>
    </Appenders>

    <RollingFile name="FileAppender" fileName="logs/log4j2-demo.log"
                 filePattern="log4j2-demo-%d{yyyy-MM-dd}-%i.log">
        <PatternLayout>
            <Pattern>${LOG_PATTERN}</Pattern>
        </PatternLayout>
        <Policies>
            <SizeBasedTriggeringPolicy size="10MB" />
        </Policies>
        <DefaultRolloverStrategy max="10"/>
    </RollingFile>

    <Loggers>
        <Root level="info">
            <AppenderRef ref="ConsoleAppender" />
            <AppenderRef ref="FileAppender" />

        </Root>
    </Loggers>
</Configuration>

then I use it via this command:然后我通过这个命令使用它:

private static final Logger logger = LogManager.getLogger(Application.class);
logger.error("Hello from Log4j 2");

I'm in doubt that Spring uses the configuration file at all.我怀疑 Spring 是否使用了配置文件。 Because it won't create the log folder and log4j2-demo.log file.因为它不会创建日志文件夹和log4j2-demo.log文件。 What is the problem?问题是什么?

Use LoggerFactory使用LoggerFactory

 private static final Logger LGR = LoggerFactory.getLogger(ObjectsController.class);

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

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