How to log logs using log4j

Source: Internet
Author: User
Tags log4j

It programmer development Essentials-all kinds of resources download list, history of the most IT resources, personal collection summary.


1. Why does the logger log not print?
Answer: (1) need to include the log package in the Resin_homg\lib directory, such as: Log4j-1.2.8.jar,commons-logging.jar;
(2) required in the Web-info\classes\ directory must contain: Commons-logging.properties and log4j.properties these two documents;
(3) The contents of the Commons-logging.properties file are as follows: If the log4j logger is used then it must be written as: org.apache.commons.logging.log= Org.apache.commons.logging.impl.Log4JLogger
# #set Log as log4j
Org.apache.commons.logging.log=org.apache.commons.logging.impl.log4jlogger

# # Set Log as Simplelog
#org. Apache.commons.logging.log=org.apache.commons.logging.impl.simplelog
(4) You need to configure initialization log4j in Web. xml:
(5) There are several ways to use Cheng:
The first way:
Importorg.apache.log4j.Logger;
privatestatic Logger Logger = Logger.getlogger (Loginaction.class);
Logger.info ("Now enter Loginaction:execute () method!");
The second way:
Importorg.apache.log4j.Category;
Privatestatic categorylogger=category.getinstance (Loginaction.class);
Logger.info ("Now enter Loginaction:execute () method!");
The Third Way:
Importorg.apache.commons.logging.Log;
Importorg.apache.commons.logging.LogFactory;
Privatestatic Log Logger=logfactory.getlog (loginaction.class);
Logger.info ("Now enter Loginaction:execute () method!");
*******************************************************************************************
See below for details:
One: Introduction
Log4j is a project of Jakarta is mainly for debugging use, in order not to let is written in the program a lot of System.out.println ();
I personally think is a good tool, the program after debugging you can turn off log4j.
Two: Download
You can download it from the address below to ant, current version: 1.13
http://jakarta.apache.org/builds/jakarta-log4j/release/v1.1.3/

Three: Installation
LOG4J does not need to be installed, you only need to put Log4j.jar,log4j-core.jar in classpath you can use.


Four: Using log4j
1:) Copylog4j.jar,log4j-core.jar to Classpath, in tomcat4.03 you can put it in
The Tomcat_home\common\lib directory.
2: Set Properties file
Log4j need to use a log4j.properties file content as follows:
Log4j.properties
=====================================
log4j.rootcategory=, A1
Log4j.appender.a1.layout=org.apache.log4j.patternlayout
The simplest configuration, if you want to be complex, look at the document yourself.

3: Write Startup file
There are several ways to initialize log4j, and I only offer one way below.
Initservlet.java (a servlet)
==================================
Importjava.io.InputStream;
Importjava.io.IOException;
importjava.util.Hashtable;
Importjava.util.Locale;
Importjava.util.MissingResourceException;
Importjavax.servlet.RequestDispatcher;
Importjavax.servlet.ServletException;
Importjavax.servlet.UnavailableException;
Importjavax.servlet.http.HttpServlet;
Importjavax.servlet.http.HttpServletRequest;
Importjavax.servlet.http.HttpServletResponse;
Importjavax.servlet.http.HttpSession;
importjavax.servlet.*;
importjava.io.*;

Public Classinitservlet extends HttpServlet {

Protected String config = "/log4j.properties";

public void Init () throws Servletexception {
ServletContext SCT = Getservletcontext ();
System.out.println ("[log4j]: The Root Path:" + sct.getrealpath ("/"));
System.out.println ("[log4j]: Initservlet init start ...");
Org.apache.log4j.PropertyConfigurator.configure (Sct.getrealpath ("/") + Getservletconfig (). Getinitparameter (" Log4j "));
System.out.println ("[log4j]: Initservlet init over.");
}


public void doget (HttpServletRequest request,
HttpServletResponse response)

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.