Java Logger - Logback

Java Conceptuel Diagram

About

logback is a logging system.

Configuration

http://logback.qos.ch/manual/configuration.html

example

<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <logger name="com.mchange.v2" level="warn"/>
  <logger name="io.netty" level="warn"/>
  <logger name="io.vertx" level="info"/>
  <logger name="net.bytle" level="warn"/>

  <root level="debug">
    <appender-ref ref="STDOUT"/>
  </root>

</configuration>

where, there is:

  • one appender that write to the console with the message format %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
  • several logger that gets their level.
  • the root logger gets the debug level and the console appender. All other logger will inherit from it.

Set the configuration file

  • at the command line
java -Dlogback.configurationFile=/path/to/config.xml chapters.configuration.MyApp1
  • in the code
import ch.qos.logback.classic.util.ContextInitializer;

public class ServerMain {
    public static void main(String args[]) throws IOException, InterruptedException {
       // must be set before the first call to  LoggerFactory.getLogger();
       // ContextInitializer.CONFIG_FILE_PROPERTY is set to "logback.configurationFile"
       System.setProperty(ContextInitializer.CONFIG_FILE_PROPERTY, "/path/to/config.xml");
       ...
    }   
}

Rescan

<configuration scan="true" scanPeriod="30 seconds" > 
  ... 
</configuration> 

Logger

Parameterized message

With a parameterized message only after evaluating whether to log or not, and only if the decision is positive, will the logger implementation format the message and replace the '{}' pair with the string value of entry. In other words, this form does not incur the cost of parameter construction when the log statement is disabled.

logger.debug("The entry is {}.", entry);

// or

logger.debug("The new entry is {}. It replaces {}.", entry, oldEntry);

// or

Object[] paramArray = {newVal, below, above};
logger.debug("Value {} was inserted between {} and {}.", paramArray);

Configuring logger

Configuring loggers, or the

element]]

One appender use on the root and chapters.configuration logger

<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <logger name="chapters.configuration">
    <appender-ref ref="STDOUT" />
  </logger>

  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>
Appender

Java - Appender (or Handlers) in Logback

Doc

List:

File Appender

Doc

<configuration>
  <appender name="fileAppender" class="ch.qos.logback.core.rolling.FileAppender">
    <file>my.log</file>
    <encoder>
      <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>
  [...]
</configuration>

where:

Documentation / Reference





Discover More
Java Conceptuel Diagram
Java - Logger

A Logger is the entry point to logs messages. The main entity on which applications make logging calls. A Logger object is used to log messages. It has a hierarchical scope that is generally the...
Java Conceptuel Diagram
Java - Slf4j

slf4j ) is a façade for loggers. To use a specific logger, add one of the binding logger package. Library should declare a runtime dependency only on on slf4j-api and not on any SLF4J binding but...
Data System Architecture
Log - Logging

A log is a text file with: a timed list of message (activities) that an application has performed execution/request log: web server: web log ( - that stores the Http request error cron...
Java Conceptuel Diagram
Logging - Context (Mapped Diagnostic Context)

Context data (also known as Mapped Diagnostic Context or MDC) To be able to follow a request that flow in different component, you need to put the request ID in every message (generally a GUID. It's...
Java Conceptuel Diagram
Logging - Logger Manager | Logger Factory

A static logger factory is the entry point to get/create/retrieve a logger Logger Manager manages the loggers and its hierarchical namespace (ie if a logger has a parent it will inherit all properties...
Java Conceptuel Diagram
Vert.x - Logger

logging in Vertx is managed by the Vertx Logger The Vertx logger is a facade for logging system. It uses the JUL logger as default logging...



Share this page:
Follow us:
Task Runner