<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
</dependency>
</dependencies>
properties
log4j.rootLogger=INFO, 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} [%c] %p: %m%n
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyClass {
private static final Logger LOGGER = LoggerFactory.getLogger(MyClass.class);
public void doSomething() {
LOGGER.debug("This is a debug message.");
LOGGER.info("This is an info message.");
LOGGER.warn("This is a warning message.");
LOGGER.error("This is an error message.");
}
}
2021-01-01 12:34:56 [MyClass] DEBUG: This is a debug message.
2021-01-01 12:34:57 [MyClass] INFO: This is an info message.
2021-01-01 12:34:58 [MyClass] WARN: This is a warning message.
2021-01-01 12:34:59 [MyClass] ERROR: This is an error message.