The standard java.util.logging was created after log4j, but it wasn't completely popular because log4j was easy to use. Therefore, an interface that does not depend on the log library was required. If the log library is different for each library, it will be difficult to combine the logs into one file, so basically it is better to use SLF4J.
SLF4J(Simple Logging Facade For Java) It seems that there are many configurations of SLF4J + logback or SLF4J + Log4j2. It seems that it was created because Commons Logging is difficult to use, so SLF4J cannot be used If you don't have a reason, it seems good to use this.
API (required): slf4j-api-\ *. Jar Hinding to log implementation (one of them) Log4j:slf4j-log4j12-*.jar Log4j2:log4j-slf4j-impl-*.jar Logback:logback-classic-*.jar java.util.logging:slf4j-jdk14-*.jar
jcl is Jakarta Commons Logging jul is java.util.logging Abbreviation for.
https://www.nextdoorwith.info/wp/se/slf4j/
Commons Logging
JBoss Logging
java.util.logging
Log4j Out of support. It is better to use log4j2.
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
log4j.properties
### 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 %5p %c{1} - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=log/log4j.log
log4j.appender.file.Append=true
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %5p %c{1} - %m%n
log4j.rootLogger=debug, stdout, file
About Conversion Pattern -Class name: "% C". The package name can be omitted by using "% C {1}". -Method name% M -Process ID is not supported ・ Thread ID is not supported
Divide the log file into multiple files
log4j.properties
### 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=%C{1} %M %t %d %5p %c{1} - %m%n
### direct messages to file mylog.log ###
log4j.appender.file1=org.apache.log4j.FileAppender
log4j.appender.file1.File=log/log4j1.log
log4j.appender.file1.Append=true
log4j.appender.file1.layout=org.apache.log4j.PatternLayout
log4j.appender.file1.layout.ConversionPattern=%d %5p %c{1} - %m%n
### direct messages to file mylog.log ###
log4j.appender.file2=org.apache.log4j.FileAppender
log4j.appender.file2.File=log/log4j2.log
log4j.appender.file2.Append=true
log4j.appender.file2.layout=org.apache.log4j.PatternLayout
log4j.appender.file2.layout.ConversionPattern=%d %5p %c{1} - %m%n
#
log4j.logger.kero.domain=debug, file1
log4j.logger.kero.view=debug, file2
log4j.logger.kero.infrastructure=debug, file2
log4j.rootLogger=debug, stdout
Log4j2
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.9.1</version>
</dependency>
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration>
<Configuration status="OFF">
<Properties>
<Property name="ptn">%d{yyyy-MM-dd HH:mm:ss.SSS} p=%-5p c=%c t=%t C=%C F=%F M=%M L=%L m=%m %n</Property>
</Properties>
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="${ptn}" />
</Console>
<RollingFile name="file" fileName="log/log4j2.log"
filePattern="log/%d{yyyyMM}/log4j2_%d{yyyyMMdd}_%i.log">
<PatternLayout pattern="${ptn}" />
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="2 MB" />
<TimeBasedTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy max="10" />
</RollingFile>
</Appenders>
<Loggers>
<Root level="all">
<AppenderRef ref="console" />
<AppenderRef ref="file" />
</Root>
</Loggers>
</Configuration>
package ml.kerotori.log4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Test1 {
static Logger logger = LogManager.getLogger(Test1.class);
public static void main(String[] args) {
logger.trace("Test1 main method");
}
}
Logback
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.3</version>
</dependency>
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE logback>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{yyyy-MM-dd HH:mm:ss} [%thread] %level %logger{0} - %msg \(%file:%line\)%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/logback.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/app.%d{yyyy-MM-dd}.log.tar.gz</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} - %m%n</pattern>
</encoder>
</appender>
<root level="DEBUG" additivity="false">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Test2 {
private static Logger log = LoggerFactory.getLogger("Test2");
public static void main(String[] args) {
log.debug("Test2 main method");
}
}
A soft grasp of the history and current status of Java logging libraries (for beginners) https://qiita.com/nisshiee/items/c5388f1d472ec86295e0
Recommended Posts