log4j、logback汇总

log4j 参考 配置Log4j(很详细) 和 Log4j按级别输出到不同文件

主要是重写 DailyRollingFileAppender 


logback配置参考

<?xml version="1.0" encoding="UTF-8"?>

<configuration debug="true">

	<property resource="application.properties" />

	<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
		<encoder charset="UTF-8">
			<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
		</encoder>
	</appender>

	<appender name="FILE-ERROR"	class="ch.qos.logback.core.rolling.RollingFileAppender">
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${log.root.folder}/error.log.%d{yyyy-MM-dd}</fileNamePattern>
			<maxHistory>15</maxHistory>
		</rollingPolicy>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>ERROR</level> 
      		<onMatch>ACCEPT</onMatch>
      		<onMismatch>DENY</onMismatch>
		</filter>
		<encoder>
			<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
		</encoder>
	</appender>

	<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${log.root.folder}/info.log.%d{yyyy-MM-dd}</fileNamePattern>
			<maxHistory>15</maxHistory>
		</rollingPolicy>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>INFO</level> 
      		<onMatch>ACCEPT</onMatch>
      		<onMismatch>DENY</onMismatch>
		</filter>
		<encoder>
			<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
		</encoder>
	</appender>
	    
	<root level="INFO">
		<appender-ref ref="STDOUT" />
		<appender-ref ref="FILE" />
		<appender-ref ref="FILE-ERROR" />
	</root>

</configuration>


*********** 20161102 补充 ***********

logback提供了3种filter:LevelFilter、ThresholdFilter和EvalutorFilter。同时支持自定义filter。

比如为了方便日志调阅,我将项目包下面的日志独立出来。

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;

/**
 * Description:
 * Auth:Paris
 * Date:Nov 2, 2016
**/
public class ProjectFilter extends Filter<ILoggingEvent>{

	@Override
	public FilterReply decide(ILoggingEvent event) {
		if (event.getLoggerName().equals("xxx")){
			return FilterReply.ACCEPT;
		}else {
			return FilterReply.DENY;
		}
	}

}
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;

/**
 * Description:
 * Auth:Paris
 * Date:Nov 2, 2016
**/
public class ProjectDenyFilter extends Filter<ILoggingEvent>{

	@Override
	public FilterReply decide(ILoggingEvent event) {
		if (event.getLoggerName().equals("xxx")){
			return FilterReply.DENY;
		}else {
			return FilterReply.ACCEPT;
		}
	}

}

logger配置如下

<?xml version="1.0" encoding="UTF-8"?>

<configuration debug="true">

	<property resource="application.properties" />

	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<encoder charset="UTF-8">
			<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n
			</pattern>
		</encoder>
	</appender>

	<appender name="FILE-DEBUG"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${log.root.folder}/debug.log.%d{yyyy-MM-dd}
			</fileNamePattern>
			<maxHistory>15</maxHistory>
		</rollingPolicy>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>DEBUG</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
		<encoder>
			<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n
			</pattern>
		</encoder>
	</appender>

	<appender name="FILE-ERROR"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${log.root.folder}/error.log.%d{yyyy-MM-dd}
			</fileNamePattern>
			<maxHistory>15</maxHistory>
		</rollingPolicy>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>ERROR</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
		<encoder>
			<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n
			</pattern>
		</encoder>
	</appender>

	<appender name="FILE"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${log.root.folder}/info.log.%d{yyyy-MM-dd}
			</fileNamePattern>
			<maxHistory>15</maxHistory>
		</rollingPolicy>
		<filter
			class="xxx.ProjectDenyFilter" />
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>INFO</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
		<encoder>
			<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n
			</pattern>
		</encoder>
	</appender>

	<appender name="PROJECT_IFLE"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<filter
			class="xxx.ProjectFilter" />
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>INFO</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<FileNamePattern>${log.root.folder}/dispatcher.log.%d{yyyy-MM-dd}
			</FileNamePattern>
			<MaxHistory>30</MaxHistory>
		</rollingPolicy>
		<encoder>
			<pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
		</encoder>
	</appender>

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

	<logger name="xxx" level="INFO">
		<appender-ref ref="PROJECT_IFLE" />
	</logger>
</configuration>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值