Slf4j日志集成

Slf4j日志集成

下面就是集成步骤,按着做就可以了

1、logback-spring.xml

哪个服务需要记录日志就将在哪个服务的resource下新建logback-spring.xml文件,里面的内容如下:







<configuration scan="true" scanPeriod="60 seconds" debug="false">
	
	<jmxConfigurator />
	
	<property name="log_dir" value="D:/LOGS/HRM" />
	
	<property name="maxHistory" value="30" />
	
	<property name="maxFileSize" value="20MB" />
	
	<property name="FORMAT" value="%-5level %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c.%M:%L:%msg%n"/>


	
	<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>
				
				${FORMAT}
			pattern>
		encoder>
	appender>

	
	
	<appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
		
		
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			
			<level>ERRORlevel>
			
			<onMatch>ACCEPTonMatch>
			
			<onMismatch>DENYonMismatch>
		filter>
		
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			
			<fileNamePattern>
				${log_dir}/error/%d{yyyy-MM-dd}/error-%i.log
			fileNamePattern>
			
			<maxHistory>${maxHistory}maxHistory>
			
			<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>${maxFileSize}maxFileSize>
			timeBasedFileNamingAndTriggeringPolicy>
		rollingPolicy>
		<encoder>
			<pattern>
				
				${FORMAT}
			pattern>
		encoder>
	appender>

	
	<appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
		
		
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			
			<level>WARNlevel>
			
			<onMatch>ACCEPTonMatch>
			
			<onMismatch>DENYonMismatch>
		filter>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${log_dir}/warn/%d{yyyy-MM-dd}/warn-%i.logfileNamePattern>
			<maxHistory>${maxHistory}maxHistory>
			
			<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>${maxFileSize}maxFileSize>
			timeBasedFileNamingAndTriggeringPolicy>
		rollingPolicy>
		<encoder>
			<pattern>${FORMAT}pattern>
		encoder>
	appender>

	
	<appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>INFOlevel>
			<onMatch>ACCEPTonMatch>
			<onMismatch>DENYonMismatch>
		filter>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${log_dir}/info/%d{yyyy-MM-dd}/info-%i.logfileNamePattern>
			<maxHistory>${maxHistory}maxHistory>
			
			<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>${maxFileSize}maxFileSize>
			timeBasedFileNamingAndTriggeringPolicy>
		rollingPolicy>
		<encoder>
			<pattern>${FORMAT}pattern>
		encoder>
	appender>

	
	<appender name="DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>DEBUGlevel>
			<onMatch>ACCEPTonMatch>
			<onMismatch>DENYonMismatch>
		filter>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${log_dir}/debug/%d{yyyy-MM-dd}/debug-%i.logfileNamePattern>
			<maxHistory>${maxHistory}maxHistory>
			
			<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>${maxFileSize}maxFileSize>
			timeBasedFileNamingAndTriggeringPolicy>
		rollingPolicy>
		<encoder>
			<pattern>${FORMAT}pattern>
		encoder>
	appender>

	
	<root>
		
		
		<level value="info" />
		
		<appender-ref ref="console" />
		
		<appender-ref ref="ERROR" />
		<appender-ref ref="INFO" />
		<appender-ref ref="WARN" />
		<appender-ref ref="DEBUG" />
	root>
configuration>

上面内容基本可以当作模板使用了,工作后可以直接使用,里面的注释写的非常清楚了,可根据具体需求修改一些参数就可以使用了

2、导入依赖

哪个微服务需要记录日志就将在哪个微服务的pom.xml导入下面的包:

<dependency>
    <groupId>org.projectlombokgroupId>
    <artifactId>lombokartifactId>
dependency>

3、使用

@Slf4j
public class TenantController {
    
    @GetMapping(value = "/list")
    public JSONResult list(){
        log.debug("我是 debug 信息");
        log.info("我是 info 信息");
        log.warn("我是 warn 信息");
        log.error("我是 error 信息");
        return JSONResult.success();
    }
}

4、测试

直接浏览器访问一下list接口,控制台就会打印出日志了:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GpX11ixY-1689515913874)(images/image-20211111195522771.png)]

对应磁盘中会生成对应的日志文件:

Slf4j日志集成_第1张图片

你可能感兴趣的:(SpringBoot,log4j)