pratiktest
10/4/2016 - 5:22 AM

logging.md

Logback

logback link

  • to configure logback have slf4j-api jar and logback-core jar
package chapters.introduction;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld1 {

  public static void main(String[] args) {

    Logger logger = LoggerFactory.getLogger("chapters.introduction.HelloWorld1");
    logger.debug("Hello world.");

  }
}
  • slf4j is nothing but an interface. It hooks to the logging library and abstracts the actual implementation. We will mostly use slf4j methods for logging

  • to print logback related issues use the logback StatusPrinter class

package chapters.introduction;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.util.StatusPrinter;

public class HelloWorld2 {

  public static void main(String[] args) {
    Logger logger = LoggerFactory.getLogger("chapters.introduction.HelloWorld2");
    logger.debug("Hello world.");

    // print internal state
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    StatusPrinter.print(lc);
  }
}
  • You might see the following output
12:49:22.203 [main] DEBUG chapters.introduction.HelloWorld2 - Hello world.
12:49:22,076 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
12:49:22,078 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
12:49:22,093 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.xml]
12:49:22,093 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Setting up default configuration.

  • This means we have not configured the logback files and the information is going to default configured console appender
  • We can configure different appenders for different log levels (info, debug, warn, error). We can also create custom appenders.
  • some example of appenders is TCP sockets, file, JMS(Java messaging service, standard to send receive messages), syslog etc

logback modules

  • logback has 3 modules

    • logback-core
    • logback-classic
    • logback-access
  • The classic module implements the slf4j api, so we can easily switch between logback and any other logger

  • classic module extends core

  • access module is used to provide HTTP-access log functionality

  • we will mostly use classic

  • Logback is build on 3 classes

    • Logger (classic)
    • Appender (core)
    • Layout (core)

What Problem does slf4j solve (Nambis interpretation)

  • lets say we import an hibernate jar.
  • This jar comes with its own logger implementation. Lets say it uses java util logger or apache commons logger
  • Lets say our application uses logback to log
  • Now we have configured the logs for our application to go to x.blah.com in config file of logback
  • Now logback has slf4j interface so we use slf4j methods to log
  • If java util/apache commons also has same interface the slf4j will convert this to appropriate log command and log this to the correct configuration of our application
  • basically it will adapt the log to fit to the config for logback of our application and log it as specified in config file of our application

Why use slf4j

  • lets say we include a hubernate jar which has a logging config for java.util
  • However our application uses logback
  • now how do we log the messages of hibernate into our logging system (say our elastic search)
  • We use slf4j which is a facade. If hibernate uses slf4j and our system uses slf4j, we will basically have a common interface. slf4j will automatically adapt the hibernate logs to log it to our logging backend since the interface is same(as both logback and java util use slf4j) we dont need to change the hibernate code and can use the jar as is...
  • slf4j cannot be used by itself it needs an underlying logging system (hence one more dependency) like logback or log4j to be present with it with its configurations to log to respective logging backend