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 ;
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 ";
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