Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use logback.groovy file to log TRACE level to file and INFO to console

I am trying to make a log call direct different levels of output to different locations. I want all the logs to always go to the file, and just INFO and above to go to console. Is that not possible? I have the following and it doesn't work. Both are always the same:

def bySecond = timestamp("yyyyMMdd'.'HHmmss", context.birthTime)

appender("STDOUT", ConsoleAppender) {
  encoder(PatternLayoutEncoder) {
    pattern = "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"
  }
}

appender("FILE", FileAppender) {
  file = "./logs/log-${bySecond}.log"
  encoder(PatternLayoutEncoder) {
    pattern = "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"
  }
}

logger("com.crystal", WARN, ["STDOUT"])
logger("com.crystal", TRACE, ["FILE"])

root(TRACE)

scan()

Is it possible to direct the same log message to two different places based off different levels?

like image 705
Nick Banks Avatar asked May 13 '11 22:05

Nick Banks


People also ask

Which file is used by Logback logging System?

To create a configuration for Logback, you can use XML as well as Groovy. The system will automatically pick up and use the configuration automatically, as long as you're adhering to the naming convention. There are three valid standard file names you can choose from: logback-test.

How do I setup a Logback XML file?

In a Logback. xml file, all the configuration options are enclosed within the <configuration> root element. In the root element, you can set the debug=true attribute to inspect Logback's internal status. You can also configure auto scanning of the configuration file by setting the scan=true attribute.


1 Answers

send trace to both appenders

logger 'com.crystal', TRACE, ['STDOUT', 'FILE']

but add a filter to the ConsoleAppender

appender("FILE", FileAppender) {
  filter(ch.qos.logback.classic.filter.ThresholdFilter) {
    level = INFO
  }
  ...
}
like image 78
jpertino Avatar answered Sep 28 '22 06:09

jpertino