【问题标题】:Dyanmically populating a variable in a log file在日志文件中动态填充变量
【发布时间】:2019-02-07 16:21:33
【问题描述】:

我有一个处理用户请求的 Web 服务 (Jenkins),我希望能够动态地将请求会话 ID 附加到每个日志行,而不必将该变量实际添加到每个日志操作中。

我正在使用带有 slf4j 实现的 log4j2,我使用带有 org.apache.logging.log4j.core.config.Configurator 的外部配置文件初始化记录器,我使用每个会话创建一个记录器实例

final Logger logger = LoggerFactory.getLogger(MyClass.class);

例如:

logger.debug("received new request");
...
logger.debug("added something");

我希望将用户会话 ID 添加到每一行,而不必自己添加:

logger.debug("{} received new request",session.getId());
...
logger.debug("{} added something",session.getId());

我的 log4j2.xml 文件是:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Configuration status="INFO">
    <Properties>
        <Property name="logPath">...</Property>
        <Property name="rollingFileName">...</Property>
    </Properties>
    <Appenders>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="[%highlight{%-5level}] %d{DEFAULT} %c{1}.%M() - %msg%n%throwable{short.lineNumber}" />
        </Console>
        <RollingFile name="rollingFile" fileName="${logPath}/${rollingFileName}.log" filePattern="${logPath}/${rollingFileName}_%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="[%highlight{%-5level}] %d{DEFAULT} %c{1}.%M() - %msg%n%throwable{short.lineNumber}" />
            <Policies>
                <!-- Causes a rollover if the log file is older than the current JVM's start time -->
                <OnStartupTriggeringPolicy />
                <!-- Causes a rollover once the date/time pattern no longer applies to the active file -->
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Logger name="com.project" level="debug" additivity="false">
            <AppenderRef ref="console"/>
            <AppenderRef ref="rollingFile"/>
       </Logger>
    </Loggers>
</Configuration>

当前日志文件的实际结果:

[[36mDEBUG[m] 2019-02-05 16:42:09,794 SpellCheck.getResult() - start
[[36mDEBUG[m] 2019-02-05 16:42:10,420 SpellCheck.getResult() - Spelling correction returned no results.
[[36mDEBUG[m] 2019-02-05 16:42:10,420 SpellCheck.getResult() - end

我想要达到的目标:

[[36mDEBUG[m] 2019-02-05 16:42:09,794 SpellCheck.getResult() - 1234 - start
[[36mDEBUG[m] 2019-02-05 16:42:10,420 SpellCheck.getResult() - 1234 - Spelling correction returned no results.
[[36mDEBUG[m] 2019-02-05 16:42:10,420 SpellCheck.getResult() - 1234 - end

其中 1234 是例如会话 ID。

谢谢。

【问题讨论】:

    标签: dynamic log4j2 slf4j


    【解决方案1】:

    我想通了,比我想象的要容易得多。

    基本上添加了 %X{userSessionId} 到

    <PatternLayout pattern= ... /> 
    

    log4j2.xml 中的行。 并在代码中添加

    HttpSession session = request.getSession();
    org.apache.logging.log4j.ThreadContext.put("userSessionId", session.getId());
    

    【讨论】:

    • 仅供参考,这在 log4j2 手册中关于 ThreadContext 称为“鱼标记”的部分中进行了解释
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 2012-05-01
    • 2019-01-31
    相关资源
    最近更新 更多