My linux world » Java – Logger

Java - Logger


Contents

Configure logback

Add the logger dependency

If you use maven, to your pom.xml

  <dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>${logback.version}</version>
  </dependency>

Set logback.xml

Create the following file : src/main/resources/logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
      </Pattern>
    </layout>
  </appender>
 
  <logger name="net.rabahi" level="TRACE" />
  <logger name="net.rabahi.logger" level="TRACE" />
 
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>target/logger.log</file>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
      <Pattern>
	%d{yyyy-MM-dd HH:mm:ss} - %msg%n
      </Pattern>
    </encoder>
 
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- rollover daily -->
      <fileNamePattern>
         ./target/logger.%d{yyyy-MM-dd}.%i.log
      </fileNamePattern>
      <timeBasedFileNamingAndTriggeringPolicy
	  class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
	  <maxFileSize>10MB</maxFileSize>
      </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
 
  </appender>
 
  <root level="debug">
    <appender-ref ref="STDOUT" />
    <appender-ref ref="FILE" />
  </root>
</configuration>

Levels

Loggers have different levels : TRACE < DEBUG < INFO < WARN < ERROR

For each of these level, you can call them like this :

// TRACE
LOGGER.trace(message);
LOGGER.trace(message, throwable);
 
// DEBUG
LOGGER.debug(message);
LOGGER.debug(message, throwable);
 
// INFO
LOGGER.info(message);
LOGGER.info(message, throwable);
 
// WARN
LOGGER.warn(message);
LOGGER.warn(message, throwable);
 
// ERROR
LOGGER.error(message);
LOGGER.error(message, throwable);

Also, you can check if level are enabled

// TRACE
LOGGER.isTraceEnabled();
 
// DEBUG
LOGGER.isDebugEnabled();
 
// INFO
LOGGER.isInfoEnabled();
 
// WARN
LOGGER.isWarnEnabled();
 
// ERROR
LOGGER.isErrorEnabled();

Manage loggers

List categories

To get all the loggers :

LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory();
List<ch.qos.logback.classic.Logger> loggers = lc.getLoggerList();

To get loggers with only defined level :

// Get all loggers
LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory();
List<ch.qos.logback.classic.Logger> loggers = lc.getLoggerList();
 
List<ch.qos.logback.classic.Logger> result = new LinkedList<>();
 
for(ch.qos.logback.classic.Logger logger : loggers) {
	if(logger.getLevel() != null) {
		result.add(logger);
	}
}
 
// result list contains only loggers with defined level.

Create/Update logger level

From class:

LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory();
ch.qos.logback.classic.Logger logger = lc.getLogger(clazz);
logger.setLevel(newLevel);

From name:

LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory();
ch.qos.logback.classic.Logger logger = lc.getLogger(name);
logger.setLevel(newLevel);

Read logger level

From class:

LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory();
ch.qos.logback.classic.Logger logger = lc.getLogger(clazz);
logger.getLevel();

From name:

LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory();
ch.qos.logback.classic.Logger logger = lc.getLogger(name);
logger.getLevel();

Delete logger level

From class:

LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory();
ch.qos.logback.classic.Logger logger = lc.getLogger(clazz);
logger.setLevel(null);

From name:

LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory();
ch.qos.logback.classic.Logger logger = lc.getLogger(name);
logger.setLevel(null);

Copyright © 2024 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.