Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (322)
Collections:
Other Resources:
SLF4J Binding to Log4J Loger Example
How to use SLF4J API with Log4J Logger?
✍: Guest
If you want to use SLF4J API with JDK Logger,
you need specify slf4j-api-*.jar, slf4j-log4j*.jar and log4j-*.jar
in Java classpath as shown in this tutorial.
1. Write a simple Java program, Hello.java, to use SLF4J API:
// Copyright (c) FYIcenter.com
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Hello {
private static final Logger log = LoggerFactory.getLogger("fyiLog");
public static void main(String[] args) {
System.out.println("Log class: "+log.getClass().getName());
log.error("Hello - error");
log.warn("Hello - warn");
log.info("Hello - info");
log.debug("Hello - debug");
log.trace("Hello - trace");
}
}
2. Write a Log4J configuration file, log4j.properties, and keep it in the Java classpath.
# Root logger option
log4j.rootLogger=INFO, file, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p:: %m%n
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=Hello.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p:: %m%n
3. Download a Log4J version that matches the SLF4J bridge version.
SLF4J bridge to Log4J 1.2: slf4j-log4j12-1.7.31.jar Log4J 1.2: slf4j-log4j12-1.7.31.jar
4. Run Hello.java with slf4j-api-1.7.31.jar, slf4j-log4j12-1.7.31.jar, slf4j-log4j12-1.7.31.jar and log4j.properties in the Java class path:
$ java -version java version "15" 2020-09-15 $ java -cp .:slf4j-api-1.7.31.jar:slf4j-log4j12-1.7.31.jar:log4j-1.2.17.jar Hello.java Log class: org.slf4j.impl.Log4jLoggerAdapter 07-04 19:46:58 [main] ERROR:: Hello - error 07-04 19:46:58 [main] WARN :: Hello - warn 07-04 19:46:58 [main] INFO :: Hello - info
5. Review the output:
⇒ SLF4J - Simple Logging Facade for Java
⇐ SLF4J Binding to JDK Loger Example
2021-12-23, ∼1201🔥, 0💬
Popular Posts:
The JDT project provides the tool plug-ins that implement a Java IDE supporting the development of a...
JRE 8 rt.jar is the JAR file for JRE 8 RT (Runtime) libraries. JRE (Java Runtime) 8 is the runtime e...
What Is wstx-asl-3.2.8.jar? wstx-asl-3.2.8.jar is JAR file for the ASL component of Woodstox 3.2.8. ...
Smack is an Open Source XMPP (Jabber) client library for instant messaging and presence. A pure Java...
JDK 11 jdk.compiler.jmod is the JMOD file for JDK 11 Compiler tool, which can be invoked by the "jav...