欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

slf4j 日志配置文件

程序员文章站 2022-07-03 15:22:00
...
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan ="true" scanPeriod = "60 seconds" debug = "false">
	<!--  定义参数常量 -->
	<!-- TRACE < DEBUG < INFO < WARN < ERROR -->
	<!-- logger.trace("msg") -->
	<property name = "log.level" value= "debug"></property>
	<property name = "log.maxHistory" value= "30"></property>
	<property name = "log.filePath" value= "${catalina.base}/logs/webapps"/>
	<property name = "log.pattern" value= "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{55} -
				%msg%n"></property>
	<!-- 控制台设置  -->
	<appender name = "consoleAppender" class = "ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>${log.pattern}</pattern>
		</encoder>
	</appender>
	<!-- DEBUG -->
	<appender name = "debugAppender" class ="ch.qos.logback.core.rolling.RollingFileAppender">
		<!-- 文件路径  -->
		<file> ${log.filePath}/debug.log</file>
		<rollingPolicy class = "ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<!-- 文件名称 -->
			<fileNamePattern>${log.filePath}/debug/debug.%d{yyy-MM-dd}.log.gz
			</fileNamePattern>
			<!-- 文件最大保存历史数量 -->
			<maxHistory>${log.maxHistory}</maxHistory>
		</rollingPolicy>
		<encoder>
			<pattern>${log.pattern}</pattern>
		</encoder>
		<filter class = "ch.qos.logback.classic.filter.LevelFilter">
			<level>DEBUG</level>
			<onMatch>ACCEPT</onMatch>
			<onMatch>DENY</onMatch>
		</filter>
	</appender>
	
	<!-- INFO -->
	<appender name = "infoAppender" class ="ch.qos.logback.core.rolling.RollingFileAppender">
		<!-- 文件路径  -->
		<file> ${log.filePath}/info.log</file>
		<rollingPolicy class = "ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<!-- 文件名称 -->
			<fileNamePattern>${log.filePath}/info/info.%d{yyy-MM-dd}.log.gz
			</fileNamePattern>
			<!-- 文件最大保存历史数量 -->
			<maxHistory>${log.maxHistory}</maxHistory>
		</rollingPolicy>
		<encoder>
			<pattern>${log.pattern}</pattern>
		</encoder>
		<filter class = "ch.qos.logback.classic.filter.LevelFilter">
			<level>INFO</level>
			<onMatch>ACCEPT</onMatch>
			<onMatch>DENY</onMatch>
		</filter>
	</appender>
	
	<!-- ERROR -->
	<appender name = "errorAppender" class ="ch.qos.logback.core.rolling.RollingFileAppender">
		<!-- 文件路径  -->
		<file> ${log.filePath}/error.log</file>
		<rollingPolicy class = "ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<!-- 文件名称 -->
			<fileNamePattern>${log.filePath}/error/error.%d{yyy-MM-dd}.log.gz
			</fileNamePattern>
			<!-- 文件最大保存历史数量 -->
			<maxHistory>${log.maxHistory}</maxHistory>
		</rollingPolicy>
		<encoder>
			<pattern>${log.pattern}</pattern>
		</encoder>
		<filter class = "ch.qos.logback.classic.filter.LevelFilter">
			<level>ERROR</level>
			<onMatch>ACCEPT</onMatch>
			<onMatch>DENY</onMatch>
		</filter>
	</appender>
	<logger name = "com.test.myo2o" level = "${log.level}" additivity = "true">
		<appender-ref ref = "debugAppender"></appender-ref>
		<appender-ref ref = "infoAppender"></appender-ref>
		<appender-ref ref = "errorAppender"></appender-ref>
	</logger>
	<root level = "info">
		<appender-ref ref = "consoleAppender"></appender-ref>
	</root>
</configuration>

使用

private String listArea(){
	 logger.info("=====start");
	 long startTime = System.currentTimeMillis();
	 
	 logger.error("Test error");
	 long endTime = System.currentTimeMillis();
	 logger.debug("=====costTime:[{}ms]",endTime-startTime);
	 logger.info("=====end====");
	 return "hello sl4j";
	}
相关标签: slf4j