Log4j concise manual (2 / 3)

  4. Configuration 

  Request to log into the application's code requires a large amount of pre-planned and final effort.    Observation showed that about 4% of the code is used to output. 

  Therefore, the appropriate size of the procedures have been embedded with thousands of log output statements.    In order to manage without these handmade log output state to the log output to code and norms become imperative. 

  Log4j is in the process of fully configurable.    However, the configuration file configuration Log4j with greater flexibility.    At present, its configuration file support xml and java properties (key = value) file formats. 

  Let us take an example to demonstrate how it is done.    Assume that there is a procedure used Log4j MyApp. 

  Import com.foo.Bar; 

  / / Import Log4j classes. 

  Import org.apache.Log4j.Logger; 

  Import org.apache.Log4j.BasicConfigurator; 

  (Public class MyApp 

  / / Define a static logger variable so that it references the 

  / / Logger instance named "MyApp." 

  Static Logger logger = Logger.getLogger (MyApp.class); 

  Public static void main (String [] args) ( 

  / / Set up a simple configuration that logs on the console. 

  BasicConfigurator.configure (); 

  Logger.info ( "Entering application."); 

  Bar bar = new Bar (); 

  Bar.doIt (); 

  Logger.info ( "Exiting application."); 

  ) 

  ) 

  Log4j MyApp to introduce the relevant category started, and then it defines a logger static variables, and give value "MyApp" the full path name. 

  MYApp used in the definition of the class packet com.foo Bar. 

  Package com.foo; 

  Import org.apache.Log4j.Logger; 

  (Public class Bar 

  Static Logger logger = Logger.getLogger (Bar.class); 

  Public void doIt () ( 

  Logger.debug ( "Did it again!"); 

  ) 

  ) 

  Call BasicConfigurator.configure () method to create a very simple Log4j settings.    It joined a 

  A ConsoleAppender to the root logger.    Output will be adopted "r% -4 [% t]%-5p% c% x -% m% n" mode 

  Formatted by the PatternLayout. 

  Note that the default root logger was allocated Level.DEBUG level. 

  MyApp the output: 

  0 [main] INFO MyApp - Entering application. 

  36 [main] DEBUG com.foo.Bar - Did it again! 

  51 [main] INFO MyApp - Exiting application. 

  Graphic describes the subsequent calling BasicConfigurator.configure () method after the MyApp the object graph. 

  Side to remind that the only Log4j sub-logger connected to the already existing behalf of their father.    In particular, called 

  Com.foo.bar the logger is directly connected to the root logger, and not around useless com or com.foo 

  Logger.    This significantly improved performance and reduced memory footprint. 

  MyApp class configuration Log4j is by calling BasicConfigurator.configure methods.    The only other category 

  Need to introduce org.apache.Log4j.Logger category, they hope to find the logger, and to use it. 

  Examples of previous output usually the same log information.    Fortunately, the revised MyApp is easy to lose log 

  In the run time can be controlled.    Here is a small amended version. 

  Import com.foo.Bar; 

  Import org.apache.Log4j.Logger; 

  Import org.apache.Log4j.PropertyConfigurator; 

  (Public class MyApp 

  Static Logger logger = Logger.getLogger (MyApp.class.getName ()); 

  Public static void main (String [] args) ( 

  / / BasicConfigurator replaced with PropertyConfigurator. 

  PropertyConfigurator.configure (args [0]); 

  Logger.info ( "Entering application."); 

  Bar bar = new Bar (); 

  Bar.doIt (); 

  Logger.info ( "Exiting application."); 

  ) 

  ) 

  After the revised notification procedures call PropertyConfigurator MyApp () method analysis of a configuration file, and roots 

  According to the configuration file to set up log. 

  Here is an example of the configuration file, it will have the same basic case of the past BasicConfigurator 

  The output results. 

  # Set root logger level to DEBUG and its only appender to A1. 

  Log4j.rootLogger = DEBUG, A1 

  # A1 is set to be a ConsoleAppender. 

  Log4j.appender.A1 = org.apache.Log4j.ConsoleAppender 

  # A1 uses PatternLayout. 

  Log4j.appender.A1.layout = org.apache.Log4j.PatternLayout 

  4r-Log4j.appender.A1.layout.ConversionPattern =% [% t]%-5p% c% x -% m% n 

  On the assumption that we do not com.foo package the output of any type of interest, then the configuration file to show us 

  To achieve this aim one of the ways. 

  Log4j.rootLogger = DEBUG, A1 

  Log4j.appender.A1 = org.apache.Log4j.ConsoleAppender 

  Log4j.appender.A1.layout = org.apache.Log4j.PatternLayout 

  # Print the date in ISO 8601 format 

  Log4j.appender.A1.layout.ConversionPattern =% d [% t]-5p% c% -% m% n 

  # Print only messages of level WARN or above in the package com.foo. 

  Log4j.logger.com.foo = WARN 

  This configuration file to configure good MyApp output as follows: 

  2000-09-07 14:07:41508 [main] INFO MyApp - Entering application. 

  2000-09-07 14:07:41529 [main] INFO MyApp - Exiting application. 

  When logger com.foo.bar has not been assigned a level, it will inherit from com.foo, in the configuration file 

  It was set up WARN level.    Bar.doIt methods in the definition of log for the DEBUG-level, lower than the WARN, 

  Therefore doIt () method log request is disabled. 

  Here is a separate configuration file, it uses a number of appenders. 

  Log4j.rootLogger = debug, stdout, R 

  Log4j.appender.stdout = org.apache.Log4j.ConsoleAppender 

  Log4j.appender.stdout.layout = org.apache.Log4j.PatternLayout 

  # Pattern to output the caller's file name and line number. 

  Log4j.appender.stdout.layout.ConversionPattern =% 5p [% t] (% F: L%) -% m% n 

  Log4j.appender.R = org.apache.Log4j.RollingFileAppender 

  Log4j.appender.R.File = example.log 

  Log4j.appender.R.MaxFileSize = 100KB 

  Keep one backup file # 

  Log4j.appender.R.MaxBackupIndex = 1 

  Log4j.appender.R.layout = org.apache.Log4j.PatternLayout 

  Log4j.appender.R.layout.ConversionPattern t =% p% c% -% m% n 

  This configuration file to the call to strengthen the MyApp output of the following information. 

  INFO [main] (MyApp2.java: 12) - Entering application. 

  DEBUG [main] (Bar.java: 8) - Doing it again! 

  INFO [main] (MyApp2.java: 15) - Exiting application. 

  In addition, because the root logger has been allocated appender second, the output will also be directed to example.log document. 

  The file size will be 100 kb automatic backups.    When the old version of the backup file is automatically moved to the example.log 

  Example.log.1 in the document. 

  We do not need the attention on the re-compiled code can log these different acts.    We can easily 

  The output to the log UNIX Syslog daemon, redirects all com.foo to NT Event logger, 

  Or transmitted to a remote log Log4j servers, in accordance with its strategy to the local server to log output.    Cases 

  If transmitted to the second incident log Log4j server. 

  5. Default initialization process 
  Log4j Class it is not the environment to do any assumptions.    In particular, no default Log4j appender.    In some special 

  Have a good definition of the environment, the static inializer logger will attempt to automatically configure Log4j. 

  Java language ensures that the static type initializer if and only if loaded into memory when the category will be called first. 

  An important point to remember is that different types of loading device may carry the same kind of a completely different copy. 

  These copies of the same kind of virtual machine that is completely irrelevant. 

  The default initialization is very useful, especially in some applications rely on the operating environment being accurate 

  Positioning the circumstances.    For example, with the same application can be used as a standard application, or a 

  Applet, or a web-server under the control of servlet. 

  Accurate default initialization principle is defined as follows: 

  1. Attribute Log4j.defaultInitOverride system set up for "false" other than value, so will Log4j 

  Skip the default initialization process. 

  2. String variables to set up system resources Log4j.configuration attributes.    Definition of the default initialization 

  The best way to the paper through the System Properties Log4j.configuration.    If the System Properties 

  Log4j.configuration not been defined, then set up a string variable resource to its default value 

  Log4j.properties. 

  3. Try to convert resource variables for a URL. 

  4. Variable resource if the value can not be converted to a URL, for example because MalformedURLException Offenders 

  Cases, by calling 

  Org.apache.Log4j.helpers.Loader.getResource (resource, Logger.class) Methods 

  Classpath resource in the search, it will return a URL, and notice "Log4j.properties" is a wrong value 

  Mistaken URL. 

  See See Loader.getResource (java.lang.String) Show location of the search list. 

  5. If there is no URL was found, then abandon the default initialization.    Otherwise URL configuration with Log4j. 

  PropertyConfigurator URL will be used for analytical, configuration Log4j, unless the URL to. "Xml" at the end. 

  In such circumstances it will be called DOMConfigurator.    You can have the opportunity to define a custom 

  Configurator. 

  System Properties Log4j.configuratorClass from the value of your custom category of the entire path. 

  Since the definition of configurator you must realize configurator interface. 

  6. Configuration examples 
  Tomcat 6.1 under the default initialization Log4j initialization Typical applications are in the web-server environment.    In tomcat3.x and tomcat4.x 

  , You should be Log4j.properties configuration file on your web application's directory WEB-INF/classes 

  Under. 

  Log4j will find properties file, and this initialization.    This work is to make it the easiest way. 

  You can also choose to set up before running tomcat Log4j.configuration system attributes.    The tomcat 3.x, 

  TOMCAT_OPTS system variables is used to set up the command line options.    For tomcat4.0, System environment variable 

  CATALINA_OPTS instead of the TOMCAT_OPTS. 

  Example 1 

  UNIX command line 

  Export TOMCAT_OPTS = "-DLog4j.configuration = foobar.txt" 

  Log4j foobar.txt told by the paper as the default configuration files.    This document should be placed on WEB-INF/classes 

  Directory.    PropertyConfigurator this document will be read.    Each web-application will use different default 

  Configuration file, as each document is the web and its application-related. 

  Example 2 

  UNIX command line 

  Export TOMCAT_OPTS = "-DLog4j.debug-DLog4j.configuration = foobar.xml" 

  Log4j output Log4j-told internal debugging information, and use foobar.xml as the default configuration files. 

  This document should be placed on your web-application of WEB-INF/classes directory.    Because there are. Xml the 

  Extension, which will be DOMConfigurator read.    Each web-application will use different default 

  Configuration files.    All because each document and its application in the web-related. 

  Example 3 

  UNIX command line 

  DLog4j.configuration set TOMCAT_OPTS = = = com.foo.BarConfigurator foobar.lcf-DLog4j.configuratorClass 

  Log4j foobar.lcf told by the paper as the default configuration files.    This document should be on your 

  Web-application of WEB-INF/classes directory.    Because the system is defined Log4j.configuratorClass 

  , The document will be used to the definition of com.foo.barconfigurator analytic category.    Each web-application will be used not 

  With the default configuration files.    All because each document and its application in the web-related. 

  Example 4 

  UNIX command line 

  Set TOMCAT_OPTS =- DLog4j.configuration = file: / c: / foobar.lcf 

  Log4j foobar.lcf told by the paper as the default configuration files.    This configuration file using URL file: / c: / foobar.lcf 

  Definition of the entire pathname.    This same configuration file will be all the web-application use. 

  Different web-application will be through their own class loader to load Log4j.    In this way, each of the Central Log4j 

  Habitat will be an independent operation, without any mutual synchronization.    For example: in a number of web-application of the definition 

  Exactly the same as the output source FileAppenders will attempt to write the same document.    Appears to be the result of the lack of security. 

  You must ensure that each different web-application of Log4j configuration does not use the same system resources. 

  Servlet 6.2 initialization using a special servlet to do the initialization is Log4j can.    The following is an example: 

  Package com.foo; 

  Import org.apache.Log4j.PropertyConfigurator; 

  Import javax.servlet.http.HttpServlet; 

  Import javax.servlet.http.HttpServletRequest; 

  Import javax.servlet.http.HttpServletResponse; 

  Import java.io.PrintWriter; 

  Import java.io.IOException; 

  Public class Log4jInit extends HttpServlet ( 

  Public void init () ( 

  String prefix = getServletContext (). GetRealPath ("/"); 

  String file = getInitParameter ( "Log4j-init-file"); 

  / / If the Log4j-init-file is not set, then no point in trying 

  If (file! = Null) ( 

  PropertyConfigurator.configure (prefix + file); 

  ) 

  ) 

  Public void doGet (HttpServletRequest req, HttpServletResponse res) ( 

  ) 

  ) 

  In web.xml servlet defined in the following for your web-application. 

<servlet>

  <servlet-name> Log4j-init </ servlet-name> 

  <servlet-class> Com.foo.Log4jInit </ servlet-class> 

<init-param>

  <param-name> Log4j-init-file </ param-name> 

  <param-value> WEB-INF/classes/Log4j.lcf </ param-value> 

  </ Init-param> 

  <load-on-startup> 1 </ load-on-startup> 

  </ Servlet> 

  Write a servlet init is the most flexible initialization Log4j method.    There is no restriction in the code, you can 

  To the servlet init method in the definition of it. 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Releated Articles

  • Popuklar Articles

0 Comments to “Log4j concise manual (2 / 3)”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.