简体   繁体   English

log4j2.xml 在运行时没有选择系统属性的变化?

[英]log4j2.xml is not picking the changes in system properties during the runtime?

Creating an spring-boot app with log4j2 logger and server is wildfly10/jboss7.1.使用 log4j2 记录器和服务器创建一个 spring-boot 应用程序是 wildfly10/jboss7.1。 I added "system property": "loglevel" from Configuration: System Properties and able to access this from spring boot application.我从配置:系统属性中添加了“系统属性”:“日志级别”,并且能够从 spring 启动应用程序访问它。 I called this system property in log4j2.xml to set the log-level.我在 log4j2.xml 中调用了这个系统属性来设置日志级别。 log4j2.xml: log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="error" monitorInterval="30">
    <Properties>
        <Property name="basePath">D://propertieslog</Property>
    </Properties>

    <Appenders>

        <RollingFile name="fileLogger" fileName="${basePath}/acweb.log" filePattern="${basePath}/acweb-%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>
        </RollingFile>

        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
        </Console>
        <Jdbc name="databaseAppender" tableName="APPLICATION_LOG">
            <Filters>
                First deny warn, error and fatal messages
                <ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL" />
                <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL" />
                <ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL" />
                <ThresholdFilter level="DEBUG" onMatch="DENY" onMismatch="NEUTRAL" />
                <ThresholdFilter level="INFO" onMatch="DENY" onMismatch="NEUTRAL" />
                Then accept info, warn, error, fatal and deny debug/trace
                <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY" />
            </Filters>
            <ConnectionFactory class="path" method="getDatabaseConnection" />
            <Column name="LOGLEVEL" pattern="%level" />
            <Column name="LOGGER" pattern="%logger" />
            <Column name="MESSAGE" pattern="%message" />
        </Jdbc>
    </Appenders>
    <loggers>
        <Root level="${sys:loglevel:-ERROR}">
            <appender-ref ref="console" />
            <appender-ref ref="fileLogger" />
            <appender-ref ref="databaseAppender" />
        </Root>
    </loggers>

</configuration>

Controller Class:控制器类:

