标签:pattern ace bsp als max date java web servlet ssi
本人小站点: http://51kxd.com/ 欢迎大家不开心的时候訪问訪问,调节一下心情
web.xml中配置:
<!-- windows logback.xml文件跟web容器(比方tomcat)在同一个文件夹中。linux任意 -->
<context-param>
<param-name>logbackConfigLocation</param-name>
<param-value>file:/datum/Data/conf/config/logback.xml</param-value>
</context-param>
<!-- logback.xml任意放 -->
<context-param>
<param-name>logbackConfigLocation</param-name>
<param-value>file:E:\\datum\\data\\conf\\logback.xml</param-value>
</context-param>
当中file:一定要有。
<listener>
<listener-class>com.cxfmvcstu.comn.LogbackConfigListener</listener-class>
</listener>
LogbackConfigListener为自己的实现类
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.UrlResource; import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.joran.JoranConfigurator; import ch.qos.logback.core.joran.spi.JoranException; /** * @description * @path com.cxfmvcstu.comn.LogbackConfigListener * @author lijun.justin * @time 2015年6月27日 下午8:13:19 */ 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 { UrlResource urlResource = new UrlResource(logbackConfigLocation); LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); loggerContext.reset(); JoranConfigurator joranConfigurator = new JoranConfigurator(); joranConfigurator.setContext(loggerContext); //joranConfigurator.doConfigure(fn); joranConfigurator.doConfigure(urlResource.getFile().getAbsolutePath()); logger.debug("loaded slf4j configure file from {}", fn); } catch (JoranException e) { logger.error("can loading slf4j configure file from " + fn, e); }catch (Exception e) { logger.error(e.getMessage(), e); } } @Override public void contextDestroyed(ServletContextEvent event) { } }
logback.xml的配置演示样例:
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!-- 控制台输出 -->
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
</encoder>
</appender>
<!-- 时间滚动输出 level为 INFO 日志 -->
<appender name="file—INFO"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<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>D:/logs/INFO.%d{yyyy-MM-dd}.log</FileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder>
<pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
</encoder>
</appender>
<!-- 时间滚动输出 level为 ERROR 日志 -->
<appender name="file—error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY </onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>D:/logs/error.%d{yyyy-MM-dd}.log</FileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder>
<pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
</encoder>
</appender>
<!-- 特定过滤含有某字符串的日志 须要额外的两个JAR包,commons-compiler.jar和janino.jar
<appender name="file-str" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<level>ERROR</level>
<evaluator>
<expression>message.contains("str")</expression>
</evaluator>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>D:/logs/contains.%d{yyyy-MM-dd}.log
</FileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder>
<pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
</encoder>
</appender> -->
<!-- 数据库输出
<appender name="db" class="ch.qos.logback.classic.db.DBAppender">
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<level>ERROR</level>
<evaluator>
<expression>message.contains("str")</expression>
</evaluator>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<connectionSource
class="ch.qos.logback.core.db.DriverManagerConnectionSource">
<driverClass>com.mysql.jdbc.Driver</driverClass>
<url>jdbc:mysql://host_name:3306/datebase_name</url>
<user>username</user>
<password>password</password>
</connectionSource>
</appender> -->
<root level="INFO">
<appender-ref ref="stdout" />
<appender-ref ref="file—INFO" />
<appender-ref ref="file—error" />
<appender-ref ref="file-str" />
<appender-ref ref="db" />
</root>
</configuration>
java webproject中logback换配置文件的路径
标签:pattern ace bsp als max date java web servlet ssi
原文地址:http://www.cnblogs.com/mthoutai/p/7056404.html