有没有办法将System.err记录到Logback中的文件?

时间:2016-01-19 19:05:41

标签: java logback

我有一个正在做的旧项目

catch (Exception e) {
e.printStacktrace();
}
到处都是。 现在我想使用Logback将这些堆栈跟踪记录到文件中。我在网上找不到任何解决方案。

在Logback中是否有办法将System.Error语句记录到文件中?我尝试了以下但不起作用,它只将STDOUT日志写入文件。

如果有人可以提供帮助,那就太好了,谢谢!

def DEFAULT_PATTERN = "%d [%thread] %-5level %logger - %msg%n"

appender("STDOUT", ConsoleAppender) {
    encoder(PatternLayoutEncoder) { 
        pattern = DEFAULT_PATTERN 
        charset = Charset.forName("UTF-8")
    }
}

appender("SYSERR", ConsoleAppender) {
    encoder(PatternLayoutEncoder) { 
        pattern = DEFAULT_PATTERN 
        charset = Charset.forName("UTF-8")
    }
    target = "System.err"
}

appender("FILE", RollingFileAppender) {
  file = "logs/stdout.log"
  append = true
  encoder(PatternLayoutEncoder) {
    pattern = DEFAULT_PATTERN
    charset = Charset.forName("UTF-8")
  }
  filter(ch.qos.logback.classic.filter.ThresholdFilter) {
    level = INFO
  }
  rollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy) {
    fileNamePattern = "logs/stdout-%d{yyyy-MM-dd}.%i.log"
    maxHistory = 10
    timeBasedFileNamingAndTriggeringPolicy(ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP) {
      maxFileSize = "1MB"
    }
  }
}

appender("ERROR", RollingFileAppender) {
  file = "logs/errors.log"
  append = true
  encoder(PatternLayoutEncoder) {
    pattern = DEFAULT_PATTERN
    charset = Charset.forName("UTF-8")
  }
  filter(ch.qos.logback.classic.filter.ThresholdFilter) {
    level = ERROR
  }
  rollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy) {
    fileNamePattern = "logs/errors-%d{yyyy-MM-dd}.%i.log"
    maxHistory = 10
    timeBasedFileNamingAndTriggeringPolicy(ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP) {
      maxFileSize = "1MB"
    }
  }
}

/* Make Spring less verbose. */
//logger("org.springframework", INFO)
//logger("org.springframework.web.servlet", INFO)

/* Quieten Thymeleaf. */
//logger("org.thymeleaf", INFO)

/* Jetty can be really noisy on a shaded jar. */
//logger("org.eclipse.jetty.webapp.WebAppClassLoader", INFO)
//logger("org.eclipse.jetty.util.resource.JarResource", INFO)

/* Quieten Jetty in general. */
//logger("org.eclipse", DEBUG);

def appenders = []
appenders.add("STDOUT")
appenders.add("SYSERR")
appenders.add("FILE")
appenders.add("ERROR")

root(INFO, appenders)

1 个答案:

答案 0 :(得分:0)

使用将系统错误重定向到自定义输出打印流

的解决方案结束

log4j redirect stdout to DailyRollingFileAppender