Application of the use of the web log tool log4J

  Abstract: In the web application tools used in the log log4J 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> Log4J Jakarta is under an open source code component, with Log4J, we can use custom the format of the debug information and log information to the output of one or more areas in need.    Web applications in general use of a specialized Log4J Servlet to complete the configuration, and to ensure that the web.xml configuration, the other in the Servlet Servlet before, in order to call in the Servlet and jsp.    Below is the servlet, the code below: 
  <table Width="685" height="35" border="0"> <tr> <td width="399"> package example; 
  Import org.apache.log4j .*; 
  Import javax.servlet.http.HttpServlet; 
  Import javax.servlet.http.HttpServletRequest; 
  Import javax.servlet.http.HttpServletResponse; 

  Public class Log4jInit extends HttpServlet ( 
  Public void init () ( 
  String prefix = getServletContext (). GetRealPath ("/"); 
  String file = getInitParameter ( "log4j ");// configuration file location 
  If (file! = Null) ( 
  PropertyConfigurator.configure (prefix + file); 
  ) 
  ) 

  Public void doGet (HttpServletRequest req, HttpServletResponse res) 
  () 
  ) </ Td> <td width="275"> </ td> </ tr> </ table> 
  In this servlet configure web.xml: 

………..
  Servlet> 
  Log4j-init 
  Example.Log4jInit 

  Log4j 

WEB-INF/log4j.properties

  A 

………..


  Log4J used to configure the properties of documents: 
  Log4j.rootLogger = debug, A1, R 
  Log4j.appender.A1 = org.apache.log4j.ConsoleAppender 
  Log4j.appender.A1.layout = org.apache.log4j.PatternLayout 
  Log4j.appender.A1.layout.ConversionPattern =% d (-yyyy-MM-dd HH: mm: ss) [% c] - [% p]% m% n 
  Log4j.appender.R = org.apache.log4j.RollingFileAppender 
  Log4j.appender.R.File = cwblog4j.log 
  Log4j.appender.R.MaxFileSize = 100KB 
  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 specified two output sources A1 and R.    The former log information output to the console, which is a Web log files.    The largest file is 100 KB, when a log file reaches the maximum size, Log4J automatically example.log example.log.1 to rename, and then the rebuilding of a new example.log documents were rotate. 

  Test documents: 
  <% @ Page contentType = "text / html; charset = GB2312"%> 
  <% @ Page import = "org.apache.log4j .*"%> 
<%
  Logger logger = Logger.getLogger ( "test.jsp"); 
  Logger.debug ( "befor say hi"); 
%>

Hi


  <% Logger.info ( "after say hi ");%> 

  On the server, $ TOMCAT_HOME/log4j.log document that the following information: 
  DEBUG Thread-5 testlog4j.jsp - befor say hi 
  INFO Thread-5 testlog4j.jsp - after say hi 
  In the $ TOMCAT_HOME / logs / stdout.log the final document has the following output. 
  Information: Server startup in 5678 ms 
  2004-07-07 09:54:05 [testlog4j.jsp] - [DEBUG] befor say hi 
  2004-07-07 09:54:05 [testlog4j.jsp] - [INFO] after say hi </ 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