[英]Log4j2 Having same appender but different filenames for multiple loggers
我想在程序中使用不同的記錄器。 每個記錄器均寫入不同的文件。 文件名是預定義的,不是動態的。 例如,登錄軟件包將使用寫入到login.log文件的記錄器。 除了文件名外,其他所有內容都相同。 這是一個程序的示例配置文件,該程序具有兩個軟件包“ logging”和“ test”:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Properties>
<Property name="log-location"> /home/roya/workspace/LogPractice/log</Property>
</Properties>
<Appenders>
<RollingFile name="logging" fileName="${log-location}/logging.log"
filePattern="${log-location}/$${date:yyyy-MM}/logging-%d{yyyy-MM-dd-HH}-%i.log.gz">
<PatternLayout pattern="%d %p %c{1.} [%t] %m%n" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="1 KB"/>
</Policies>
<DefaultRolloverStrategy max="5">
<!--
Nested conditions: the inner condition is only evaluated on files
for which the outer conditions are true.
-->
<Delete basePath="${log-location}" maxDepth="2">
<IfFileName glob="*/app-*.log.gz">
<IfLastModified age="30d">
<IfAny>
<IfAccumulatedFileSize exceeds="100 GB" />
<IfAccumulatedFileCount exceeds="10" />
</IfAny>
</IfLastModified>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<RollingFile name="test" fileName="${log-location}/test.log"
filePattern="${log-location}/$${date:yyyy-MM}/test-%d{yyyy-MM-dd-HH}-%i.log.gz">
<PatternLayout pattern="%d %p %c{1.} [%t] %m%n" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="1 KB"/>
</Policies>
<DefaultRolloverStrategy max="5">
<!--
Nested conditions: the inner condition is only evaluated on files
for which the outer conditions are true.
-->
<Delete basePath="${log-location}" maxDepth="2">
<IfFileName glob="*/app-*.log.gz">
<IfLastModified age="30d">
<IfAny>
<IfAccumulatedFileSize exceeds="100 GB" />
<IfAccumulatedFileCount exceeds="10" />
</IfAny>
</IfLastModified>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="logging">
<AppenderRef ref="logging" level="ALL" />
</Logger>
<Logger name="test">
<AppenderRef ref="test" level="ALL" />
</Logger>
<Root level="ALL">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
如您所見,除了文件名外,附加程序相同。 有沒有一種方法,我只定義一個附加程序,然后將文件名傳遞給Logger?
不,盡管它們的配置非常相似,但它們是不同的Appender實例。 Log4j 2沒有提供共享Appender配置部分的機制。
減少重復的一件事是只聲明一次Delete操作(在一個附加程序上,並使它與多個文件匹配,以便覆蓋所有附加程序)。
您還可以在log4j2問題跟蹤器或郵件列表上提出功能請求。
我不確定是否可行,但是值得嘗試。 您可以將公共追加程序片段提取到單獨的文件中,然后使用XInclude 。 此外,文件模式的重復部分可以被定義為屬性。 兩者都會極大地減少代碼的配置行。
這看起來像這樣。
文件log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Properties>
<Property name="log-location"> /home/roya/workspace/LogPractice/log</Property>
<Property name="dir">${log-location}/$${date:yyyy-MM}</Property>
<Property name="ext">-%d{yyyy-MM-dd-HH}-%i.log.gz</Property>
</Properties>
<Appenders>
<RollingFile name="logging" fileName="${log-location}/logging.log"
filePattern="${dir}/logging${ext}">
<xi:include href="log4j-xinclude-appender-config.xml" />
</RollingFile>
<RollingFile name="test" fileName="${log-test}/test.log"
filePattern="${dir}/test${ext}">
<xi:include href="log4j-xinclude-appender-config.xml" />
</RollingFile>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
文件log4j-xinclude-appender-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<PatternLayout pattern="%d %p %c{1.} [%t] %m%n" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="1 KB"/>
</Policies>
<DefaultRolloverStrategy max="5">
<!--
Nested conditions: the inner condition is only evaluated on files
for which the outer conditions are true.
-->
<Delete basePath="${log-location}" maxDepth="2">
<IfFileName glob="*/app-*.log.gz">
<IfLastModified age="30d">
<IfAny>
<IfAccumulatedFileSize exceeds="100 GB" />
<IfAccumulatedFileCount exceeds="10" />
</IfAny>
</IfLastModified>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.