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.
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 theSystem.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 referencedSystem.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 byorg.slf4j.simpleLogger.defaultLogLevel
will be used.org.slf4j.simpleLogger.showDateTime
- Set totrue
if you want to include the current date and time in the output messages. The default value isfalse
.org.slf4j.simpleLogger.dateTimeFormat
- The date and time format used in the output messages. The pattern describing the date and time format is defined bySimpleDateFormat
. If the format is not specified or is invalid, the number of milliseconds since startup will be output.org.slf4j.simpleLogger.showThreadName
- Set totrue
if you want to output the current thread name. The default value istrue
.- (Since versions 1.7.33 and 2.0.0-alpha6)
org.slf4j.simpleLogger.showThreadId
- Set totrue
if you want to output the current thread ID. The default value isfalse
. org.slf4j.simpleLogger.showLogName
- Set totrue
if you want to include the logger instance name in the output messages. The default value istrue
.org.slf4j.simpleLogger.showShortLogName
- Set this totrue
if you want the last component of the name to be included in the output messages. The default value isfalse
.org.slf4j.simpleLogger.levelInBrackets
- Should the log level string be output in brackets? The default value isfalse
.org.slf4j.simpleLogger.warnLevelString
- The string value for the warning level output. The default value isWARN
.
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.
Final Effect
As shown in the image below.