无法使用 log4j2 中的路由附加程序基于 ThreadContext 映射值创建多个日志文件
Unable to create multiple log files based on the ThreadContext map values using routing appender in log4j2
我在基于 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>
我正在调用实用程序 class,其中会在记录后设置和清除线程上下文值。
问题:即使我更改了每个会话的 threadcontext 值,我也没有看到正在创建多个文件。所有日志都附加到一个文件中。但是当我重新启动服务器时,会生成一个新文件,所有会话日志都会再次附加到它。
谢谢。
下面的配置将完成用例。
<?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>
我在基于 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>
我正在调用实用程序 class,其中会在记录后设置和清除线程上下文值。
问题:即使我更改了每个会话的 threadcontext 值,我也没有看到正在创建多个文件。所有日志都附加到一个文件中。但是当我重新启动服务器时,会生成一个新文件,所有会话日志都会再次附加到它。
谢谢。
下面的配置将完成用例。
<?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>