Java Web log Details

Source: Internet
Author: User

First step: First set up a Web project, go to log4j website under the log4j jar package into the project Lib directory

The second step: in the SRC directory to build a log4j.properties file, file naming can be by themselves, just remember loading time and the name of the same line;

The contents of log4j.properties are as follows:

# # # set log levels # # #
Log4j.rootlogger = Debug,stdout,e

Log4j.appender.stdout = Org.apache.log4j.ConsoleAppender
Log4j.appender.stdout.Target = System.out
Log4j.appender.stdout.layout = Org.apache.log4j.PatternLayout
Log4j.appender.stdout.layout.ConversionPattern =%d{absolute}%5p%c{1}:%l-%m%n

Log4j.appender.e= Org.apache.log4j.DailyRollingFileAppender
Log4j.appender.e.file =${catalina.home}/logs/log_
Log4j.appender.e.datepattern=yyyy-mm-dd '. Log '
Log4j.appender.e.threshold =debug
Log4j.appender.e.layout = Org.apache.log4j.PatternLayout
Log4j.appender.e.layout.conversionpattern =%-d{yyyy-mm-dd hh\:mm\:ss}[%c] [%t\:%r]-[%p]%m%n

You can specify a single log output to a file:

Log4j.logger.pay-log=info,pay-loglog4j.appender.pay-log=org.apache.log4j.dailyrollingfileappender Log4j.appender.pay-log.file=d:/logs/pay-log.loglog4j.appender.pay-log.datepattern= '. '  Yyyy-mm-ddlog4j.appender.pay-log.threshold =debuglog4j.appender.pay-log.layout=org.apache.log4j.patternlayout Log4j.appender.pay-log.layout.conversionpattern =%-d{yyyy-mm-dd hh\:mm\:ss}[%c] [%t\:%r]-[%p]%m%n



Explain the next few common, the other in detail, please go to the Internet to find information:
Log4j.rootlogger = debug,stdout,e   means that the parent class logger will be output to a log higher than the debug level Stdout,e two places, subclasses are the same. The
stdout is a logging configuration that means that the log is output to the console, which is the configuration property--->log4j.appender.stdout.target = System.out &NBSP, when the time comes to production can be annotated off on it.
E is a logging configuration that logs log files to a file, Log4j.appender.e.file =${catalina.home}/logs/log_  is the path where the log files are placed, where ${ Catalina.home} is the path to the container
log4j.appender.e= org.apache.log4j.dailyrollingfileappender  This property configuration is a day to produce a new log file (there are other configuration methods, this is the most used)
Log4j.appender.e.datepattern=yyyy-mm-dd '. Log '    This property is a time-based file log with the following form:
Log_2011-06-26.log,log_2011-06-28.log, but the first log file generated is the name: Log_
Log4j.appender.e.layout.conversionpattern =%-d{yyyy-mm-dd hh\:mm\:ss}[%c] [%t\:%r]-[%p] %m%n  This is the output log content format, please check the information yourself;
Log4j.appender.e.threshold =debug  This is the output log level (meaning that only log information that is higher than the DEBUG level is output);

#appender名字定义
Log4j.rootlogger=info,file,consoleappender,rollingfile,mail,dailyrollingfile,database
Log4j.addivity.org.apache=true

#配置日志信息输出目的地
#log4j. Appender.appendername = Fully.qualified.name.of.appender.class
# (eg: log4j.appender.RollingFile = org.apache.log4j.RollingFileAppender)
# 1.org.apache.log4j.consoleappender (console)
# 2.org.apache.log4j.fileappender (file)
# 3.org.apache.log4j.dailyrollingfileappender (Generate one log file per day)
# 4.org.apache.log4j.rollingfileappender (creates a new file when the file size reaches the specified size)
# 5.org.apache.log4j.writerappender (send log information in stream format to any specified location)

#配置日志信息的格式
#log4j. appender.appenderName.layout = Fully.qualified.name.of.layout.class
# (eg: log4j.appender.RollingFile.layout = org.apache.log4j.HTMLLayout)
# 1.org.apache.log4j.htmllayout (Layout in HTML table Form),
# 2.org.apache.log4j.patternlayout (with flexibility to specify layout mode),
# 3.org.apache.log4j.simplelayout (contains the level of log information and the information string),
# 4.org.apache.log4j.ttcclayout (contains information about the time, thread, category, etc.) of log generation

