How the Java Web project loads the log4j configuration file

Source: Internet
Author: User
Tags log4j

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
    1. <!--load log4j configuration information --
    2. <listener >
    3. <listener-class>hb.init.log4j.Log4jInit</ listener-class >
    4. </listener >
<!--loading log4j configuration information-  <listener>  <listener-class>hb.init.log4j.log4jinit</ Listener-class>  </listener>

2. "Listener Class" inherits "Servletcontextlistener" interface

Java code
  1. Package hb.init.log4j;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.util.Properties;
  5. import javax.servlet.ServletContext;
  6. import javax.servlet.ServletContextEvent;
  7. import javax.servlet.ServletContextListener;
  8. import org.apache.log4j.Logger;
  9. import org.apache.log4j.PropertyConfigurator;
  10. public class Log4jinit implements servletcontextlistener{
  11. Logger log = Logger.getlogger (log4jinit. class );
  12. public void contextdestroyed (Servletcontextevent sce) {
  13. Log.info ("Log4jinit contextdestroyed!" );
  14. }
  15. public void contextinitialized (Servletcontextevent sce) {
  16. //How to get ServletContext object
  17. ServletContext sc = Sce.getservletcontext ();
  18. //indicates the relative path of the file to get the absolute path of the file
  19. System.out.println (Sc.getrealpath ("/"));
  20. String Path = Sc.getrealpath ("/config/log4j.properties");
  21. //Load the log configuration file when starting the server
  22. init (PATH,SC);
  23. Log.info ("log4j");
  24. }
  25. /**
  26. *
  27. * @param path of path configuration file
  28. * @param sc ServletContext object
  29. */
  30. public void init (String path,servletcontext SC) {
  31. FileInputStream istream = NULL ;
  32. Try{
  33. Properties props = New Properties ();
  34. //Load configuration file
  35. istream = New FileInputStream (path);
  36. Props.remove ("Log4j.appender.file.File");
  37. System.out.println (Sc.getrealpath ("/log/hb.log"));
  38. //indicate the location of the log file
  39. props.put ("log4j.appender.file.File", Sc.getrealpath ("/log/hb.log" ));
  40. //load file stream, load configuration file information for log4j file
  41. Props.load (IStream);
  42. propertyconfigurator.configure (props);
  43. } catch (Exception ex) {
  44. Try {
  45. throw new Exception (ex);
  46. } catch (Exception e) {
  47. e.printstacktrace ();
  48. }
  49. } finally{
  50. Try {
  51. istream.close ();
  52. } catch (IOException e) {
  53. e.printstacktrace ();
  54. }
  55. }
  56. }
  57. }
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

Related Article

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.