Log4j+Commons-Logging
依赖
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.6.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-jcl</artifactId>
</exclusion>
</exclusions>
</dependency>
log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration PUBLIC "-//LOGGER"
"http://org/apache/log4j/xml/log4j.dtd">
<log4j:configuration>
<appender name="myConsole" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%-d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m%n"/>
</layout>
</appender>
<appender name="myFile1" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="E:/QFtrain/Log/projectLog4j.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="1kb"/>
<param name="MaxBackupIndex" value="2" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m%n" />
</layout>
</appender>
<root>
<priority value="all"/>
<appender-ref ref="myConsole"/>
<appender-ref ref="myFile1"/>
</root>
</log4j:configuration>
logback
依赖
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="true">
<property name="log.path" value="E:/QFtrain/Log" />
<property name="CONSOLE_LOG_PATTERN"
value="%d{yyyy-MM-dd HH:mm:ss.SSS} |-[%-5p] in %logger.%M[line-%L] -%m%n"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>debug</level>
</filter>
<encoder>
<Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="file" class="ch.qos.logback.core.FileAppender">
<file>${log.path}/projectLogback.log</file>
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="file2" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/projectLogback2.log</file>
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/logback-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>8kb</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>1</maxHistory>
</rollingPolicy>
</appender>
<root level="trace">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="file"/>
<appender-ref ref="file2"/>
</root>
</configuration>
测试
public class LogTest {
protected Log log = LogFactory.getLog(LogTest.class);
protected Logger logback = LoggerFactory.getLogger(LogTest.class);
@Test
public void log4jTest(){
log.trace("trace");
log.debug("debug");
log.info("info");
log.warn("warn");
log.error("error");
log.fatal("fatal");
}
@Test
public void logbackTest(){
logback.trace("trace");
logback.debug("debug");
logback.info("info");
logback.warn("warn");
logback.error("error");
}
}