In the whole web system, in order to unify the use of log management, it is necessary to load the log4j configuration file when the system is started, in order to ensure that the format of the later use of log4j is consistent, easy to track and solve the problem.
So, how to load the log4j configuration file when the system starts up? Let me briefly explain:
1. Add a "listener" to the Web. xml file
XML code
- <!--load log4j configuration information --
- <listener >
- <listener-class>hb.init.log4j.Log4jInit</ listener-class >
- </listener >
<!--loading log4j configuration information- <listener> <listener-class>hb.init.log4j.log4jinit</ Listener-class> </listener>
2. "Listener Class" inherits "Servletcontextlistener" interface
Java code
- Package hb.init.log4j;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.util.Properties;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- import org.apache.log4j.Logger;
- import org.apache.log4j.PropertyConfigurator;
- public class Log4jinit implements servletcontextlistener{
- Logger log = Logger.getlogger (log4jinit. class );
- public void contextdestroyed (Servletcontextevent sce) {
- Log.info ("Log4jinit contextdestroyed!" );
- }
- public void contextinitialized (Servletcontextevent sce) {
- //How to get ServletContext object
- ServletContext sc = Sce.getservletcontext ();
- //indicates the relative path of the file to get the absolute path of the file
- System.out.println (Sc.getrealpath ("/"));
- String Path = Sc.getrealpath ("/config/log4j.properties");
- //Load the log configuration file when starting the server
- init (PATH,SC);
- Log.info ("log4j");
- }
- /**
- *
- * @param path of path configuration file
- * @param sc ServletContext object
- */
- public void init (String path,servletcontext SC) {
- FileInputStream istream = NULL ;
- Try{
- Properties props = New Properties ();
- //Load configuration file
- istream = New FileInputStream (path);
- Props.remove ("Log4j.appender.file.File");
- System.out.println (Sc.getrealpath ("/log/hb.log"));
- //indicate the location of the log file
- props.put ("log4j.appender.file.File", Sc.getrealpath ("/log/hb.log" ));
- //load file stream, load configuration file information for log4j file
- Props.load (IStream);
- propertyconfigurator.configure (props);
- } catch (Exception ex) {
- Try {
- throw new Exception (ex);
- } catch (Exception e) {
- e.printstacktrace ();
- }
- } finally{
- Try {
- istream.close ();
- } catch (IOException e) {
- e.printstacktrace ();
- }
- }
- }
- }
Package Hb.init.log4j;import Java.io.fileinputstream;import Java.io.ioexception;import java.util.Properties;import Javax.servlet.servletcontext;import Javax.servlet.servletcontextevent;import Javax.servlet.servletcontextlistener;import Org.apache.log4j.logger;import Org.apache.log4j.PropertyConfigurator ;p Ublic class Log4jinit implements Servletcontextlistener{logger log = Logger.getlogger (log4jinit.class);p ublic void Contextdestroyed (Servletcontextevent SCE) {log.info ("Log4jinit contextdestroyed!");} public void contextinitialized (Servletcontextevent sce) {//method to get ServletContext object ServletContext sc = Sce.getservletcontext ();//indicates the relative path of the file to obtain the absolute path of the file System.out.println (Sc.getrealpath ("/")); String Path = Sc.getrealpath ("/config/log4j.properties");//The configuration file that loads the log when starting the server init (PATH,SC); Log.info ("log4j");} /** * * @param path configuration file * @param sc ServletContext object */public void init (String path,servletcontext SC) {Fileinputstrea M istream = null;try{properties props = new Properties ();//load config file istream = NEW FileInputStream (Path);p rops.remove ("Log4j.appender.file.File"); System.out.println (Sc.getrealpath ("/log/hb.log"));//Indicates the location of the log file Props.put ("Log4j.appender.file.File", Sc.getrealpath ("/log/hb.log"));//load file stream, load configuration file information for log4j file Props.load (IStream); Propertyconfigurator.configure (props);} catch (Exception ex) {try {throw new Exception (ex);} catch (Exception e) {e.printstacktrace ();}} finally{try {Istream.clos E ();} catch (IOException e) {e.printstacktrace ();}}}}
The purpose of the
Load log4j configuration file is to keep the log file "in place relative to the engineering path" so that the display is normal even if the project is ported to a different operating system