0

I have an old project that is doing

catch (Exception e) {
e.printStacktrace();
}

all over the place. Now I want to log those stacktraces to a file using Logback. I couldn't find any solution online.

Is there a way in Logback, to log System.Error statements to a file? I've tried the following but doesn't work, it only writes STDOUT logs to the file.

If anyone could help, it'd be great, thanks!

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)
littlejedi
  • 969
  • 1
  • 13
  • 23
  • What you're trying to do feels quite bad... but if it's your fancy, this [blog post](https://blogs.oracle.com/nickstephen/entry/java_redirecting_system_out_and) from oracle will hint you how to do it (albeit for the Java logging framework, but it's not difficult to port this to logback). You basically need to replace System.err with a custom PrintStream that redirects the calls to logback. Alternatively, could you do a string replace on your project? – Augusto Jan 19 '16 at 19:17
  • I know it's bad...It's just a really old project that needs *some* maintaining and logging, so I wanted to see if there's an easy way in Logback to do it : ( – littlejedi Jan 19 '16 at 19:33
  • I understand :) - There's tons of software out there written long time ago with ... 'different' practices. Please do check the blog post I linked as it should send you in the right direction. – Augusto Jan 19 '16 at 21:43
  • Yup, that helped, thanks a lot – littlejedi Jan 20 '16 at 23:04

1 Answers1

0

Ended up using a solution that redirects System err to a custom output printstream

log4j redirect stdout to DailyRollingFileAppender

Community
  • 1
  • 1
littlejedi
  • 969
  • 1
  • 13
  • 23