Log4j log management system using a simple note

  Abstract: Log4j log management system using a simple note 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> 
  <table Width="673" border="0"> <tr> <td width="265"> </ td> <td width="398"> Log4j log management system using a simple note 

  Log4j has three main components: Loggers, and Layouts Appenders here can be understood as a simple log type, log output to the local and export log in what form.    Integrated use of these three components can be easily record of the type and level of information, and to be running log export control pattern and location.    Below the three separate components: 

  1, Loggers 

  Loggers components in this system are divided into five levels: DEBUG, INFO, WARN, and FATAL ERROR.    This is a five-level order: 

  DEBUG <INFO <WARN <ERROR <FATAL 

  Understand that this is one very important point here Log4j have a rule: Loggers assumption that the level of P, if the Loggers had occurred in one of the high-level Q, P, it can activate, otherwise suppress. 
  </ Td> </ tr> </ table> 
  For example, Java programs: 

  / / Build Logger an example, the name "com.foo" 
  Logger logger = Logger.getLogger ( "com.foo"); 
  / / Set the logger level.    Procedures are usually not set the level logger.    General settings in the configuration file. 
  Logger.setLevel (Level.INFO); 
  Logger barlogger = Logger.getLogger ( "com.foo.Bar"); 
  / / Below this request available, because WARN> = INFO 
  Logger.warn ( "Low fuel level."); 
  / / Below this request can not be used because DEBUG <INFO 
  Logger.debug ( "Starting search for nearest gas station."); 
  / / The name "com.foo.bar" examples barlogger will inherit an example "com.foo" level.    Thus, the following this request available, because INFO> = INFO 
  Barlogger.info ( "Located nearest gas station."); 
  / / Below this request can not be used because DEBUG <INFO 
  Barlogger.debug ( "Exiting gas station search"); 

  Here "is available" means Logger can output information. 

  In the name of the Logger example, no restriction could be of interest from arbitrary name.    Under normal circumstances recommendations to the location of the named Logger example, it is more effective in terms of the Logger naming.    This makes each category to build their own log information, manageable.    For example: 

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

  2, Appenders 

  Banned the use of log request only Log4j one of the small places, Log4j log system allows the log output to different places, such as the console (Console), paper (Files), according to the number of days or file size of a new document, to flow in the form sent to other places, and so on. 

  Its syntax is expressed as: 

  Org.apache.log4j.ConsoleAppender (console), 
  Org.apache.log4j.FileAppender (document), 
  Org.apache.log4j.DailyRollingFileAppender (produce a daily log file) 
  Org.apache.log4j.RollingFileAppender (file size, arrived at the designated size and it is a new document) 
  Org.apache.log4j.WriterAppender (log information to be sent to any format-designated place) 

  Configuration for use: 

  Log4j.appender.appenderName = fully.qualified.name.of.appender.class 
  Log4j.appender.appenderName.option1 = value1 
…
  Log4j.appender.appenderName.option = valueN 
  This log on to the output to provide a great convenience. 

  3, Layouts 

  Sometimes users want according to their own preferences formatted log output.    Log4j can be in the back of additional Layouts Appenders to complete this function.    Layouts offers four log output format, such as HTML styles according to specified forms, including log-level and the information contained logs and the style of the time, thread, patterns of the types of information and so on. 

  Its syntax is expressed as: 

  Org.apache.log4j.HTMLLayout (HTML table form layout), 
  Org.apache.log4j.PatternLayout (the flexibility to specify the layout mode), 
  Org.apache.log4j.SimpleLayout (including log information and the level of information string), 
  Org.apache.log4j.TTCCLayout (including log the time, thread, type information, etc.) 

  Configuration for use: 

  Log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class 
  Log4j.appender.appenderName.layout.option1 = value1 
…
  Log4j.appender.appenderName.layout.option = valueN 

  The above is from the Log4j Principle shows the use of the Java programming in specific Log4j can refer to the use of the following examples: 
  1, the establishment of Logger examples: 
  Grammar: public static Logger getLogger (String name) 
  Actual use: static Logger logger = Logger.getLogger (ServerWithLog4j.class.getName ()); 
  2, read configuration files: 
  Logger obtained examples, the following will be configured Log4j use of the environment: 
  Grammar said: 
  BasicConfigurator.configure (): Automatic use of default Log4j environment. 
  PropertyConfigurator.configure (String configFilename): Read the use of the characteristics of Java document prepared by the configuration files. 
  DOMConfigurator.configure (String filename): Read the form of XML configuration files. 
  Actual use: PropertyConfigurator.configure ( "ServerWithLog4j.properties"); 
  3, insert the log information to complete the above steps, even after Below can log on at different levels should insert into your log records any place. 
  Grammar said: 
  Logger.debug (Object message); 
  Logger.info (Object message); 
  Logger.warn (Object message); 
  Logger.error (Object message); 
  Actual use: logger.info ( "ServerSocket before accept:" + server); 

  In the actual programming, to make Log4j really running on the system configuration files prior to the definition.    Logger step is the definition, the difference between Appender and Layout use, as follows: 
  1, root Logger configuration, its syntax: 
  Log4j.rootLogger = [level], appenderName, appenderName,… 
  Logger level here that the priority, appenderName log information is output, multiple output can be designated to.    Such as: log4j.rootLogger = INFO, A1, A2 
  2, configuration information log output destination, its syntax: 
  Log4j.appender.appenderName = fully.qualified.name.of.appender.class 
  Can be specified above a five destinations. 
  3, configuration information log format, syntax: 
  Log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class 
  Here is the above three steps in front of the Log4j components of a simplified; Below is a specific configuration examples, in the light of the implementation process can be: 
  Log4j.rootLogger = INFO, A1 
  Log4j.appender.A1 = org.apache.log4j.ConsoleAppender 
  Log4j.appender.A1.layout = org.apache.log4j.PatternLayout 
  4r-log4j.appender.A1.layout.ConversionPattern =%% d%-5p (yyyy-MM-dd HH: mm: ssS)% c% m% n 
  It should be stated here is the log information in the format represented by the symbols of several meanings: 
  - X: X information output left-justified; 
  % P:-log information 
  % D (): log information Generation 
  % C: Logs site (category) 
  % M: the log specific information 
  % N: output log information for firms 

  According to the above log format, the output of a particular procedure was as follows: 
  0 INFO 2003-06-13 13:23:46968 ClientWithLog4j Client socket: Socket [addr = localhost/127.0.0.1, port = 8002, 2014 = localport] 
  16 DEBUG 2003-06-13 13:23:46984 ClientWithLog4j Server says: 'Java server with log4j, Fri Jun 13 13:23:46 CST 2003' 
  16 DEBUG 2003-06-13 13:23:46984 ClientWithLog4j GOOD 
  16 DEBUG 2003-06-13 13:23:46984 ClientWithLog4j Server responds: 'Command' HELLO 'not understood.' 
  16 DEBUG 2003-06-13 13:23:46984 ClientWithLog4j HELP 
  16 DEBUG 2003-06-13 13:23:46984 ClientWithLog4j Server responds: 'Vocabulary: HELP QUIT' 
  16 DEBUG 2003-06-13 13:23:46984 ClientWithLog4j QUIT 

  </ Td> </ tr> <tr> 

  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: , ,

Releated Java Articles

Comments

Leave a Reply