#日记记录的优先级: Priority, from high to low
# OFF, FATAL, ERROR, WARN, INFO, DEBUG, all.
# LOG4J recommends using only the five levels of fatal, ERROR, WARN, INFO, Debug.
# error is a serious error, mainly a program bug
# WARN for general warnings, such as session loss
# Info is the general information to display, such as login log out
# Debug information for the program debug


#输出格式: The parameters in layout begin with%, and subsequent parameters represent different formatting information (the parameters are listed in alphabetical order):
#%c outputs the full name of the class, which can be modified to%d{num}, the dimension that the Num class name outputs (such as: "Org.apache.elathen.ClassName",%c{2} will output elathen. ClassName)
#%d output log time in the format%d{yyyy-mm-dd hh:mm:ss,sss}, you can specify a format such as%d{hh:mm:ss}
#%l Output Log event location, including class name, thread occurrence, number of lines in code
#%n Line break
#%m Output code specifies information, such as info ("message"), output message
#%p output priority, i.e. FATAL, ERROR, etc.
#%r the number of milliseconds to output from boot to display the log information
#%t Output The name of the thread that generated the log event


#ConsoleAppender, console output
#FileAppender, file log output
#SMTPAppender, outgoing mail output log
#SocketAppender, Socket log
#NTEventLogAppender, Window NT log
#SyslogAppender,
#JMSAppender,
#AsyncAppender,
#NullAppender

Step Three: Configure the container to load the log4j configuration file when it is started:
Create a servlet content as follows:

Package com.tchzt.logTest;

Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;

Import Org.apache.log4j.PropertyConfigurator;

public class Log4jinit extends HttpServlet {
Private static final long serialversionuid = 1L;
public void Destroy () {
Super.destroy ();
}

Public Log4jinit () {
Super ();
}

/**
* Initialization of the servlet. <br>
*
* @throws Servletexception If an error occurs
*/
public void Init () throws Servletexception {
String file =this.getinitparameter ("log4j");//read from Web. XML configuration, name must be consistent with Web. XML configuration
if (file! = null) {
Propertyconfigurator.configure (file);
}
}

}

Web. XML configuration:
<servlet>
<servlet-name>Log4jInit</servlet-name>
<servlet-class>com.tchzt.logTest.Log4jInit</servlet-class>
<init-param>
<param-name>log4j</param-name>//the name is the identity of the bottom path configuration (like key)
<param-value>/web-inf/classes/log4j.properties</param-value>//This is the path to load the log4j configuration file when the container initializes (this is like a value);
</init-param>
</servlet>

Okay, basically, the configuration is done, and the test begins:
Test class: Create a servlet content as follows:

Package com.tchzt.logTest;

Import java.io.IOException;

Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import Org.apache.log4j.Logger;

public class Test extends HttpServlet {

Private static final long serialversionuid = 1L;

/**
* The Doget method of the servlet. <br>
*
* This method was called when a form had its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response The response send by the server to the client
* @throws servletexception If an error occurred
* @throws IOException If an error occurred
*/
public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
If the direct Classname.class log is output to the global Rootlogger specified file
Logger Logger = Logger.getlogger (Test.class.getName ());
If the logger name is specified, the log is output to the log file specified by the Pay-log.
Logger Logger = Logger.getlogger ("Pay-log");
Mylog4j.getsomething ();
System.out.println ("================97987==============");
Logger.info ("Log information start!");
Logger.info ("Log information end!");
try {
Integer.parseint ("a");
} catch (NumberFormatException e) {
Logger.error ("Parsing of numbers appears to be abnormal", e);
E.printstacktrace ();
}
}

/**
* The DoPost method of the servlet. <br>
*
* This method was called when a form had its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response The response send by the server to the client
* @throws servletexception If an error occurred
* @throws IOException If an error occurred
*/
public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
This.doget (request, response);
}

}


The Web. XML configuration for this servlet is as follows:
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>com.tchzt.logTest.Test</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/servlet/Test</url-pattern>
</servlet-mapping>


Boot container: Output url:http://localhost:8080/log4jwebtest/servlet/test
The second time to close the container, modify the system time; In the boot container, the last generated log file is as follows:

Log_;log_2011-06-26.log;log_2011-06-28.log;log_2011-06-30.log

