Java Applications (non-Web applications) log4j.properties dynamically modify the configuration file, no need to restart, can take effect immediately, how to achieve.
Main use: Propertyconfigurator.configureandwatch (path,1000),//interval specific time, detect files are modified, automatically re-read configuration
To implement automatic overloading of log4j configuration files after modification.
Log4jconfig.java
Package log4jautoload; Import Org.apache.log4j.PropertyConfigurator; public class Log4jconfig {private static Boolean isreload = true;/** * Load log4j config file * @author ZHAIGX * @DATE 2011-5-28 * * public static void Load () {String path = Log4jConfig.class.getClass (). GetResource ("/"). GetPath () + "log4j.properties"; String path= "Config/log4j.properties"; System.out.println ("log4j configfile path=" + path); Propertyconfigurator.configureandwatch (path,1000);//interval specific time, detect file modification, automatically re-read configuration} private static void reload () {if ( Isreload) {load ();} isreload = false; public void Setreload (Boolean flag) {isreload = flag;}}
Logger.java
* * Created on 2005-6-22 * * TODO to change the template for this generated file go to * Window-preferences-java-co De style-code Templates */package log4jautoload; Import Org.apache.commons.logging.Log; Import Org.apache.commons.logging.LogFactory; /** * @author Administrator * TODO To change the template for this generated type comment go to * window-preferences- Java-code Style-code Templates/public class Logger {private Log log = null; static{log4jconfig.load ();//Load log4j match File} private Logger () {log = Logfactory.getlog (This.getclass ());} private Logger (Class c) {log = Logfactory.getlog (c); Private Logger (String className) {log = Logfactory.getlog (className);} public static Logger GetLogger () {return new Logger (); public static Logger GetLogger (Class c) {return new Logger (c);} public static Logger GetLogger (String className) {RET Urn New Logger (className); public void Trace (String info) {if (log.istraceenabled ()) Log.trace (info);} public void deBug (string info) {if (log.isdebugenabled ()) Log.debug (info);} public void info (string info) {if (log.isinfoenabled ()) L Og.info (info); public void warn (string info) {if (log.iswarnenabled ()) Log.warn (info),} public void error (string info) {if (log.iser Rorenabled ()) Log.error (info); public void fatal (String info) {if (log.isfatalenabled ()) Log.fatal (info), public boolean istraceenabled () {return L Og.istraceenabled (); public Boolean isdebugenabled () {return log.isdebugenabled ():} public boolean isinfoenabled () {return log.isinfoenabl Ed (); public Boolean iswarnenabled () {return log.iswarnenabled ():} public boolean iserrorenabled () {return log.iserrorenabl Ed (); public Boolean isfatalenabled () {return log.isfatalenabled ();}}
Test class:
Package log4jautoload; public class Betestclass {static Logger Log=logger.getlogger (Betestclass.class), public static void main (string[] args) { for (int i = 0; i < 1000 i++) {log.info ("----------info"); Log.debug ("----------Debug"); Log.error ("----------Error" ); System.out.println ("***********************"); try {thread.sleep;} catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();}} } }
Log_home=.. /log Log4j.rootlogger=info,console Log4j.appender.console=org.apache.log4j.consoleappender Log4j.appender.console.layout=org.apache.log4j.patternlayout log4j.appender.console.layout.conversionpattern= [% D{YYYYMMDD-HH:MM:SS}-%P][%F:%L]%m%n Log4j.appender.dailyfile=org.apache.log4j.dailyrollingfileappender Log4j.appender.DailyFile.Threshold = info Log4j.appender.dailyfile.file=${log_home}/test.log LOG4J.APPENDER.DAILYFILE.ENCODING=GBK log4j.appender.dailyfile.append=true log4j.appender.dailyfile.layout= Org.apache.log4j.PatternLayout log4j.appender.dailyfile.layout.conversionpattern=[%d{yyyymmdd-hh:mm:ss}-%p][%f: %L]%m%n
Test:
Run the test class and then modify the Log4j.properties log4j.rootlogger=info,console
Change info to error or debug, and then view the output effect.