需要自定义一个Listener 来加载配置文件
package com.sj.common.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
/**
*
* @author longload
*
*/
public class LogbackConfigListener implements ServletContextListener {
private static final Logger logger = LoggerFactory.getLogger(LogbackConfigListener.class);
private static final String CONFIG_LOCATION = "logbackConfigLocation";
@Override
public void contextInitialized(ServletContextEvent event) {
//从web.xml中加载指定文件名的日志配置文件
String logbackConfigLocation = event.getServletContext().getInitParameter(CONFIG_LOCATION);
String fn = event.getServletContext().getRealPath(logbackConfigLocation);
try {
LoggerContext loggerContext = (LoggerContext)LoggerFactory.getILoggerFactory();
loggerContext.reset();
JoranConfigurator joranConfigurator = new JoranConfigurator();
joranConfigurator.setContext(loggerContext);
joranConfigurator.doConfigure(fn);
logger.debug("loaded slf4j configure file from {}", fn);
}
catch (JoranException e) {
logger.error("can loading slf4j configure file from " + fn, e);
}
}
@Override
public void contextDestroyed(ServletContextEvent event) {
}
}
在web.xml中加入
- <!--初始化日志配置文件 -->
- <listener>
- <listener-class>
- com.sj.common.listener.LogbackConfigListener
- </listener-class>
- </listener>
- <context-param>
- <param-name>logbackConfigLocation</param-name>
- <param-value>WEB-INF/logback.xml</param-value>
- </context-param>
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<jmxConfigurator />
<!-- 控制台输出日志 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<Encoding>UTF-8</Encoding>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %logger{36} -line: %L - %msg%n</pattern>
</layout>
</appender>
<!-- 文件输出日志 (文件大小策略进行文件输出,超过指定大小对文件备份)-->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<Encoding>UTF-8</Encoding>
<File>service_debug.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>service.log.%i.bak</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>12</MaxIndex>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -line: %L - %msg%n</Pattern>
</layout>
</appender>
<appender name="FILE-ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<OnMismatch>DENY</OnMismatch>
<OnMatch>ACCEPT</OnMatch>
</filter>
<File>service-err.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>service-err.log.%i.bak</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>3</MaxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -line: %L - %msg%n</Pattern>
</layout>
</appender>
<!--这里指定logger name 是为jmx设置日志级别做铺垫 -->
<logger name="com.sj">
<level value="DEBUG" />
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
<appender-ref ref="FILE-ERROR" />
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
<appender-ref ref="FILE-ERROR" />
</root>
</configuration>