Log4j Log Operation method

Source: Internet
Author: User
Tags tomcat log4j

1.1 Preparation work

One, Tomcat has been properly configured and used.

Second, software download: log4j------Http://www.apache.org/dist/jakarta/log4j/jakarta-log4j-1.2.8.zip

1.2. log4j Introduction

In emphasizing reusable component development today, in addition to developing a reusable log operation class from beginning to end, Apache provides us with a powerful log-handling package-log4j.

Log4j is an open source project for Apache, by using log4j, we can control the destination of log information delivery is console, file, GUI component, even interface server, NT Event recorder, UNIX syslog daemon, etc. We can also control the output format of each log, and by defining the level of each log information, we can control the log generation process more carefully. Most interesting of all, these can be configured flexibly with a single configuration file without the need to modify the applied code.

In addition, by log4j other language interfaces, you can use log4j in C, C + +,. Net, and Pl/sql programs, and its syntax and usage, as in Java programs, make a unified and consistent Log component module for a multilingual distributed system. Also, by using a variety of third-party extensions, you can easily integrate log4j into Java EE, Jini, and even SNMP applications. This article describes the log4j version is 1.2.8, how to use a configuration file to configure flexibly, the main application platform is TOMCAT4.

1.3, the LOG4J configuration

First, download a log4j component to Jakarta. Copy the Log4j-1.2.8.jar file under Jakarta-log4j-1.2.8\dist\lib to the classpath specified directory! It can be Tomcat's common\lib directory, or it can be a Lib directory that you need to use to log4j application.

1.4在Application目录下的web.xml文件加入以后代码

<servlet>
<servlet-name>log4j</servlet-name>
<servlet-class>com.apache.jakarta.log4j.Log4jInit</servlet-class>
<init-param>
<param-name>log4j</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

This code means to load Com.apache.jakarta.log4j.Log4jInit This class file called Log4jinit.class when Tomcat is started. Where Log4jinit.class source code is as follows

package com.apache.jakarta.log4j;
import org.apache.log4j.PropertyConfigurator;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; 网管联盟bitsCN_com
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, then no point in trying
System.out.println("................log4j start");
if(file != null) {
PropertyConfigurator.configure(prefix+file);
}
}
public void doGet(HttpServletRequest req, HttpServletResponse res) {
}
}

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.