About
logback is a logging system.
Articles Related
Configuration
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:
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
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
List:
- SMTPAppender (Email)
File Appender
<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:
- my.log is the name of the file
- pattern defines the message format
Documentation / Reference