简体   繁体   English

在log4j中配置RollingFileAppender

[英]Configuring RollingFileAppender in log4j

I'm working on a set of web services and we'd like to have a daily rotated log. 我正在研究一组Web服务,我们希望每天都有一个旋转日志。

I'm trying to get org.apache.log4j.rolling.RollingFileAppender from the log4j extras companion working, since the documentation suggests this is best for production environments. 我正在尝试从log4j extras配套工作中获取org.apache.log4j.rolling.RollingFileAppender ,因为文档表明这最适合生产环境。

I have both the main log4J library ( log4j-1.2.15.jar ) and the log4j extras library ( apache-log4j-extras-1.1.jar ) on the classpath. 我在类路径上有主log4J库( log4j-1.2.15.jar )和log4j额外库( apache-log4j-extras-1.1.jar )。

I have the following configuration for the appender in the log4j.properties file: 我在log4j.properties文件中为appender配置了以下配置:

### SOAP Request Appender
log4j.appender.request=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.request.File=SOAPmessages.log
log4j.appender.request.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.request.RollingPolicy.ActiveFileName =SOAPmessages-%d.log
log4j.appender.request.RollingPolicy.FileNamePattern=SOAPmessages-%d.log.zip
log4j.appender.request.layout = org.apache.log4j.PatternLayout
log4j.appender.request.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

However, when I start the web service with log4j in debug mode I get these error messages: 但是,当我在调试模式下使用log4j启动Web服务时,我收到以下错误消息:

log4j: Parsing appender named "request".
log4j: Parsing layout options for "request".
log4j: Setting property [conversionPattern] to [%d{ABSOLUTE} %5p %c{1}:%L - %m%n].
log4j: End of parsing for "request".
log4j: Setting property [file] to [/logs/SOAPmessages.log].
log4j:WARN Failed to set property [rollingPolicy] to value "org.apache.log4j.rolling.TimeBasedRollingPolicy". 
log4j:WARN Please set a rolling policy for the RollingFileAppender named 'request'
log4j: Parsed "request" options.

I've found documentation about how to configure this appender a little sparse, so can anyone help me fix my configuration? 我找到了关于如何配置这个appender有点稀疏的文档,那么任何人都可以帮我修复我的配置吗?

EDIT0: Added debug mode output, rather than just the standard warnings EDIT0:添加了调试模式输出,而不仅仅是标准警告

I had a similar problem and just found a way to solve it (by single-stepping through log4j-extras source, no less...) 我有一个类似的问题,只是找到了解决它的方法(通过单步执行log4j-extras源,不会少......)

The good news is that, unlike what's written everywhere, it turns out that you actually CAN configure TimeBasedRollingPolicy using log4j.properties (XML config not needed! At least in versions of log4j >1.2.16 see this bug report ) 好消息是,与各处写的不同,事实证明你实际上可以使用log4j.properties 配置TimeBasedRollingPolicy (不需要XML配置!至少在log4j> 1.2.16的版本中看到这个bug报告

Here is an example: 这是一个例子:

log4j.appender.File = org.apache.log4j.rolling.RollingFileAppender
log4j.appender.File.rollingPolicy = org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.File.rollingPolicy.FileNamePattern = logs/worker-${instanceId}.%d{yyyyMMdd-HHmm}.log

BTW the ${instanceId} bit is something I am using on Amazon's EC2 to distinguish the logs from all my workers -- I just need to set that property before calling PropertyConfigurator.configure() , as follow: BTW我在Amazon的EC2上使用${instanceId}位来区分我所有工作人员的日志 - 我只需要在调用PropertyConfigurator.configure()之前设置该属性,如下所示:

p.setProperty("instanceId", EC2Util.getMyInstanceId());
PropertyConfigurator.configure(p);

Faced more issues while making this work. 在完成这项工作时面临更多问题。 Here are the details: 以下是详细信息:

  1. To download and add apache-log4j-extras-1.1.jar in the classpath, didn't notice this at first. 要在类路径中下载并添加apache-log4j-extras-1.1.jar ,最初没有注意到这一点。
  2. The RollingFileAppender should be org.apache.log4j.rolling.RollingFileAppender instead of org.apache.log4j.RollingFileAppender . RollingFileAppender应该是org.apache.log4j.rolling.RollingFileAppender而不是org.apache.log4j.RollingFileAppender This can give the error: log4j:ERROR No output stream or file set for the appender named [file]. 这可以给出错误: log4j:ERROR No output stream or file set for the appender named [file].
  3. We had to upgrade the log4j library from log4j-1.2.14.jar to log4j-1.2.16.jar . 我们必须将log4j库从log4j-1.2.14.jar升级到log4j-1.2.16.jar

Below is the appender configuration which worked for me: 以下是适用于我的appender配置:

<appender name="file" class="org.apache.log4j.rolling.RollingFileAppender">
        <param name="threshold" value="debug" />
        <rollingPolicy name="file"
            class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
            <param name="FileNamePattern" value="logs/MyLog-%d{yyyy-MM-dd-HH-mm}.log.gz" />
            <!-- The below param will keep the live update file in a different location-->
            <!-- param name="ActiveFileName" value="current/MyLog.log" /-->
        </rollingPolicy>

        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%5p %d{ISO8601} [%t][%x] %c - %m%n" />
        </layout>
