Log4j learning notes (3)
I have two programs here, one is an ordinary Java program that implements a "ninety-nine table"; the other is a servlet. The Tomcat I used here is 4.1.12, J2se is 1.3.1, the Log4j version is 1.2.8.
First, nine nine tables. Environment settings: Need to put log4j-1.2.8.jar into the ClassPath variable. The content of the Hello.java file is as follows:
Import org.apache.log4j. *; public class hello {static logger logger = logger.getlogger (hello.class); public static void main (string [] args) {Int i, j; // Basicconfigurator.configure (); PropertyConfigurator.configure (args [0]); logger.info ("Entering Application."); For (i = 1; i <10; i ) {logger.debug (" i); for (j = 1; J <= i; j ) {logger.warn ("" j); system.out.print (i * j); system.out.print ("/ t");} system.out.println ("" " Logger.info ("EXITING Application.");
Log4j's profile log4j.inf is as follows:
log4j.rootLogger = WARN, stdout, Rlog4j.appender.stdout = org.apache.log4j.ConsoleAppenderlog4j.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 # print the date in ISO 8601 formatLog4j.convender.stdout.Layout.conversionPattern =% D [% T]% -5p% c -% m% nlog4j.appender.R = org.apache.log4j.RollingFileAppenderlog4j.appender.R.File = example.loglog4j.appender.R.MaxFileSize = 100KB # Keep one backup filelog4j.appender.R.MaxBackupIndex = 1 Log4j.Appender.r.Layout = org.apache.log4j.patternLayoutLog4j.Appender.r.Layout.conversionPattern =% P% T% C -% M% N # Print Only Messages of Level Warn OR Above In The Package COM. Foo.log4j.logger.com.foo = WARN
run:
Javac Hello.java
Java Hello Log4j.inf
The result will be displayed on the screen and record it in an Example.log file.
Second, servlet environment settings: Place log4j-1.2.8.jar and servlet.jar into environment variables ClassPath and to copy log4j-1.2.8 to $ Tomcat_home / common / lib directory. Assume that there is a deployment to Tomcat app called myWeb. The content of the servlet program log4jinit.java is in $ TOMCAT_HOME / WebApps / MyWeb / Web-INF / CLASS / COM / HEDONG / LEARNING / LOG4J / Directory, the content is as follows: package com.hedong.Learning.log4j; 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"); // if The log4j-init-file is not set, the no point in trying system.out.println ("......... ....... log4j start "); if (file! = null) {PropertyConfigurator.configure (prefix file);}} public void doget (httpservletRequest Req, httpservletResponse res) {}}
Compile it under the directory where log4jinit.java is located:
Javac log4jinit.java
MyWeb set file web.xml In the $ TOMCAT_HOME / WebApps / MyWeb / Web-INF / directory, the following red part is added.
web-app> .........
At the same time, build a file name called log4j.properties in this directory, as follows:
log4j.rootLogger = INFO, A1, Rlog4j.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.rollingfileAppenderLog4j.Appender.r.file = $ Tomcat_Home / WebApps / DBWeb /LOGS/LOG4J.LOG<-- Change $ Tomcat_home to Tomcat installation directory log4j.appender.r.maxfilesize = 100kblog4j.Appender.r.maxbackupindex = 1Log4j.Appender.r.Layout = Org.Apache.log4j. PatternLayOutlog4j.Appender.r.Layout.conversionPattern =% P% T% C -% M% N remember, don't forget to create a directory $ Tomcat_home / webApps / dbweb / logs / if it does not exist.
Create a directory $ Tomcat_home / WebApps / dbweb / test /, then build a file Test.jsp in this directory, as follows:
<% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ page import = "org.apache.log4j. *"%> <% Logger Logger = Logger.getlogger ("Test.jsp"); Logger.info ("Befor Say Hi");%>
Then, restart Tomcat, access this JSP page through the browser, such as http://yourdomain.com:8080/myweb/test/test.jsp, if everything is normal, you will see a big HI. Then, see the following information in the $ TOMCAT_HOME / WebApps / DbWeb / logs / log4j.log file on the server:
Info httpprocessor [8080] [4] Test.jsp - Befor Say Hi
Info httpprocessor [8080] [4] Test.jsp - After Say Hi
. By default, Tomcat's screen output is redirected to $ Tomcat_home / logs / catalina.out file, and the above output should also be seen in the file.