log4j2.xml中的xml条件代码 [英] xml conditional code in log4j2.xml

查看:168
本文介绍了log4j2.xml中的xml条件代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的log4j2.xml文件中创建一个条件语句,它似乎不接受任何条件格式.我已经尝试过各种选项,例如xslt等,但它似乎不起作用.任何帮助都会很棒.

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.

我的意图是基于操作系统为日志创建单独的路径.我看到追加器错误是因为尚未设置MyRollingLog值.但是,这是我无法解决的CLASS_NOT_FOUND错误,而invalid element.

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>

推荐答案

如果要有条件地登录到不同的附加程序或不同的位置(取决于环境),则可以添加具有默认值的Properties.例如:

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>

它将创建可取值的log4j2'LOG_DIR'变量:变量"$ LOG_PATH"的第一个,如果为空(:-部分),则查找系统变量logging.path,如果该变量也为空,后退到"./log". 您可以在<6>使用它:

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

如果变量"default.log.appender"不存在,则其值为"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>

这篇关于log4j2.xml中的xml条件代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