Content Select a file here:
2011-06-30 21:59:28[test.jsp] [http-8080-2:0]-[ERROR] Asdfasdf
2011-06-30 21:59:28[test.jsp] [http-8080-2:16]-[INFO] test Logging in JSP
2011-06-30 21:59:28[test.jsp] [http-8080-2:16]-[FATAL] test FATAL logging in JSP page.
2011-06-30 21:59:28[test.jsp] [http-8080-2:16]-[ERROR] parsing number exception
Java.lang.NumberFormatException:For input string: "A"
At java.lang.NumberFormatException.forInputString (Unknown Source)
At Java.lang.Integer.parseInt (Unknown Source)
At Java.lang.Integer.parseInt (Unknown Source)
At Org.apache.jsp.index_jsp._jspservice (index_jsp.java:90)
At Org.apache.jasper.runtime.HttpJspBase.service (httpjspbase.java:70)
At Javax.servlet.http.HttpServlet.service (httpservlet.java:803)
At Org.apache.jasper.servlet.JspServletWrapper.service (jspservletwrapper.java:393)
At Org.apache.jasper.servlet.JspServlet.serviceJspFile (jspservlet.java:320)
At Org.apache.jasper.servlet.JspServlet.service (jspservlet.java:266)
At Javax.servlet.http.HttpServlet.service (httpservlet.java:803)
At Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (applicationfilterchain.java:290)
At Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:206)
At Org.apache.catalina.core.StandardWrapperValve.invoke (standardwrappervalve.java:233)
At Org.apache.catalina.core.StandardContextValve.invoke (standardcontextvalve.java:175)
At Org.apache.catalina.core.StandardHostValve.invoke (standardhostvalve.java:128)
At Org.apache.catalina.valves.ErrorReportValve.invoke (errorreportvalve.java:102)
At Org.apache.catalina.core.StandardEngineValve.invoke (standardenginevalve.java:109)
At Org.apache.catalina.connector.CoyoteAdapter.service (coyoteadapter.java:263)
At Org.apache.coyote.http11.Http11Processor.process (http11processor.java:844)
At Org.apache.coyote.http11.http11protocol$http11connectionhandler.process (http11protocol.java:584)
At Org.apache.tomcat.util.net.jioendpoint$worker.run (jioendpoint.java:447)
At Java.lang.Thread.run (Unknown Source)
2011-06-30 21:59:37[com.tchzt.logtest.mylog4j] [http-8080-2:8641]-[ERROR] Java.io.filenotfoundexception:f:\tt.txt (The system cannot find the file specified.) )
2011-06-30 21:59:37[com.tchzt.logtest.test] [http-8080-2:8656]-[INFO] log information start!
2011-06-30 21:59:37[com.tchzt.logtest.test] [http-8080-2:8656]-[INFO] The end of the log message!
2011-06-30 21:59:37[com.tchzt.logtest.test] [http-8080-2:8656]-[ERROR] parsing number exception
Java.lang.NumberFormatException:For input string: "A"
At java.lang.NumberFormatException.forInputString (Unknown Source)
At Java.lang.Integer.parseInt (Unknown Source)
At Java.lang.Integer.parseInt (Unknown Source)
At Com.tchzt.logTest.Test.doGet (test.java:33)
At Javax.servlet.http.HttpServlet.service (httpservlet.java:690)
At Javax.servlet.http.HttpServlet.service (httpservlet.java:803)
At Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (applicationfilterchain.java:290)
At Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:206)
At Org.apache.catalina.core.StandardWrapperValve.invoke (standardwrappervalve.java:233)
At Org.apache.catalina.core.StandardContextValve.invoke (standardcontextvalve.java:175)
At Org.apache.catalina.core.StandardHostValve.invoke (standardhostvalve.java:128)
At Org.apache.catalina.valves.ErrorReportValve.invoke (errorreportvalve.java:102)
At Org.apache.catalina.core.StandardEngineValve.invoke (standardenginevalve.java:109)
At Org.apache.catalina.connector.CoyoteAdapter.service (coyoteadapter.java:263)
At Org.apache.coyote.http11.Http11Processor.process (http11processor.java:844)
At Org.apache.coyote.http11.http11protocol$http11connectionhandler.process (http11protocol.java:584)
At Org.apache.tomcat.util.net.jioendpoint$worker.run (jioendpoint.java:447)
At Java.lang.Thread.run (Unknown Source)

Java Web log Details

Related Article

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.