简体   繁体   English

如何在log4j2中使用Spring更改生产中的控制台日志记录?

[英]How to change console logging in production with Spring in log4j2?

How can I disable console logging completely in production? 如何在生产中完全禁用控制台日志记录? (eg using spring.profiles.active=production ? (例如使用spring.profiles.active=production

During dev I always want to see the full console logging. 在开发期间,我总是希望看到完整的控制台日志记录。 But in production I'd like to prevent console out. 但在制作中我想阻止控制台出局。 But how? 但是怎么样?

log4j2-spring.xml: log4j2-spring.xml:

<configuration>
   <Appenders>
        <Console name="CONSOLE" target="SYSTEM_OUT">
            <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY" />
        </Console>

        <RollingRandomAccessFile name="XML"  .../>
    </Appenders>

    <Loggers>
       <Root level="all">
            <AppenderRef ref="CONSOLE" />
            <AppenderRef ref="XML" />
       </Root>
    </Loggers>
</configuration>

Sidenote: the INFO statements should still be logged to XML file. 旁注:INFO语句仍应记录到XML文件中。 They should just not be printed to console additionally. 它们不应该另外打印到控制台。

It's possible using vm args as follows: 使用vm args可能如下:

<Configuration>
    <Properties>
       <property name="console.log">INFO</property> <!-- default value if not set -->
    </Properties>

        <Console name="CONSOLE" target="SYSTEM_OUT">
            <ThresholdFilter level="${sys:console.log}" onMatch="ACCEPT" onMismatch="DENY" />
        </Console>

The logger will use INFO level by default. 记录器默认使用INFO级别。 And when providing eg -Dconsole.log=ERROR on launch, it will only log errors to console. 在启动时提供例如-Dconsole.log=ERROR时,它只会将错误记录到控制台。

Would be even nicer if log4j2 could read just a property defined in application-production.properties , but for not the approach works. 如果log4j2只能读取application-production.properties定义的属性,那就更好了,但是这种方法不行。

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

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