slf4j
前にから気になってたロガーのslf4jを使ってみる
Main.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Main {
private static final Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) throws Exception {
logger.info("hoge");
}
}
log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration>
<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="sample.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="5MB" />
<param name="MaxBackupIndex" value="5" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %5p %c{1} %m%n" />
</layout>
</appender>
<root>
<priority value ="INFO" />
<appender-ref ref="FILE"/>
</root>
</log4j:configuration>
logback.xml
<configuration>
<appender name="STDOUT1" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>TRACE</level>
</filter>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MMM-dd HH:mm:ss.SSS} %-5level %marker %logger - %msg%n</Pattern>
</layout>
</appender>
<appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
<append>true</append>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>info.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>info.%d{yyyy-MM-dd}.zip</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>time:%d{yyyy-MMM-dd HH:mm:ss.SSS} level:%level %marker thread:%thread logger:%logger message:%msg%n
</pattern>
</encoder>
</appender>
<appender name="ASYNC_STDOUT1" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="STDOUT1"/>
</appender>
<appender name="ASYNC_INFO" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="INFO"/>
</appender>
<root level="INFO">
<appender-ref ref="ASYNC_STDOUT1"/>
<appender-ref ref="ASYNC_INFO"/>
</root>
</configuration>
build.gradle
apply plugin: "java"
apply plugin: "eclipse"
repositories {
mavenCentral()
}
dependencies {
compile "org.slf4j:slf4j-api:1.7.5"
//compile "org.slf4j:slf4j-simple:1.7.5"
compile "org.slf4j:slf4j-log4j12:1.7.5"
//compile "ch.qos.logback:logback-classic:1.0.13"
}
log4jでログを出すならslf4j-log4j12をクラスパスに、logbackでログを出すならlogback-classicをクラスパスに指定する事でログ出力を行う実装を簡単に切り替える事が出来る的な感じかと