Log4j2具有相同的appender但多个记录器的文件名不同

时间:2016-09-20 15:31:36

标签: java logging log4j2 appender

我想在我的程序中使用不同的记录器。每个Logger都写入不同的文件。文件名是预定义的,不是动态的。例如,登录包将使用写入login.log文件的记录器。除文件名外,Appender都是相同的。这是一个程序的示例配置文件,该程序包含两个软件包' logging'并且'测试':

<?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>

正如您所看到的,除了文件名之外,appender是相同的。有没有办法只定义一个appender并将文件名传递给Logger?

2 个答案:

答案 0 :(得分:1)

不,它们是不同的Appender实例,即使它们的配置非常相似。 Log4j 2不提供共享Appender配置部分的机制。

你可以做的一件事就是减少重复是仅在一个appender上声明Delete操作,并使其与多个文件匹配,以便覆盖所有的appender。

您还可以在log4j2问题跟踪器或邮件列表上提出功能请求。

答案 1 :(得分:0)

我不确定,如果它可行,但值得尝试。您可以将公共appender片段提取到单独的文件中并使用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>
相关问题