java - How to write different logs in different files with log4j2 (MDC in xml)? -
now i'm using structure this: appender:
<rollingfile name="user.log" append="true" filename="users/%mdc{username}.txt" filepattern="users/archive/%mdc{username}-%d{mm-dd-yyyy}-%i.txt.gz"> <patternlayout pattern="%-5p %d{mmmm-dd hh:mm:ss} %x: %c - %m%n"/> <policies> <timebasedtriggeringpolicy/> <sizebasedtriggeringpolicy size="50 mb"/> </policies> </rollingfile>
logger:
<appender-ref ref="user.log"> <threadcontextmapfilter onmatch="accept" onmismatch="deny" operator="or"> <keyvaluepair key="username" value="%x{username}"/> <keyvaluepair key="ip" value="%x{ip}"/> </threadcontextmapfilter> </appender-ref>
but doesn't work mdc keys. how use mdc keys in xml config rollingfileappender?
take @ routingappender. maybe can started:
<?xml version="1.0" encoding="utf-8"?> <configuration status="debug" name="myapp" packages=""> <appenders> <routing name="routing"> <routes pattern="$${ctx:username}"> <route> <rollingfile name="user.log" append="true" filename="users/${ctx:username}.txt" filepattern="users/archive/${ctx:username}-%d{mm-dd-yyyy}-%i.txt.gz"> <patternlayout> <pattern>%d{iso8601} [%t] %p %c %l - %m%n</pattern> </patternlayout> <policies> <timebasedtriggeringpolicy/> <sizebasedtriggeringpolicy size="50 mb"/> </policies> </rollingfile> </route> </routes> </routing> </appenders> <loggers> <root level="trace"> <appender-ref ref="routing" level="debug" /> </root> </loggers> </configuration>
Comments
Post a Comment