关闭

java web工程中logback换配置文件的路径

标签: java
3473人阅读 评论(0) 收藏 举报
分类:

本人小网站:   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) {
	}
}

要依赖于spring的UrlResource类。里面无用的代码,读者自己精简一下


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> 



本人小网站:   http://51kxd.com/  欢迎大家访问
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:41854次
    • 积分:839
    • 等级:
    • 排名:千里之外
    • 原创:13篇
    • 转载:44篇
    • 译文:0篇
    • 评论:0条
    文章分类