Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xml conditional code in log4j2.xml

Tags:

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

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>
like image 428
deleeps Avatar asked Dec 25 '22 08:12

deleeps


2 Answers

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>
like image 194
Augustin Ghauratto Avatar answered Dec 27 '22 21:12

Augustin Ghauratto


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

like image 20
Remko Popma Avatar answered Dec 27 '22 22:12

Remko Popma