Path to logback config file in Java Web project

Source: Internet
Author: User

I small website: http://51kxd.com/Welcome everyone not happy when visit, adjust the mood


Configuration in Web. xml:


<!--windows Logback.xml files are in the same directory as Web containers (such as Tomcat). Linux Random--
<context-param>
<param-name>logbackConfigLocation</param-name>
<param-value>file:/datum/Data/conf/config/logback.xml</param-value>
</context-param>


<!--logback.xml free-
<context-param>
<param-name>logbackConfigLocation</param-name>
<param-value>file:E:\\datum\\data\\conf\\logback.xml</param-value>
</context-param>


Where file: Be sure to have.


<listener>
<listener-class>com.cxfmvcstu.comn.LogbackConfigListener</listener-class>
</listener>

Logbackconfiglistener for their own implementation class

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 June 27, 2015 PM 8:13:19 */public Class Logbackconfiglistener implements Servletcontextlistener { private static final Logger Logger = Loggerfactory.getlogger (logbackconfiglistener.class);p rivate static final String Config_location = "Logbackconfiglocation"; @Overridepublic void Contextinitialized (Servletcontextevent event) {// Load the log profile for the specified file name from web. XML 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);}} @Overridepublic void Contextdestroyed (Servletcontextevent event) {}}

To rely on spring's Urlresource class. The useless code inside, the reader to streamline their own


Example of Logback.xml configuration:


<?xml version= "1.0" encoding= "UTF-8"?>
<configuration scan= "true" scanperiod= "seconds" debug= "false" >
<!--console output--
<appender name= "stdout" class= "Ch.qos.logback.core.ConsoleAppender" >
<encoder>
<pattern>%date [%thread]%-5level%logger{80}-%msg%n</pattern>
</encoder>
</appender>


<!--time scrolling output level is INFO log--
<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>


<!--time scrolling output level is ERROR log--
<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>


<!--a specific filter containing a string of logs requires an additional two jar packages, Commons-compiler.jar and 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>


<!--database Output
<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>



I small website: http://51kxd.com/welcome everyone to visit

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Path to logback config file in Java Web project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.