Log4j RollingFileAppender不能按预期工作[英] Log4j RollingFileAppender not working as expected

本文是小编为大家收集整理的关于Log4j RollingFileAppender不能按预期工作的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我只是在一个应用程序上,我必须根据大小维护日志,而我已经按照以下代码段进行了,我期望的是每10mb之后,一个新的日志滚子说mylog.1.log, mylog.2.log等将创建,不幸的是它不会发生.

尽管它创建mylog.1.log,但张贴它删除mylog.1.log并创建mylog.2.log,此 不是预期的行为.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration debug="true"
                 xmlns:log4j='http://jakarta.apache.org/log4j/'>

   <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">

      <param name="append" value="true"/>
      <param name="file" value="E:/logs/Mylog.log"/>
      <param name="MaxFileSize" value="10MB"/>
      <param name="MaxBackupIndex" value="10"/>

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

   <root>
      <level value="DEBUG"/>
     <appender-ref ref="fileAppender"/>
   </root>

环境 OS:Windows 7 应用程序服务器:IBM WebSphere 8 编程语言:Java

谷歌搜索了很多,结果似乎并没有富有成果,任何帮助都将受到赞赏.谢谢.

推荐答案

我们的生产中有以下Log4J配置运行良好.使用的log4j版本为1.2.15

<appender name="FILE_JVM" class="org.apache.log4j.RollingFileAppender">
    <param name="file" value="E:/logs/app.log"/>
    <param name="maxFileSize" value="20MB"/>
    <param name="maxBackupIndex" value="50"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{ISO8601} %-5p [%t] %C{2} (%F:%L) - %m%n"/>
    </layout>
</appender>

将其与您的i只是params maxfilesize&maxbackupIndex的情况不同.

我尝试了一个带有log4j 1.2.17的示例,并且能够看到日志通过app.log.1,app.log.2,app.log.3等在我的日志目录中的文件上播放.尝试在您的尽头运行它,看看可能缺少什么.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration>

    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ISO8601} %-5p [%t] %C{2} (%F:%L) - %m%n"/>
        </layout>
    </appender>

    <appender name="FILE_JVM" class="org.apache.log4j.RollingFileAppender">
        <param name="file" value="E:/Sundry/Projects/Log4jEmailSample/logs/app.log"/>
        <param name="maxFileSize" value="1MB"/>
        <param name="maxBackupIndex" value="50"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ISO8601} %-5p [%t] %C{2} (%F:%L) - %m%n"/>
        </layout>
    </appender>

    <!-- the ROOT Logger -->
    <root>
        <level value="INFO"/>
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILE_JVM"/>
    </root>

</log4j:configuration>

独立应用下方

import org.apache.log4j.Logger;

import java.util.concurrent.TimeUnit;

public class MainApp {


    public static void main(String[] args) {
        Logger logger = Logger.getLogger(MainApp.class);
        while (true) {
            logMessages(logger);
            try {
                TimeUnit.MILLISECONDS.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private static void logMessages(Logger logger) {
        logger.info("Info message");
    }

}

我在课程路径中唯一的罐子是log4j-1.2.17.jar.它在您的尽头工作吗?

其他推荐答案

这是因为在rollOver()中RollingFileAppender.java中的方法,重命名当前日志文件正在失败.

renameSucceeded = file.renameTo(target);

在这里file.renameTo(target)返回false.要解决此问题,请覆盖该方法并添加自定义代码以解决问题.一种方法是在上述语句之后添加以下代码.

if(!renameSucceeded)                                //custom code begins
      {                                                 
         try 
         {
            FileUtils.copyFile(file, target);
            BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
            bw.write("");
            bw.flush();
            bw.close();
            renameSucceeded = true;
        } 
         catch (IOException e) {
             renameSucceeded = false;
        }
      }

本文地址:https://www.itbaoku.cn/post/1575112.html

问题描述

I'm just working on an application where I gotta maintain logs based on size, and I have done so as below code snippet, what I was expecting was after every 10MB, a new log roller say Mylog.1.log, Mylog.2.log and so on will be created, unfortunately its not happening.

Though it creates Mylog.1.log, post that its removing Mylog.1.log and creating Mylog.2.log, this is not expected behaviour.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration debug="true"
                 xmlns:log4j='http://jakarta.apache.org/log4j/'>

   <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">

      <param name="append" value="true"/>
      <param name="file" value="E:/logs/Mylog.log"/>
      <param name="MaxFileSize" value="10MB"/>
      <param name="MaxBackupIndex" value="10"/>

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

   <root>
      <level value="DEBUG"/>
     <appender-ref ref="fileAppender"/>
   </root>

Environment OS : Windows 7 Application Server : IBM Websphere 8 Programming language : java

Googled a lot, result doesnt seems fruitful, any help is appreciated. Thanks.

推荐答案

We have the below log4j configuration running well in production. The log4j version used is 1.2.15

<appender name="FILE_JVM" class="org.apache.log4j.RollingFileAppender">
    <param name="file" value="E:/logs/app.log"/>
    <param name="maxFileSize" value="20MB"/>
    <param name="maxBackupIndex" value="50"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{ISO8601} %-5p [%t] %C{2} (%F:%L) - %m%n"/>
    </layout>
</appender>

Comparing it with your I just the case of params maxFileSize & maxBackupIndex different.

I tried a sample with log4j 1.2.17 and was able to see the logs getting rolled over with app.log.1, app.log.2, app.log.3 and so on files in my logs directory. Try running it at your end and see what could be missing.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration>

    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ISO8601} %-5p [%t] %C{2} (%F:%L) - %m%n"/>
        </layout>
    </appender>

    <appender name="FILE_JVM" class="org.apache.log4j.RollingFileAppender">
        <param name="file" value="E:/Sundry/Projects/Log4jEmailSample/logs/app.log"/>
        <param name="maxFileSize" value="1MB"/>
        <param name="maxBackupIndex" value="50"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ISO8601} %-5p [%t] %C{2} (%F:%L) - %m%n"/>
        </layout>
    </appender>

    <!-- the ROOT Logger -->
    <root>
        <level value="INFO"/>
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILE_JVM"/>
    </root>

</log4j:configuration>

Below the standalone app

import org.apache.log4j.Logger;

import java.util.concurrent.TimeUnit;

public class MainApp {


    public static void main(String[] args) {
        Logger logger = Logger.getLogger(MainApp.class);
        while (true) {
            logMessages(logger);
            try {
                TimeUnit.MILLISECONDS.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private static void logMessages(Logger logger) {
        logger.info("Info message");
    }

}

The only jar I have in my class path is log4j-1.2.17.jar. Does it work at your end?

其他推荐答案

This happens because in rollOver() method in RollingFileAppender.java, renaming of the current log file is failing.

renameSucceeded = file.renameTo(target);

Here file.renameTo(target) returns false. To solve this, override the method and add custom code to fix the issue. One way to do is add the below code after the above statement.

if(!renameSucceeded)                                //custom code begins
      {                                                 
         try 
         {
            FileUtils.copyFile(file, target);
            BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
            bw.write("");
            bw.flush();
            bw.close();
            renameSucceeded = true;
        } 
         catch (IOException e) {
             renameSucceeded = false;
        }
      }