Initialization of log4j

Source: Internet
Author: User
1. Initialization under Tomcat

The default log4j initialization typical application is in the Web-server environment. Under tomcat3.x and tomcat4.x, you should place the configuration file log4j.properties in the web-inf/classes directory of your Web application.
LOG4J will discover and initialize the property file. This is the easiest way to make it work.
You can also choose to set the System Properties Log4j.configuration before running Tomcat. For TOMCAT 3.x,tomcat_opts System variables are the options used to set the command line. For tomcat4.0, the tomcat_opts is replaced by the system environment variable catalina_opts.

UNIX command Line
export tomcat_opts= "-dlog4j.configuration=foobar.txt"


Tell log4j to use the file Foobar.txt as the default profile. This file should be placed in the Web-inf/classes directory. This file will be read by Propertyconfigurator. Each web-application will use a different default profile, because each file is associated with its web-application.export tomcat_opts= "-dlog4j.debug-dlog4j.configuration=foobar.xml" Export tomcat_opts= "-dlog4j.debug- Dlog4j.configuration=foobar.xml "
Tells log4j the debug information for the output log4j-internal and uses Foobar.xml as the default configuration file. This file should be placed in the web-inf/classes directory of your web-application. Because there is an extension of. xml, it will be read by Domconfigurator. Each web-application will use a different default profile. Because each file is related to the web-application it is in.Set TOMCAT_OPTS=-DLOG4J.CONFIGURATION=FOOBAR.LCF
-dlog4j.configuratorclass=com.foo.barconfigurator

Tell log4j to use the file FOOBAR.LCF as the default profile. This file should be placed in the web-inf/classes directory of your web-application. Because the Log4j.configuratorclass system attribute is defined, the file is parsed with a custom com.foo.barconfigurator class. Each web-application will use a different default profile. Because each file is related to the web-application it is in.Set TOMCAT_OPTS=-DLOG4J.CONFIGURATION=FILE:/C:/FOOBAR.LCF Set tomcat_opts=-dlog4j.configuration=file:/c:/ FOOBAR.LCF
Tell log4j to use the file FOOBAR.LCF as the default profile. This configuration file defines the full pathname with the URL file:/c:/foobar.lcf. This same configuration file will be used by all web-application.

    different web-application will load log4j through their own class loaders. In this way, each log4j environment will operate independently, without any synchronization of each other. For example, fileappenders that has exactly the same output source defined in multiple web-application will attempt to write the same file. The result seems to be a lack of security. You must make sure that the same system resources are not used for each of the different web-application log4j configurations.
2.  The Servlet initialization

It is also possible to do log4j initialization with a special servlet. The following is an example: Java code public class log4jinit extends httpservlet {         public void init ()  {              string prefix = getservletcontext (). Getrealpath ("/");              string file = getinitparameter ("Log4j-init-file ");             if (file != null)  {                  Propertyconfigurator.configure (prefix+file);              }         }              public void doget (httpservletrequest req, httpservletresponse res) &nbsP {          }  }  

public class Log4jinit extends HttpServlet {public      void init () {           String prefix = Getservletcontext (). Getrealpath ("/");           String file = Getinitparameter ("Log4j-init-file");           if (file!= null) {              propertyconfigurator.configure (prefix+file);           }       }         public void Doget (HttpServletRequest req, httpservletresponse res) {        }}

Define the subsequent servlet in the Web.xml for your web-application. XML code <servlet> <servlet-name>Log4j-init</servlet-name> <servlet-class>xx.xx.log4jinit </servlet-class> <init-param> <param-name>Log4j-init-file</param-name> <param-value >WEB-INF/classes/Log4j.properties</param-value> </init-param> <load-on-startup>1</ Load-on-startup> </servlet>

<servlet> <servlet-name>Log4j-init</servlet-name> <servlet-class>xx.xx.log4jinit</ servlet-class> <init-param> <param-name>Log4j-init-file</param-name> <param-value> Web-inf/classes/log4j.properties</param-value> </init-param> <load-on-startup>1</ Load-on-startup> </servlet>

Writing an initialized servlet is the most resilient way to initialize log4j. There are no restrictions in the code, you can define it in the servlet init method. 3. Initialize log4j based on configuration file

Log4j can be initialized using the 3 configurator: Basicconfigurator,domconfigurator,propertyconfigurator
The syntax is: basicconfigurator.configure (): Automatically and quickly uses the default log4j environment. Propertyconfigurator.configure (String configfilename): Reads a configuration file written using a Java-specific attribute file. Domconfigurator.configure (String filename): Reads a configuration file in XML form.

The use of Propertyconfigurator applies to all systems. such as the following statement:
propertyconfigurator.configure ("log4j.properties");


The log4j environment is initialized with Log4j.properties for the configuration file.
Note that this statement only needs to be executed once when the system is started.

For example, it is called once in the Actionservlet init () method.  Java code public class Actionservlet extends httpservlet{.../** * Initialize Global variables * * public void init ()   Throws Servletexception {//Initialize action resource try{initlog4j (); ...   }   catch (IOException e) {throw new Servletexception ("Load actionres is Error");   } ... protected void initlog4j () {propertyconfigurator.configure ("log4j.properties"); }   ...   } End Class Actionservlet

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.