How do I load the log4j configuration file when the system starts?
1. Customize the listener class and inherit the "Servletcontextlistener" interface:
1 PackageCn.ibeans.common;2 3 ImportJava.io.File;4 Importjava.util.List;5 6 Importjavax.servlet.ServletContextEvent;7 ImportJavax.servlet.ServletContextListener;8 9 ImportOrg.apache.log4j.Logger;Ten ImportOrg.apache.log4j.PropertyConfigurator; One ImportCn.ibeans.common.util.FileUtil; A /** - * - * @authorHezuoan the * - */ - Public classApplicationlistenerImplementsServletcontextlistener { - + Private StaticLogger log = Logger.getlogger (Applicationlistener.class); - @Override + Public voidcontextinitialized (Servletcontextevent sce) { A at //get the address of the log4j configuration file -String Log4jpath = Applicationdispatchservlet.class. GetResource ("/"). GetPath () + "Config/logs"; -list<file> files = Fileutil.listfile (NewFile (Log4jpath), "Properties",false); - - if(Files =NULL) || (files.size () = = 0)){ -Log.info ("No log4j configuration file found.")); in return; - } to for(File file:files) { + //Load configuration file - propertyconfigurator.configure (File.getpath ()); the } *Log.info ("Load log4j configuration file complete.")); $ }Panax Notoginseng - @Override the Public voidcontextdestroyed (Servletcontextevent sce) {} + A}
The Fileutil.listfile () method in the preceding code is a tool class that is encapsulated by itself.
1 /**2 * Get all the files under the folder3 * @paramdir Folder path4 * @paramfilename suffix name5 * @paramrecursive whether to recursively sub-folders under this folder6 * @return 7 */8 Public StaticList<file> listfile (File dir,FinalString filename,Booleanrecursive)9 {Ten if(!dir.exists ()) { One Throw NewIllegalArgumentException ("directory:" + dir + "does not exist"); A } - if(!dir.isdirectory ()) { - Throw NewIllegalArgumentException (dir + "not directory"); the } -FileFilter FF =NULL; - if(FileName = =NULL) || (filename.length () = = 0)) { -FF =NewFileFilter () + { - Public BooleanAccept (File pathname) + { A return true; at } - }; -}Else { -FF =NewFileFilter () - { - Public BooleanAccept (File pathname) in { - if(Pathname.isdirectory ()) { to return true; + } -String name =pathname.getname (); the if(Name.indexof (filename)! =-1) { * return true; $ }Panax Notoginseng return false; - } the }; + } A returnlistfile (dir, FF, recursive); the } + - Private StaticList<file> listfile (File dir, FileFilter FF,Booleanrecursive) $ { $list<file> list =NewArraylist<file>(); -File[] Subs =dir.listfiles (FF); - if((Subs! =NULL) && (subs.length > 0)) { the for(File sub:subs) { - if(Sub.isfile ()) {Wuyi List.add (sub); the}Else if(recursive) { -List.addall (ListFile (Sub, FF,true)); Wu } - } About } $ returnlist; -}
View Code
2. Add the listener's configuration to Web. xml:
< Listener > < Listener-class >cn.ibeans.common.ApplicationListener</listener-class> </listener>
The purpose of loading the log4j configuration file is to keep the log file "in place relative to the project" so that the display is normal even if the project is ported to a different operating system.
Load the log4j configuration file when the Web system starts