springboot使用slf4j

1、引入lombok
<dependency>
	<groupId>org.projectlombokgroupId>
	<artifactId>lombokartifactId>
dependency>
2、添加文件名称为logback-spring.xml的配置文件到classpash,内容如下:

<configuration debug="false">
	
	<property name="LOG_HOME" value="./logs" />

	
	
	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
			
			<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%npattern>
		encoder>
	appender>

	
	<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
			
			<FileNamePattern>${LOG_HOME}/cmppv2-%d{yyyy-MM-dd}.%i.logFileNamePattern>
			
			
			<maxFileSize>300MBmaxFileSize>
		rollingPolicy>
		<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
			
			<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%npattern>
		encoder>
	appender>

	
	<appender name="HTML" class="ch.qos.logback.core.FileAppender">
		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
			
			<level>ERRORlevel>
		filter>
		<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
			<layout class="ch.qos.logback.classic.html.HTMLLayout">
				<pattern>%p%d%msg%M%F{32}%Lpattern>
			layout>
		encoder>
		<file>${LOG_HOME}/error-log.htmlfile>
	appender>
	

	
	
	

	
	<logger name="com.apache.ibatis" level="TRACE" />
	<logger name="java.sql.Connection" level="DEBUG" />
	<logger name="java.sql.Statement" level="DEBUG" />
	<logger name="java.sql.PreparedStatement" level="DEBUG" />

	
	<root level="INFO">
		<appender-ref ref="STDOUT" />
		<appender-ref ref="FILE" />
		
	root>

configuration>
代码中使用

增加类注解:

@Slf4j
public class TestClass {
	public TestClass() {
		log.info("日志");
	}
}

你可能感兴趣的:(Java随笔,java)