xml conditional code in log4j2.xml

谁说我不能喝 提交于 2019-12-23 12:45:20

问题


I'm trying to create a conditional statement in my log4j2.xml file and it does not seem to accept any of the conditional formatting. I've tried various options such as xslt etc. and it doesn't seem to work. Any help here would be great.

My intention is to create separate paths for logging, based on the operating system. I see that the appender error is because the MyRollingLog value has not be set. However it's the CLASS_NOT_FOUND error that I'm not able to solve and the the invalid element.

I'm getting the following error for this code...

2014-06-10 17:19:48,771 ERROR Error processing element then: CLASS_NOT_FOUND

2014-06-10 17:19:48,773 ERROR appenders contains an invalid element or attribute "if"

2014-06-10 17:19:48,776 ERROR Unable to locate appender MyRollingLog for logger com.xxx.xyz

Any help here would be great.

<?xml version="1.0" encoding="UTF-8"?>
<configuration status = "WARN">
   <appenders>
    <if>
      <conditions>
          <condition property="isMac">
             <os family="mac" />
          </condition>
        </conditions>
        <then>
                     <RollingFile name="MyRollingLog" fileName='../logs/CheckView.log' 
 filePattern="../logs/$${date:yyyy-MM}/CheckView-%d{MM-dd-yyyy}-%i.log.gz">              
            <PatternLayout>
              <pattern>%d %p %m%n</pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="15 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="20"></DefaultRolloverStrategy>
         </RollingFile>   
        </then>
    </if>
    <Console name="Console-out" target="SYSTEM_OUT">
    <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %m%n"/>
   </Console> 
  </appenders>
  <loggers>
  <logger name="com.xxx.xyz"  level = "TRACE" additivity="false">
   <appender-ref ref="MyRollingLog" />
  </logger>
  <root level = "ERROR">
  <appender-ref ref="Console-out"  />
  </root>
  </loggers>
 </configuration>

回答1:


If you want to conditionally log to different appenders, or different places (depending on environment) you can add Properties with default value. ex.:

<Properties>
    <Property name="LOG_DIR">${LOG_PATH:-${sys:logging.path:-./log}}</Property>
    <Property name="APPENDER">${default.log.appender:-file}</Property>
</Properties>

It creates log4j2 'LOG_DIR' variable that can take value: first of variable "$LOG_PATH", if it's empty (part :-) than it looks for system variable logging.path, and if that as well is empty, fall-backs to "./log". You can use it in appender:

<RollingRandomAccessFile append="true" fileName="${LOG_DIR}/myapp.log" name="file">

If variable "default.log.appender" doesn't exist it takes value "file". So we can "conditionally" select appender by creating or not this variable. You can then choose appender (from existing one) ex.

<Loggers>
    <Logger name="com.example" level="debug" />
    <Root level="INFO">
        <AppenderRef ref="${APPENDER}"/>
    </Root>
</Loggers>



回答2:


You can conditionally add appenders in Log4j2

I had done it the following way. To conditionally show console output only if a variable was set to true. I am using a YAML configuration but you will get the gist. https://logging.apache.org/log4j/2.0/manual/configuration.html#Scripts

AsyncLogger:
  - name: SampleLogger
    level: error
    additivity: false
    AppenderRef:
      - ref: RollingError
      - ref: STDOUT
        ScriptFilter:
          onMatch: ACCEPT
          onMisMatch: DENY
          Script:
            name: ConsoleOutputCheck
            language: groovy
            value: "return (\"true\").equalsIgnoreCase(System.getenv(\"ConsoleOutput\"));"



回答3:


The log4j2 configuration does not support conditional logic.

However, you can achieve your goal of having a different paths for e.g. different OSs by using a system property for the file name. The documentation explains how to do this and has some samples: http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution



来源:https://stackoverflow.com/questions/24140359/xml-conditional-code-in-log4j2-xml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!