一、slf4j+logback的配置
1、引入以下三个依赖文件
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
2、在项目resources目录下创建logback.xml文件
logback.xml文件的详细配置参考资料:https://logback.qos.ch/manual/configuration.html
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!--设置日志输出到控制台-->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoder的默认实现类是ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
</encoder>
</appender>
<!-- name值可以是包名或具体的类名:该包(包括子包)下的类或该类将采用此logger -->
<logger name="com.john.logging.b" level="INFO" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<!--设置日志输出为文件-->
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>logFile.log</file>
<encoder>
<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
3、编写测试类
public class TestLog {
private static Logger logger= LoggerFactory.getLogger(TestLog.class);
public static void main(String[] args){
logger.debug("this is debug log");
logger.info("this is info log");
logger.error("this is error log");
}
}
4、输出结果
二、log4j日志的配置
1、首先引入依赖的jar包
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
2、在项目resources目录下面添加log4j.properties属性文件
log4j.rootLogger=info, ServerDailyRollingFile, stdout
log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ServerDailyRollingFile.DatePattern='.'yyyy-MM-dd
log4j.appender.ServerDailyRollingFile.File=app-test.log
log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=%d - %m%n
log4j.appender.ServerDailyRollingFile.Append=true
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p [%c Line:%L] %m%n
log4j.appender.A1=org.apache.log4j.RollingFileAppender
log4j.appender.A1.Encoding=UTF-8
log4j.appender.A1.File=all.log