【问题标题】:Unable to create multiple log files based on the ThreadContext map values using routing appender in log4j2无法使用 log4j2 中的路由附加程序基于 ThreadContext 映射值创建多个日志文件
【发布时间】:2015-09-03 18:51:45
【问题描述】:

我在 12c 上构建的 oracle adf 应用程序中使用 log4j2 jar。

要求:根据会话创建多个日志文件并能够动态更改日志记录属性。

Log4j2.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace" packages="apps.adfAppUI.ui.bean">
    <Appenders>
        <File name="file" fileName="./adfAppCustomLogs/TestLog4j2.log">
            <PatternLayout>
                <Pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} %-5level %class %L %M - %msg%xEx%n</Pattern>
            </PatternLayout>
        </File>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="%m%n"/>
        </Console>
        <Routing name="AppRouting">
            <Routes pattern="$${ctx:ROUTINGKEY}">
                <!-- This route is chosen if ThreadContext has value 'user' for key ROUTINGKEY. -->
                <Route key="USER">
                    <RollingFile name="Rolling-USER-${ctx:ROUTINGKEY}-${ctx:LOGGEDSESSIONID}" append="true" fileName="./adfAppCustomLogs/${ctx:ROUTINGKEY}-${ctx:LOGINID}-${ctx:LOGGEDSESSIONID}.log"
                                 filePattern="./adfAppCustomLogs/archive/${date:yyyy-MM}/${ctx:LOGINID}-%d{MM-dd-yyyy}-%i.txt.gz">
                        <PatternLayout>
                            <Pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} %-5level %class %L %M - %msg%xEx%n</Pattern>
                        </PatternLayout>
                        <Policies>
                            <TimeBasedTriggeringPolicy interval="6" modulate="true" />
                            <SizeBasedTriggeringPolicy size="50 MB"/>
                        </Policies>
                    </RollingFile>
                </Route>
            </Routes>
        </Routing>
        <Async name="async" bufferSize="1000" includeLocation="true">
        <AppenderRef ref="AppRouting" />
    </Async>
    </Appenders>
    <Loggers>
        <Root level="trace">
            <!--<AppenderRef ref="file" level="DEBUG"/> -->
            <AppenderRef ref="async"/>
            <AppenderRef ref="STDOUT"/>
        </Root>
    </Loggers>
</Configuration>

我正在调用一个实用程序类,其中设置并在记录后清除线程上下文值。

问题:即使我更改了每个会话的线程上下文值,我也没有看到正在创建多个文件。所有日志都附加到一个文件中。但是当我重新启动服务器时,会生成一个新文件,并再次将所有会话日志附加到它。

谢谢。

【问题讨论】:

    标签: java oracle-adf log4j2 threadcontext


    【解决方案1】:

    以下配置将完成用例。

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="trace">
        <MapFilter onMatch="ACCEPT" operator="or">
            <KeyValuePair key="$${ctx:LOGLEVELYN}" value="Y"/>
        </MapFilter>
        <Appenders>
            <File name="file" fileName="./adfAppCustomLogs/TestLog4j2.log">
                <PatternLayout>
                    <Pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} %-5level %class %L %M - %msg%xEx%n</Pattern>
                </PatternLayout>
            </File>
            <Console name="STDOUT" target="SYSTEM_OUT">
                <PatternLayout pattern="%m%n"/>
            </Console>
            <Routing name="AppRouting">
                <Routes pattern="$${ctx:LOGGEDSESSIONID}">
                    <!-- This route is chosen if ThreadContext has no value for key ROUTINGKEY. -->
                    <Route key="$${ctx:LOGGEDSESSIONID}">
                        <RollingFile name="Rolling-ALL" fileName="./adfAppCustomLogs/DefaultAll.log"
                                     filePattern="./adfAppCustomLogs/archive/${date:yyyy-MM}/DefaultAll-%d{MM-dd-yyyy}-%i.txt.gz">
                            <PatternLayout>
                                <Pattern>%X %d{YYYY-MM-dd HH:mm:ss.SSS} %-5level %t %msg%xEx%n</Pattern>
                            </PatternLayout>
                            <Policies>
                                <TimeBasedTriggeringPolicy interval="6" modulate="true"/>
                                <SizeBasedTriggeringPolicy size="10 MB"/>
                            </Policies>
                        </RollingFile>
                    </Route>
                    <!-- This route is chosen if ThreadContext has value other than null for key ROUTINGKEY. -->
                    <Route>
                        <RollingFile name="Rolling-OTHER-${ctx:LOGGEDSESSIONID}"
                                     fileName="./adfAppCustomLogs/${ctx:LOGINID}-${ctx:LOGGEDSESSIONID}.log"
                                     filePattern="./adfAppCustomLogs/archive/${date:yyyy-MM}/${ctx:LOGINID}-%d{MM-dd-yyyy}-%i.txt.gz">
                            <PatternLayout>
                                <Pattern>%X %d{YYYY-MM-dd HH:mm:ss.SSS} %-5level %t %msg%xEx%n</Pattern>
                            </PatternLayout>
                            <Policies>
                                <TimeBasedTriggeringPolicy interval="6" modulate="true"/>
                                <SizeBasedTriggeringPolicy size="10 MB"/>
                            </Policies>
                            <!-- <DefaultRolloverStrategy max="100"/> -->
                        </RollingFile>
                    </Route>
                </Routes>
            </Routing>
            <Async name="async" bufferSize="1000" includeLocation="true">
                <AppenderRef ref="AppRouting"/>
            </Async>
        </Appenders>
        <Loggers>
            <Root level="trace">
                <!--<AppenderRef ref="file" level="DEBUG"/> -->
                <AppenderRef ref="async"/>
                <AppenderRef ref="STDOUT"/>
            </Root>
        </Loggers>
    </Configuration>
    

    【讨论】:

    • 除了纯XML之外,您能否还添加说明您原始XML中的问题是什么?
    猜你喜欢
    • 2018-09-15
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 2022-01-22
    • 2019-01-03
    相关资源
    最近更新 更多