slf4j相关的配置信息

1.  首先准备相关jar包,如下:

     slf4j-api-1.7.2.jar,logback-classic-1.0.9.jar,logback-core-1.0.9.jar


2.  在src 文件路径下,存放logback.xml文件,具体内容如下:

    

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
	<!-- 应用名称 -->
	<property name="APP_NAME" value="logtest" />
	<!--日志文件的保存路径,首先查找系统属性-Dlog.dir,如果存在就使用其;否则,在当前目录下创建名为logs目录做日志存放的目录 -->
	<property name="LOG_HOME" value="${log.dir:-logs}/${APP_NAME}" />
	<!-- 日志输出格式 -->
	<property name="ENCODER_PATTERN"
		value="%d{yyyy-MM-dd  HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n" />
	<contextName>${APP_NAME}</contextName>

	<!-- 控制台日志:输出全部日志到控制台 -->
	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
			<Pattern>${ENCODER_PATTERN}</Pattern>
		</encoder>
	</appender>

	<!-- 文件日志:输出全部日志到文件 -->
	<appender name="FILE"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${LOG_HOME}/output.%d{yyyy-MM-dd}.log</fileNamePattern>
			<maxHistory>7</maxHistory>
		</rollingPolicy>
		<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
			<pattern>${ENCODER_PATTERN}</pattern>
		</encoder>
	</appender>

	<!-- 错误日志:用于将错误日志输出到独立文件 -->
	<appender name="ERROR_FILE"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${LOG_HOME}/error.%d{yyyy-MM-dd}.log</fileNamePattern>
			<maxHistory>7</maxHistory>
		</rollingPolicy>
		<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
			<pattern>${ENCODER_PATTERN}</pattern>
		</encoder>
		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
			<level>WARN</level>
		</filter>
	</appender>
	
	<!-- 独立输出的同步日志 -->
	<appender name="SYNC_FILE"  class="ch.qos.logback.core.rolling.RollingFileAppender">
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${LOG_HOME}/sync.%d{yyyy-MM-dd}.log</fileNamePattern>
			<maxHistory>7</maxHistory>
		</rollingPolicy>
		<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
			<pattern>${ENCODER_PATTERN}</pattern>
		</encoder>
	</appender>
	
	<logger name="log.sync" level="DEBUG" addtivity="true">
		<appender-ref ref="SYNC_FILE" />
	</logger>
	
	<root>
		<level value="DEBUG" />
		<appender-ref ref="STDOUT" />
		<appender-ref ref="FILE" />
		<appender-ref ref="ERROR_FILE" />
	</root>
</configuration>

3.  测试日志代码即可。

package test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class Slf4jTest {

//	private final static Logger log = LoggerFactory.getLogger(Slf4jTest.class);

	private final static Logger log1 = LoggerFactory.getLogger("log.sync");

	
	public static void main(String[] args) {
		
		try {
			log1.error("this is an error test.");
			log1.info("{} is {}.",new Object[]{"a","b"});
			System.out.println("OK.");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}





你可能感兴趣的:(slf4j相关的配置信息)