SLF4J Logging Output

Recently, while working on a project using kotlin and vertx, logging has become a common requirement. This article introduces the use of slf4j-simple in vertx.

Vert.x Deprecation of Built-in Logging API

After version 4.0, vertx deprecated its built-in logging found under io.vertx.core.logging.LoggerFactory. Therefore, if you attempt to use it, you will receive a deprecation warning, as the official logging library is no longer maintained.

image-20240620224234263

Thus, you need to choose an appropriate logging library; in this case, I am using slf4j.

Introduction

Add the dependency in the dependencies section of build.gradle.kts:

implementation("org.slf4j:slf4j-simple")

Since vertx already depends on org.slf4j:slf4j-api, there is no need to add it again. However, if you are working with another type of project, you should also include implementation("org.slf4j:slf4j-api") in your dependencies.

Usage

Import org.slf4j.LoggerFactory in your code and use its getLogger method to obtain an instance:

import org.slf4j.LoggerFactory

class MainVerticle : AbstractVerticle() {

  companion object {
    val logger = LoggerFactory.getLogger(MainVerticle::class.java)!!
  }
}

Configuration

You can specify configuration using system properties (such as those set in JVM startup arguments), or by adding a simplelogger.properties file in src/main/resources (ensure the filename is correct). An example configuration is as follows:

org.slf4j.simpleLogger.logFile=System.out
org.slf4j.simpleLogger.log.com.zhaoxinsoft=debug
org.slf4j.simpleLogger.showDateTime=true
org.slf4j.simpleLogger.dateTimeFormat=MM-dd HH:mm:ss:SSS
#org.slf4j.simpleLogger.showShortLogName=true
org.slf4j.simpleLogger.showThreadName=false
org.slf4j.simpleLogger.levelInBrackets=true
org.slf4j.simpleLogger.log.org.mongodb=error
# This is a comment line. The commented line will not take effect.

The configurable properties and their explanations are as follows (reference):

  • org.slf4j.simpleLogger.logFile - The output target, which can be a path to a file or the special values "System.out" and "System.err". The default value is "System.err".
  • org.slf4j.simpleLogger.cacheOutputStream - If the output target is set to "System.out" or "System.err" (see the previous entry), by default, the logs will output to the latest value of the System.out/err variables. By setting this parameter to true, the output stream will be cached, meaning it is allocated once at initialization and reused independently of the currently referenced System.out/err values.
  • org.slf4j.simpleLogger.defaultLogLevel - The default log level for all SimpleLogger instances. It must be one of "trace", "debug", "info", "warn", "error", or "off". If not specified, the default is "info". The levels increase to the right, and at higher levels, lower-level logs will not be printed, so by default, "debug" and "trace" will not be output.
  • org.slf4j.simpleLogger.log.a.b.c - The log detail level for the SimpleLogger instance named "a.b.c". The value on the right must be one of "trace", "debug", "info", "warn", "error", or "off". When the SimpleLogger instance named "a.b.c" is initialized, its level will be assigned from this property. If not specified, the level of the nearest parent logger will be used, and if that is not set, the value specified by org.slf4j.simpleLogger.defaultLogLevel will be used.
  • org.slf4j.simpleLogger.showDateTime - Set to true if you want to include the current date and time in the output messages. The default value is false.
  • org.slf4j.simpleLogger.dateTimeFormat - The date and time format used in the output messages. The pattern describing the date and time format is defined by SimpleDateFormat. If the format is not specified or is invalid, the number of milliseconds since startup will be output.
  • org.slf4j.simpleLogger.showThreadName - Set to true if you want to output the current thread name. The default value is true.
  • (Since versions 1.7.33 and 2.0.0-alpha6) org.slf4j.simpleLogger.showThreadId - Set to true if you want to output the current thread ID. The default value is false.
  • org.slf4j.simpleLogger.showLogName - Set to true if you want to include the logger instance name in the output messages. The default value is true.
  • org.slf4j.simpleLogger.showShortLogName - Set this to true if you want the last component of the name to be included in the output messages. The default value is false.
  • org.slf4j.simpleLogger.levelInBrackets - Should the log level string be output in brackets? The default value is false.
  • org.slf4j.simpleLogger.warnLevelString - The string value for the warning level output. The default value is WARN.

Note

If the same property name is specified in both the JVM startup parameters and the properties file, the property specified in the JVM will take precedence. For example:

java -Dorg.slf4j.simpleLogger.log.com.zhaoxinsoft=trace

Using Grep Console

If you are using IDEA and want to differentiate log levels by color, you can use the Grep Console plugin. The configuration reference is shown in the image below, with the prerequisite that org.slf4j.simpleLogger.logFile=System.out is set.

image-20240620230059592

Final Effect

As shown in the image below.

image-20240620231705503