We have already know how to use an appender to send output to the console, now we see how to create a file appender, and also how to create a configuration logger, to describe a relation between a SLF4J logger, as defined and used in our code, and an appender.
Here is an XML Logback configuration file:
<configuration debug="true"> <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" /> <appender name="CApp" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36}: %msg%n</pattern> </encoder> </appender> <appender name="FApp" class="ch.qos.logback.core.FileAppender"> <file>TestLogback.log</file> <encoder> <pattern>%date %level [%thread] %logger{10} [%file:%line]: %msg%n</pattern> </encoder> </appender> <logger name="test.TestLogback" level="info"> <appender-ref ref="FApp" /> </logger> <root level="trace"> <appender-ref ref="CApp" /> </root> </configuration>You see that this configuration file is set to be in debug mode, and it has a status listener element set to output its feedback to the standard console. More on Logback configuration debug in the previous post.
Same for the CApp appender, console appender already seen in the previous post.
The FApp appender is a FileAppender. We specify its name and the format internally used.
The logger element works like a root element, the biggest difference is due that root does not need a name, while a logger has to provide it, to be used by the SLF4J logger factory to retrieve the settings.
We can specify the additivity attribute in the logger, defaulted to true, if not explicitly set to false, it means that this logger uses the appenders specified by its ancestors (if any) and adds the one(s) specified in this element's belly. False means that all the existing appenders are discarded and only the one specified there are used.
The logger level is explicitly set to "info", this logger and all the ones on hierarchy below are affected by this.
We can get the same result by a groovy configuration file:
import static ch.qos.logback.classic.Level.DEBUG import static ch.qos.logback.classic.Level.INFO import ch.qos.logback.classic.encoder.PatternLayoutEncoder import ch.qos.logback.core.status.OnConsoleStatusListener import ch.qos.logback.core.ConsoleAppender import ch.qos.logback.core.FileAppender statusListener(OnConsoleStatusListener) appender("CApp", ConsoleAppender) { encoder(PatternLayoutEncoder) { pattern = "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36}: %msg%n" } } appender("FApp", FileAppender) { file = "TestLogback.log"; encoder(PatternLayoutEncoder) { pattern = "%date %level [%thread] %logger{10} [%file:%line]: %msg%n" } } logger("test.TestLogback", INFO, ["FApp"]) root(DEBUG, ["CApp"])The lack of semicolons at the end of lines is a perlish Groovy variation on Java syntax.
No comments:
Post a Comment