Log4j learning (on)

xiaoxiao2021-03-06  49

There are a lot of examples on log4j on the 9CBS, some written very well, but in order to consolidate knowledge, I also wrote a copy.

My requirements for log4j are not very high, there are two main purposes.

A) How to use log4j and common-logging packages in web applications;

2) How the normal Java application uses log4j and common-logging packages;

First summarize simple, how to use log4j and common-logging packages in ordinary Java applications

Proceed as follows:

1) Add log4j.jar and common-logging.jar two packages to environment variables or classpath

2) Write log4j.xml files, for example:

3) How to use in the program:

Package com.log4j.test;

Import org.apache.log4j.xml.domconfigurator;

Import org.apache.commons.logging. *;

Class configlog4j {public static void config () {Domconfigurator.configure ("config / log4j.xml");}} public class testlog4j {private log log = logfactory.getlog (Testlog4j.class);

Public Testlog4j () {configlog4j.config ();

}

Public void test1 () {log.debug ("debug test"); log.info ("info test"); log.warn ("Warn Test"); log.error ("Error Test");} public static void Main (String [] args) {testlog4j test = new testlog4j (); test.testprint1 ();

}

}

OK! This will print a message as required.

Also attached to log4j.dtd

Log4j: Configuration

|

- Appender (Name, CLASS)

| | |

| - Param (Name, Value)

| - Layout (Class)

| | |

| - Param (Name, Value)

- Logger (Name, Additive)

| | |

| - Level (Class, Value)

| | | | |

| | - Param (Name, Value)

| - Appender-Ref (REF)

- root

|

- Param (Name, Class)

- Level

| | |

| - Param (Name, Value)

- appender-ref (Ref)

Output format definition:

Message% P output priority% R output from the% m output code Output Self-application starting to output the LOG information consuming the category to which the species belongs to the species, usually the full name% T output of the class generates the log event The thread name% N outputs a carriage return, the Windows platform is "/ r / n", the Unix platform is "/ N"% D output log time point or time, the default format is ISO8601, or after it can be Specify format, such as:% D {YYY MMM DD HH: MM: SS, SSS}, Output Similar: October 18, 2002 22: 28, 921% l Output Log Event location, including category names, The thread that occurs, and the number of rows in the code. It is generally used without using% C.

references:

http://blog.9cbs.net/lxblg/archive/2004/09/14/104207.aspx

http://blog.9cbs.net/baijsp/archive/2004/09/03/log4j.aspx

转载请注明原文地址:https://www.9cbs.com/read-116577.html

New Post(0)