Andrey Hihlovskiy

Professional blog on groovy, gradle, Java, Javascript and other stuff.

Category Archives: logging

Gretty version 0.0.11 is out!

Gretty is a feature-rich gradle plugin for running web-applications under jetty.

New in version 0.0.11: now it’s possible to specify logback configuration file (.groovy or .xml) via plugin extension property “logbackConfigFile”.

Full sources, documentation and examples: https://github.com/akhikhl/gretty

Gretty 0.0.11 is also available on maven central under group “org.akhikhl.gretty”.

Advertisements

Programmatic configuration of slf4j/logback

Now I have experience with programmatic configuration of slf4j/logback.
Task
A program must open separate log file for each processed input file.
Solution for task
Instead of configuring logback via xml, the one needs to “manually” instantiate encoders, appenders and loggers, then configure and link them together.
Caveat 1
Logback goes crazy on attempt to share encoder (i.e. PatternLayoutEncoder) between appenders.
Solution for caveat 1
Create separate encoder for each appender.
Caveat 2
Logback refuses to log anything, if encoders and appenders are not associated with logging context.
Solution for caveat 2
Call setContext on each encoder and appender, passing LoggerFactory as a parameter.
Caveat 3
Logback refuses to log anything, if encoders and appenders are not started.
Solution for caveat 3
encoders and appenders need to be started in the correct order, i.e. first encoders, then appenders.
Caveat 4
RollingPolicy objects (i.e. TimeBasedRollingPolicy) produce strange error messages like “date format not recognized”, when they are not attached to the same context as appender.
Solutin for caveat 4
call setContext on RollingPolicy same way as on encoders and appenders.

Here is working example of “manual” logback configuration:


My conclusion: logback is much easier to configure via XML. “Manual” configuration is rather tedious task and should be avoided, unless it is dictated by project needs.