xml conditional code in log4j2.xml

del*_*eps 4 xml xslt log4j2

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
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Aug*_*tto 6

如果你想有条件地登录到不同的 appender,或者不同的地方(取决于环境),你可以添加Properties默认值。前任。:

<Properties>
    <Property name="LOG_DIR">${LOG_PATH:-${sys:logging.path:-./log}}</Property>
    <Property name="APPENDER">${default.log.appender:-file}</Property>
</Properties>
Run Code Online (Sandbox Code Playgroud)

它创建可以取值的 log4j2 'LOG_DIR' 变量:变量“$LOG_PATH”的第一个,如果它是空的(部分:-)而不是它查找系统变量logging.path,如果它也是空的,则回退到“./log” . 您可以在appender

<RollingRandomAccessFile append="true" fileName="${LOG_DIR}/myapp.log" name="file">
Run Code Online (Sandbox Code Playgroud)

如果变量“default.log.appender”不存在,则取值“file”。所以我们可以通过创建或不创建这个变量来“有条件地”选择appender。然后您可以选择appender(从现有的一个)ex。

<Loggers>
    <Logger name="com.example" level="debug" />
    <Root level="INFO">
        <AppenderRef ref="${APPENDER}"/>
    </Root>
</Loggers>
Run Code Online (Sandbox Code Playgroud)


Rem*_*pma 2

log4j2配置不支持条件逻辑。

但是,您可以通过使用文件名的系统属性来实现为不同操作系统(例如不同操作系统)设置不同路径的目标。该文档解释了如何执行此操作并有一些示例: http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution