Logback被截断,而不是追加到日志文件 [英] Logback is truncating instead of appending to log file

查看:526
本文介绍了Logback被截断,而不是追加到日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在下面复制了一个简单的Logback配置.我的期望是应用程序将在启动时继续写入(即追加)到日志文件.但是,当我们重新启动应用程序时,现有文件将被丢弃,记录器将创建一个新文件.我的理解是FileAppender默认情况下会追加到日志文件.即使添加"true"也没有任何区别.我想念什么吗?

We have a straightforward Logback configuration copied below. My expectation is that the application would continue to write (i.e. append) to the log file on startup. However, when we restart the application, the existing file is discarded and the logger creates a new file. My understanding is that the FileAppender appends to the log file by default. Even adding the "true" does not make a difference. Am I missing something?

<configuration>
  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <append>true</append>
    <File>${logDir}/${filename}.log</File>
    <encoder>
      <pattern>%d{yyyyMMdd-HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
    <appender-ref ref="FILE" />
  </appender>

  <root level="INFO">
    <appender-ref ref="ASYNC"/>
  </root>
</configuration>

推荐答案

Luhar,

不确定是否仍在寻找答案,因为我看到很久以前就问过这个问题.但是我最近做了一些有关回滚的工作,但偶然发现了这个悬而未决的问题.您尝试做的事情看起来非常简单,所以我想我会稍作调整,然后看看发生了什么.

Not sure if you are still looking for an answer for this as I see that the question was asked quite a while back. But I recently did some work with logback and stumbled upon this unanswered question. What you were trying to do looked pretty straightforward, so I thought I'd give it a whirl and see what's going on.

使用下面的logback配置和我可以附加到日志文件的随附代码就可以了.

Using the following logback configuration and the accompanying code I was able to append to the log file just fine.

<configuration>
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <File>test.log</File>
        <encoder>
            <pattern>%d{yyyyMMdd-HH:mm:ss.SSS} [%thread] %-5level %logger{36} -
                %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="FILE" />
    </appender>
    <root level="INFO">
        <appender-ref ref="ASYNC" />
    </root>
</configuration>

使用上述配置的简单Java类:

Simple Java class using the configuration above:

public class App {

    static Logger logger = LoggerFactory.getLogger("SimpleTest");

    public static void main(String[] args) {
        logger.info("This is an INFO statement");
        logger.warn("This is a WARN statement");
        logger.error("This is an ERROR statement");

        try {
            Thread.sleep(2000);     
            // make sure the AsyncAppender queue is flushed
            // before program exits
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

以下是我的程序运行3次后的日志文件.

The following is my log file after 3 runs of the program.

20141222-22:32:10.391 [main] INFO SimpleTest- 这是一个INFO语句

20141222-22:32:10.391 [main] INFO SimpleTest - This is an INFO statement

20141222-22:32:10.393 [main] WARN SimpleTest- 这是一个WARN声明

20141222-22:32:10.393 [main] WARN SimpleTest - This is a WARN statement

20141222-22:32:10.393 [main]错误SimpleTest- 这是一个ERROR语句

20141222-22:32:10.393 [main] ERROR SimpleTest - This is an ERROR statement

20141222-22:32:18.558 [main] INFO SimpleTest- 这是一个INFO语句

20141222-22:32:18.558 [main] INFO SimpleTest - This is an INFO statement

20141222-22:32:18.560 [main] WARN SimpleTest- 这是一个WARN声明

20141222-22:32:18.560 [main] WARN SimpleTest - This is a WARN statement

20141222-22:32:18.560 [main]错误SimpleTest- 这是一个ERROR语句

20141222-22:32:18.560 [main] ERROR SimpleTest - This is an ERROR statement

20141222-22:32:23.192 [main] INFO SimpleTest- 这是一个INFO语句

20141222-22:32:23.192 [main] INFO SimpleTest - This is an INFO statement

20141222-22:32:23.194 [main] WARN SimpleTest- 这是一个WARN声明

20141222-22:32:23.194 [main] WARN SimpleTest - This is a WARN statement

20141222-22:32:23.194 [main]错误SimpleTest- 这是一个ERROR语句

20141222-22:32:23.194 [main] ERROR SimpleTest - This is an ERROR statement

因此,我认为登录配置正确运行.希望这可以帮助!

So I think the logback configuration is working correctly. Hope this helps!

这篇关于Logback被截断,而不是追加到日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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