You can configure slf4j-simple logger by configuring ‘simplelogger.properties’ file in application class path. Below table summarizes the properties used to configure the behavior of this logger.
Property |
Description |
org.slf4j.simpleLogger.logFile |
The output target which can be the path to a file, or the special values "System.out" and "System.err". Default is "System.err". |
org.slf4j.simpleLogger.cacheOutputStream |
If the output target is set to "System.out" or "System.err" (see preceding entry), by default, logs will be output to the latest value referenced by System.out/err variables. By setting this parameter to true, the output stream will be cached, i.e. assigned once at initialization time and re-used independently of the current value referenced by System.out/err. |
org.slf4j.simpleLogger.defaultLogLevel |
Default log level for all instances of SimpleLogger. Must be one of ("trace", "debug", "info", "warn", "error" or "off"). If not specified, defaults to "info". |
org.slf4j.simpleLogger.log.a.b.c |
Logging detail level for a SimpleLogger instance named "a.b.c". Right-side value must be one of "trace", "debug", "info", "warn", "error" or "off". When a SimpleLogger named "a.b.c" is initialized, its level is assigned from this property. If unspecified, the level of nearest parent logger will be used, and if none is set, then the value specified by org.slf4j.simpleLogger.defaultLogLevel will be used. |
org.slf4j.simpleLogger.showDateTime |
Set to true if you want the current date and time to be included in output messages. Default is false |
org.slf4j.simpleLogger.dateTimeFormat |
The date and time format to be 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 start up will be output. |
org.slf4j.simpleLogger.showThreadName |
Set to true if you want to output the current thread name. Defaults to true. |
org.slf4j.simpleLogger.showLogName |
Set to true if you want the Logger instance name to be included in output messages. Defaults to true. |
org.slf4j.simpleLogger.showShortLogName |
Set to true if you want the last component of the name to be included in output messages. Defaults to false. |
org.slf4j.simpleLogger.levelInBrackets |
Should the level string be output in brackets? Defaults to false. |
org.slf4j.simpleLogger.warnLevelString |
The string value output for the warn level. Defaults to WARN. |
Follow below step-by-step procedure to build complete working application.
Step 1: Create new maven project ‘slf4j-simple-log-configuration’.
Step 2: Update pom.xml with maven dependencies.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample.app</groupId>
<artifactId>slf4j-simple-log-configuration</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.31</version>
</dependency>
</dependencies>
</project>
Step 3: Define simplelogger.properties file under src/main/resources folder.
simplelogger.properties
org.slf4j.simpleLogger.defaultLogLevel=info org.slf4j.simpleLogger.showDateTime=true org.slf4j.simpleLogger.dateTimeFormat="yyyy.MM.dd G 'at' HH:mm:ss z" org.slf4j.simpleLogger.showThreadName=false
Step 4: Define HelloWorld application class.
HelloWorld.java
package com.sample.app;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloWorld {
public static void main(String args[]) {
Logger logger = LoggerFactory.getLogger(HelloWorld.class);
logger.info("Welcome to SLF4J logging");
}
}
Total project structure looks like below.
Run HelloWorld class, you will see below message in console.
"2021.07.05 AD at 14:46:10 IST" INFO com.sample.app.HelloWorld - Welcome to SLF4J logging
You can download complete working application from below link.
https://github.com/harikrishna553/java-libs/tree/master/slf4j/slf4j-simple-log-configuration
Reference
https://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html
http://www.slf4j.org/api/org/slf4j/impl/SimpleLogger.html
Previous Next Home
No comments:
Post a Comment