Java 使用 Log4j2 记录 Spring

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16341020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:24:11  来源:igfitidea点击:

Logging Spring using Log4j2

javaspringlogginglog4jlog4j2

提问by hveiga

I'm trying to use Log4j2to print the spring logs into a file and console. I guess it is a problem in my Log4j2 configuration. I have not been able to get it working. I have this configuration in my log4j2.xml file:

我正在尝试使用Log4j2将 spring 日志打印到文件和控制台中。我想这是我的 Log4j2 配置中的问题。我一直无法让它工作。我的 log4j2.xml 文件中有这个配置:

<?xml version="1.0" encoding="UTF-8"?>
<configuration name="defaultConfiguration" status="warn" strict="true" monitorInterval="5">
    <properties>
        <property name="patternlayout">%d{ISO8601} [%t] %-5level %logger{36} - %msg%n%throwable{full}</property>
        <property name="filename">${env:MY_ROOT}/logs/mylog.log</property>
        <property name="filenamePattern">${env:MY_ROOT}/logs/mylog-%d{yyyy-dd-MM}-%i.log.gz</property>
    </properties>
    <appenders>
        <appender name="Console" type="Console" target="SYSTEM_OUT">
            <layout type="PatternLayout" pattern="${patternlayout}" />
        </appender>
        <appender name="File" type="RollingFile" fileName="${filename}" filePattern="${filenamePattern}" bufferedIO="true" immediateFlush="true"
        append="true">
            <layout type="PatternLayout" pattern="${patternlayout}" />
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="50 MB" />
            </Policies>
            <DefaultRolloverStrategy max="30" />
        </appender>
        <appender name="AsynchFile" type="asynch" blocking="true" bufferSize="128">
            <appender-ref ref="File" />
        </appender>
    </appenders>
    <loggers>
        <root level="info">
            <appender-ref ref="Console" />
            <appender-ref ref="AsynchFile" />
        </root>
        <logger name="org.springframework.beans">
            <appender-ref ref="Console" />
            <appender-ref ref="AsynchFile" />
        </logger>
    </loggers>
</configuration>

These are the dependencies that I have in my pom file: (probably some of them are not required)

这些是我的 pom 文件中的依赖项:(可能其中一些不是必需的)

<dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-api</artifactId>
 <version>1.6.6</version>
</dependency>

<dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-log4j12</artifactId>
 <version>1.6.6</version>
</dependency>

<dependency>
 <groupId>org.apache.logging.log4j</groupId>
 <artifactId>log4j-api</artifactId>
 <version>2.0-beta5</version>
</dependency>

<dependency>
 <groupId>org.apache.logging.log4j</groupId>
 <artifactId>log4j-core</artifactId>
 <version>2.0-beta5</version>
</dependency>
<dependency>
 <groupId>org.apache.logging.log4j</groupId>
 <artifactId>log4j-1.2-api</artifactId>
 <version>2.0-beta5</version>
</dependency>

<dependency>
 <groupId>com.lmax</groupId>
 <artifactId>disruptor</artifactId>
 <version>3.0.0.beta3</version>
</dependency>

I'm not doing anything related to spring in my Java code. I'm using the Main class from Apache Camel which reads my spring configuration and loads the beans.

我没有在我的 Java 代码中做任何与 spring 相关的事情。我正在使用 Apache Camel 的 Main 类,它读取我的 spring 配置并加载 bean。

What am I doing wrong? Thanks!

我究竟做错了什么?谢谢!

Edit: I am not getting spring logs in any output (console or file). However, I am able to get the logs I create in my java code. I hope this clarification will help.

编辑:我没有在任何输出(控制台或文件)中获取 spring 日志。但是,我能够获取在我的 Java 代码中创建的日志。我希望这个澄清会有所帮助。

采纳答案by Remko Popma

Looking at the dependencies in your pom, you have this one: slf4j-log4j12. This causes log statements against the slf4j api to be routed to the Log4j-1.2 implementation. You probably want these to be routed to the Log4j-2.0 implementation. Can you replace slf4j-log4j12 with log4j-slf4j-impl and try again?

查看你的 pom 中的依赖项,你有这个:slf4j-log4j12。这会导致针对 slf4j api 的日志语句被路由到 Log4j-1.2 实现。您可能希望将这些路由到 Log4j-2.0 实现。您可以用 log4j-slf4j-impl 替换 slf4j-log4j12 并重试吗?

回答by Kouichi

This issue caused by spring use common-logging 1.X ,so if you want to have this logging routed to Log4j 2,you need add dependencies in your pom.xml

这个问题是spring使用common-logging 1.X导致的,所以如果你想把这个日志路由到Log4j 2,你需要在你的pom.xml中添加依赖

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-jcl</artifactId>
    <version>2.1</version>
 </dependency>

do not remove your common-logging 1.X dependencies

不要删除您的 common-logging 1.X 依赖项