Change the logback configuration file path in the java web project

Source: Internet
Author: User

Change the logback configuration file path in the java web project

My small Website: http://51kxd.com/welcome to visit when you are not happy, adjust the mood

 

Configuration in web. xml:

 



LogbackConfigLocation
File:/datum/Data/conf/config/logback. xml

 



LogbackConfigLocation
File: E: \ datum \ data \ conf \ logback. xml
 

 

File: Yes.

 


Com. cxfmvcstu. comn. LogbackConfigListener
 

LogbackConfigListener is your 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. joranaggregator; import ch. qos. logback. core. joran. spi. joranException;/*** @ description * @ path com. cxfmvcstu. comn. logbackConfigListener * @ author lijun. justin * @ time June 27, 2015 8:13:19 */public class LogbackConfigListener implements ServletContextListener {private static final Logger logger = LoggerFactory. getLogger (LogbackConfigListener. class); private static final String CONFIG_LOCATION = "logbackConfigLocation"; @ Overridepublic void contextInitialized (ServletContextEvent event) {// from the web. 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) ;}@overridepublic void contextDestroyed (ServletContextEvent event ){}}

It depends on the UrlResource class of spring. Useless code in it, and readers can streamline it themselves

 

 

Logback. xml configuration example:

 






% Date [% thread] %-5 level % logger {80}-% msg % n





Class = "ch. qos. logback. core. rolling. RollingFileAppender">

INFO
ACCEPT
DENY


D:/logs/INFO. % d {yyyy-MM-dd}. log
30


% Date [% thread] %-5 level % logger {80}-% msg % n







ERROR
ACCEPT
DENY


D:/logs/error. % d {yyyy-MM-dd}. log
30


% Date [% thread] %-5 level % logger {80}-% msg % n



















 

 

 

 

My small Website: http://51kxd.com/welcome to visit

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.