Logging, part 1

(in Java with SLF4J and Logback)



Piotr Przybył
piotr@przybyl.org
piotrprz
Wrocław JUG, 2017-11-20

Perfect World

half full, half empty glass

How to find out how the system works?

  • Debugging
  • Logging
  • (Exploratory) testing
  • and others

Debugging

where the programme
when during execution
what anything that can be evaluated

Logging

where log file / console / DB / ...
when post factum
what only logged messages

Logs aren't invented in IT

Tachoscheibe

https://en.wikipedia.org/wiki/Tachograph#/media/File:Tachoscheibe.jpg

Tachoscheibe

https://en.wikipedia.org/wiki/Electrocardiography#/media/File:12leadECG.jpg

Logging libs in Java World

  • Log4J (2)
  • Java Util Logging
  • Logback
  • tinylog

Logging facades in Java World

  • Apache Commons Logging
  • SLF4J
facade

Log levels

  • FATAL
  • ERROR
  • WARN(ING)
  • INFO
  • DEBUG
  • TRACE
  • OFF

Setup


/path/to/bin/gradle wrapper --gradle-version 4.3.1
                

./gradlew init --type java-application
                

Let's log something

https://en.wikipedia.org/wiki/Scribe#/media/File:Escribano.jpg

Dependencies


dependencies {
    compile "org.slf4j:slf4j-api:1.7.25"
    runtimeOnly "ch.qos.logback:logback-classic:1.2.3"
}
            

Parameters


logger.debug("Fancy log statement with param1 {} and param2 {}",
             param1, getParam2());
            

catch(Exception e) {
    logger.debug("Aw, snap, an exception...", e);
}
            

Status


// assume SLF4J is bound to logback in the current environment
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
// print logback's internal status
StatusPrinter.print(lc);
                
Logback configuration manual



            

Appenders

  • Console
  • (Rolling)File
  • many others

Bugs

potato beetle

JMX Configurator


<contextName>logging-example-simple</contextName>
<jmxConfigurator />
            

jconsole

console

Some life in configuration

Janino

Janino is a super-small, super-fast Java compiler.


http://janino-compiler.github.io/janino/

"Inheritance"

Filtering

filter

Mapped Diagnostic Context


[user: %mdc{username}] - %msg %n
            

Next week: Logging, part 2

Graylog: one tool to log 'em all.

Logging, part 1

(in Java with SLF4J and Logback)


Thanks a lot.

Piotr Przybył
piotr@przybyl.org
piotrprz
Wrocław JUG, 2017-11-20
http://ow.ly/UPFW30gGV38
loggerSample  loggerWebSample
qr