@GetMapping("/dashboard")
    public ModelAndView welcomeMethod(HttpServletRequest request, HttpServletResponse response,
            @ModelAttribute("token") final AccessToken accessToken) {
        if(null==accessToken.getJwtToken()) { 
            logger.trace("A TRACE Message");
            logger.debug("A DEBUG Message");
            logger.info("An INFO Message");
            logger.warn("An WARN Message");
            logger.warn("Printing system property1:"+ System.getProperty("loglevel"));
            logger.error("An ERROR Message");
            logger.error("Printing system property1:"+ System.getProperty("loglevel"));
            return new ModelAndView("redirect:launchApp"); 
        } 

Issue is while changing the system property from wildfly console, it is reflecting in java class logs, but it is not reflecting in log4j2.xml.问题是从 Wildfly 控制台更改系统属性时,它反映在 java 类日志中,但未反映在 log4j2.xml 中。 log level in log4j2.xml remains as the one which was set during application build. log4j2.xml 中的日志级别保持在应用程序构建期间设置的级别。

set the loglevel system property set as "WARN" and build the project and deployed it.将日志级别系统属性设置为“WARN”并构建项目并部署它。 Logs are below:日志如下:

[WARN ] 2018-09-26 15:13:05.787 [default task-7] WelcomeController - An WARN Message
[WARN ] 2018-09-26 15:13:05.792 [default task-7] WelcomeController - Printing system property1:WARN
[ERROR] 2018-09-26 15:13:05.793 [default task-7] WelcomeController - An ERROR Message
[ERROR] 2018-09-26 15:13:05.793 [default task-7] WelcomeController - Printing system property1:WARN

After on same deployment changed the loglevel system property to "INFO", but the logs are not changing to INFO level:在同一部署后,将日志级别系统属性更改为“INFO”,但日志没有更改为 INFO 级别:

[WARN ] 2018-09-26 15:15:34.933 [default task-11] WelcomeController - An WARN Message
[WARN ] 2018-09-26 15:15:34.933 [default task-11] WelcomeController - Printing system property1:INFO
[ERROR] 2018-09-26 15:15:34.935 [default task-11] WelcomeController - An ERROR Message
[ERROR] 2018-09-26 15:15:34.935 [default task-11] WelcomeController - Printing system property1:INFO

I don't think you can re-write the log4j2.xml via the code.我认为您无法通过代码重新编写 log4j2.xml。

In addition to what @JamesR.Perkins said, log4j2's configuration states:除了@JamesR.Perkins 所说的, log4j2 的配置声明:

"Note that unlike Log4j 1.x, the public Log4j 2 API does not expose methods to add, modify or remove appenders and filters or manipulate the configuration in any way." “请注意,与 Log4j 1.x 不同,公共 Log4j 2 API 不公开添加、修改或删除附加程序和过滤器或以任何方式操作配置的方法。”

I believe you would have to re-define the log4j2 configuration.我相信您必须重新定义 log4j2 配置。

Feel free to borrow my example to do so.随意借用我的例子来这样做。 If you have any questions about it, feel free to ask:如果您对此有任何疑问,请随时提问:

Gist link to my example 指向我的示例的要点链接

Log4j2 offers a guide in configuring the logging by Java and it's somewhat helpful... Can be a little confusing. Log4j2 提供了通过 Java 配置日志记录的指南,它有点帮助......可能有点令人困惑。 Here's their guide . 这是他们的指南

If you know how the log4j2.xml is setup, that would help make it a little easier when reading through the guide and using my example.如果您知道 log4j2.xml 是如何设置的,那么在阅读指南和使用我的示例时会更容易一些。


A little side note...一个小小的旁注...

I think you have the <ThresholdFilter> configured improperly.我认为您的<ThresholdFilter>配置不正确。 It is a little bit confusing until you get a hang of it.在您掌握它之前,这有点令人困惑。

First off, figure out exactly at what level you would like the logger to print log entries to the databaseAppender (APPLICATION_LOG).首先,弄清楚您希望记录器在什么级别将日志条目打印到 databaseAppender (APPLICATION_LOG)。

The way I read it, you are denying everything and then accepting trace.我读它的方式,你否认一切,然后接受痕迹。 The simplest way to do that would be:最简单的方法是:

<Jdbc name="databaseAppender" tableName="APPLICATION_LOG">
    <ThresholdFilter level="DEBUG" onMatch="DENY" onMismatch="NEUTRAL" />
    <ThresholdFilter level="TRACE" onMatch="ACCEPT" onMismatch="DENY" />
    <!-- Other properties / attributes... -->
</Jdbc>

This is how it will translate:这是它将如何翻译:

--------------------------------
| Level   | Int value | Action |
--------------------------------
| FATAL   | 100       | DENY   |
--------------------------------
| ERROR   | 200       | DENY   |
--------------------------------
| WARN    | 300       | DENY   |
--------------------------------
| INFO    | 400       | DENY   |
--------------------------------
| DEBUG   | 500       | DENY   |
--------------------------------
| TRACE   | 600       | ACCEPT |
--------------------------------

If you referenced an appender in a logger and set a level, you are setting the minimum level.如果您在记录器中引用了一个 appender 并设置了一个级别,那么您就是在设置最低级别。

Lets say you didn't set the ThresHold filter for Foo ...假设您没有为Foo设置 ThresHold 过滤器...

<appenders>
    <FileAppender name="Foo" fileName="theLogPath/fooLog.log" />
</appenders>
<Loggers>
    <Logger name="package.FooClass">
        <AppenderRef ref="Foo" level="INFO" />
    </Logger>
</Loggers>

This would mean that everything between Fatal and Info would be logged.这意味着将记录 Fatal 和 Info 之间的所有内容。 If you wanted to include both Info and Warn, but ignore the rest, add a <ThresholdFilter> to the FileAppender like so:如果您想同时包含 Info 和 Warn,但忽略其余部分,请将<ThresholdFilter>添加到 FileAppender 中,如下所示:

<FileAppender name="Foo" fileName="theLogPath/fooLog.log">
    <ThresholdFilter level="ERROR" onMatch="DENY" onMisMatch="ACCEPT" />
</FileAppender>

And as long as you set the level in the <AppenderRef> , it will log everything between that level up to and including (ERROR).只要您在<AppenderRef>设置级别,它就会记录该级别之间的所有内容,包括(错误)。

Does that make sense?那有意义吗?

If this doesn't help, let me know what I'm missing :)如果这没有帮助,请告诉我我错过了什么:)

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

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