Java Log component Logback use: Load a configuration file under a non-classpath and set a scheduled update

Source: Internet
Author: User
Tags getmessage


Java Log component Logback use: Load a configuration file under a non-classpath and set a scheduled update

Excerpt: 78874499

December 22, 2017 16:20:29Hits: 868 Tags: javalogback log configuration file Logback-xm more Personal Category:Java Log Copyright Notice: This article for Bo Master original article, without Bo Master permission not reproduced. 78874499logback load the Logback configuration file under the non-classpath and set the timing update to reload the Logback profile periodically

Logback.xml

< Configuration  scan  =  scanperiod  = "30 Seconds ";  .... </configuration ;  
  
 
    • 1
    • 2
    • 3

Configuration Description:
-Scan: Set to True to reload the log configuration file for a specified period of time
-Scanperiod: When scan=true, the log profile is reloaded within the specified unit time, and by default it is reloaded once per minute.
-Configuration instructions for Scanperiod:
Time unit: Milliseconds, seconds, minutes, hours

eg:

 5 min: < configuration  scan  = "true"  scanperiod  = "5 minutes" ;  1 hrs: <configuration  scan  =" true " scanperiod  =" 1 hours ";  
  
 
    • 1
    • 2
The following code is called in the Main method of loading the Logback.xml configuration file under the non-class path:
path name of the//logback.xmlFile File =NewFile (System.getproperty ("User.dir") +"/conf/logback.xml"); Loggercontext Loggercontext = (loggercontext) loggerfactory.getiloggerfactory (); Joranconfigurator Joranconfigurator =NewJoranconfigurator (); Joranconfigurator.setcontext (Loggercontext); Loggercontext.reset ();Try{joranconfigurator.doconfigure (file);}Catch(Exception e) {System.out.println (String.Format ("Load logback config file error. Message: ", E.getmessage ())); } statusprinter.printincaseoferrorsorwarnings (Loggercontext);
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
The stripping code is a class:
 ImportCh.qos.logback.classic.LoggerContext;ImportCh.qos.logback.classic.joran.JoranConfigurator;ImportCh.qos.logback.core.util.StatusPrinter;ImportOrg.slf4j.LoggerFactory;ImportJava.io.File;/** * Created by XUYH at 2017/11/20 14:32. */  Public  class logbackinit {     /** * Set Logback.xml profile and load * * @param configfilepathname Profile path name */      Public Static void Initlogback(String configfilepathname) {File File =NewFile (Configfilepathname);         Loggercontext Loggercontext = (loggercontext) loggerfactory.getiloggerfactory (); Joranconfigurator Joranconfigurator =NewJoranconfigurator ();         Joranconfigurator.setcontext (Loggercontext); Loggercontext.reset ();Try{joranconfigurator.doconfigure (file); }Catch(Exception e) {System.out.println (String.Format ("Load logback config file error. Message: ", E.getmessage ()));     } statusprinter.printincaseoferrorsorwarnings (Loggercontext); } }
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21st
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
Call
//初始化logback日志配置文件 LogbackInit.initLogback(System.getProperty("user.dir""/conf/logback.xml");

Java Log component Logback use: Load a configuration file under a non-classpath and set a scheduled update

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.