</appender>

Update: at least as early as 2013 (see Mubashar's comment) this started working. 更新:至少早在2013年(参见Mubashar的评论)就开始了。


According to Log4jXmlFormat you cannot configure it with log4j.properties, but only using the XML config format: 根据Log4jXmlFormat,您无法使用log4j.properties配置它,但只能使用XML配置格式:

Note that TimeBasedRollingPolicy can only be configured with xml, not log4j.properties 请注意,TimeBasedRollingPolicy只能使用xml配置,而不能使用log4j.properties配置

Unfortunately, the example log4j.xml they provide doesn't work either: 不幸的是,它们提供的示例log4j.xml也不起作用:

log4j:ERROR Parsing error on line 14 and column 76
log4j:ERROR Element type "rollingPolicy" must be declared.
...
log4j:WARN Please set a rolling policy for the RollingFileAppender named 'FILE'

Toolbear74 is right log4j.XML is required. Toolbear74是正确的log4j.XML是必需的。 In order to get the XML to validate the <param> tags need to be BEFORE the <rollingPolicy> I suggest setting a logging threshold <param name="threshold" value="info"/> 为了让XML验证<param>标签需要在<rollingPolicy>之前我建议设置一个记录阈值<param name="threshold" value="info"/>

When Creating a Log4j.xml file don't forget to to copy the log4j.dtd into the same location. 创建Log4j.xml文件时,不要忘记将log4j.dtd复制到同一位置。

Here is an example: 这是一个例子:

<?xml version="1.0" encoding="windows-1252"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<!-- Daily Rolling File Appender that compresses old files -->
  <appender name="file" class="org.apache.log4j.rolling.RollingFileAppender" >
     <param name="threshold" value="info"/>
     <rollingPolicy name="file"  
                      class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
        <param name="FileNamePattern" 
               value="${catalina.base}/logs/myapp.log.%d{yyyy-MM-dd}.gz"/>
        <param name="ActiveFileName" value="${catalina.base}/logs/myapp.log"/>
     </rollingPolicy>
     <layout class="org.apache.log4j.EnhancedPatternLayout" >
        <param name="ConversionPattern" 
               value="%d{ISO8601} %-5p - %-26.26c{1} - %m%n" />
    </layout>
  </appender>

  <root>
    <priority value="debug"></priority>
    <appender-ref ref="file" />
  </root>
</log4j:configuration>

Considering that your setting a FileNamePattern and an ActiveFileName I think that setting a File property is redundant and possibly even erroneous 考虑到你设置FileNamePatternActiveFileName我认为设置File属性是多余的,甚至可能是错误的

Try renaming your log4j.properties and dropping in a log4j.xml similar to my example and see what happens. 尝试重命名你的log4j.properties并放入类似于我的例子的log4j.xml,看看会发生什么。

Regarding error: log4j:ERROR Element type "rollingPolicy" must be declared 关于错误: log4j:ERROR Element type "rollingPolicy" must be declared

  1. Use a log4j.jar version newer than log4j-1.2.14.jar, which has a log4j.dtd defining rollingPolicy . 使用log4j.jar版本比log4j的-1.2.14.jar,其中有一个较新的log4j.dtd定义rollingPolicy
  2. of course you also need apache-log4j-extras-1.1.jar 当然你也需要apache-log4j-extras-1.1.jar
  3. Check if any other third party jars you are using perhaps have an older version of log4j.jar packed inside. 检查您使用的任何其他第三方罐子是否包含旧版本的log4j.jar。 If so, make sure your log4j.jar comes first in the order before the third party containing the older log4j.jar. 如果是这样,请确保您的log4j.jar在包含旧log4j.jar的第三方之前的顺序中排在第一位。

In Log4j2, the "extras" lib is not mandatory any more. 在Log4j2中,“extras”lib不再是必需的。 Also the configuration format has changed. 配置格式也发生了变化。

An example is provided in the Apache documentation Apache文档中提供了一个示例

property.filename = /foo/bar/test.log

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = /foo/bar/rolling/test1-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d %p %C{1.} [%t] %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 2
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=100MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5


logger.rolling.name = com.example.my.class
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile

I'm suspicious of the ActiveFileName property. 我怀疑ActiveFileName属性。 According to the log4j javadoc, there isn't such a property on the TimeBasedRollingPolicy class. 根据log4j javadoc, TimeBasedRollingPolicy类上没有这样的属性。 (No setActiveFileName or getActiveFileName methods.) (没有setActiveFileNamegetActiveFileName方法。)

I don't know what specifying an unknown property would do, but it is possible that it will cause the containing object to be abandoned, and that could lead to the warnings that you saw. 我不知道指定一个未知属性会做什么,但它可能会导致包含对象被放弃,这可能会导致您看到的警告。

Try leaving it out and seeing what happens. 试着把它留下来看看会发生什么。

You have a bad package name 你有一个糟糕的包名

org.apache.log4j.rolling.RollingFileAppender 

The correct one is: 正确的是:

org.apache.log4j.RollingFileAppender 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM