Java logback manually loads the configuration file

Source: Internet
Author: User

Not to mention the code: There are two java files in total. The first is the example, the second is the configuration file loading class, And LogbackTest. java/** To change this template, choose Tools | Templates * and open the template in the editor. */package logbacktest; import ch. qos. logback. core. joran. spi. joranException; import java. io. IOException; import org. slf4j. loggerFactory;/***** @ author Administrator */public class LogbackTest {/***** @ param args the command line arguments */publi C static void main (String [] args) throws IOException, JoranException {LogBackConfigLoader. load ("logback-log.xml"); org. slf4j. logger logger = LoggerFactory. getLogger ("snail Il"); logger. debug ("Hello") ;}} LogBackConfigLoader. java/** To change this template, choose Tools | Templates * and open the template in the editor. */package logbacktest; import java. io. file; import java. io. IOException; import Org. slf4j. logger; import org. slf4j. loggerFactory; import ch. qos. logback. classic. loggerContext; import ch. qos. logback. classic. joran. joranaggregator; import ch. qos. logback. core. joran. spi. joranException; import ch. qos. logback. core. util. statusPrinter;/*** Simple Utility class for loading an external config file for logback * @ author daniel */public class LogBackConfigLoader {public static void Load (String externalConfigFileLocation) throws IOException, JoranException {LoggerContext lc = (LoggerContext) LoggerFactory. getILoggerFactory (); File externalConfigFile = new File (externalConfigFileLocation); if (! ExternalConfigFile. exists () {throw new IOException ("Logback External Config File Parameter does not reference a file that exists");} else {if (! ExternalConfigFile. isFile () {throw new IOException ("Logback External Config File Parameter exists, but does not reference a file");} else {if (! ExternalConfigFile. canRead () {throw new IOException ("Logback External Config File exists and is a file, but cannot be read. ");} else {JoranConfigurator aggregator = new JoranConfigurator (); aggregator. setContext (lc); lc. reset (); aggregator. doConfigure (externalConfigFileLocation); StatusPrinter. printInCaseOfErrorsOrWarnings (lc) ;}}} attaches a simple logback-log.xml <? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <appender name = "STDOUT" class = "ch. qos. logback. core. ConsoleAppender"> <! -- Encoder is configured as PatternLayoutEncoder by default --> <encoder> <pattern> [%-5 level] % d {yyyy-MM-dd HH: mm: ss }%msg % n </pattern> </encoder> </appender> www.2cto.com <appender name = "debug" class = "ch. qos. logback. core. fileAppender "> <File> log/debug. log </File> <Append> true </Append> <encoder> <pattern> [%-5 level] % d {yyyy-MM-dd HH: mm: ss }%msg % n </pattern> </encoder> <filter class = "ch. qos. logback. classic. filter. thresholdFilter "> <level> TRACE </level> </filter> </appender> <logger name =" snil "level =" TRACE "additi.pdf =" false "> <appender- ref = "debug"/> </logger> </configuration>

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.