繁体   English   中英

Log4j2具有相同的附加程序,但多个记录器的文件名不同

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM