Log4j Study (ON)

Source: Internet
Author: User
Tags format config format definition log log log thread log4j
CSDN has a lot of examples and tutorials on log4j, some are very good, but in order to consolidate knowledge, I also write a bar.





my request to log4j is not very high, mainly has two uses





one) How to use log4j and common-logging packages in Web applications;


II) How ordinary Java applications use log4j and common-logging packages;





first summarize the simple, how to use log4j and common-logging packages in a common Java application





steps are as follows:


1) Add Log4j.jar and Common-logging.jar two packages to environment variables or classpath





2) Write Log4j.xml file, for example:





<?xml version= "1.0" encoding= "UTF-8" >


<! DOCTYPE log4j:configuration SYSTEM "Log4j.dtd" >





<log4j:configuration xmlns:log4j= "http://jakarata.apache.org/log4j/" >





<appender name= "file" class= "Org.apache.log4j.RollingFileAppender" >


<param name= "File" value= "Log.log"/>


<layout class= "Org.apache.log4j.PatternLayout" >


<param name= "Conversionpattern" value= "%d [%t]%p-%m%n"/>


</layout>


</appender>





<logger name= "Com.log4j.test.test" additivity= "false" >


<level value= "Debug"/>


<appender-ref ref= "file"/>


</logger>





<root>


<level value= "info"/>


<appender-ref ref= "file"/>


</root>





</log4j:configuration>





3) Program use method:


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 TestPrint1 ()


{


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 the message as required.





additional LOG4J.DTD





log4j:configuration


|


+--Appender (Name, Class)


| |


| +--param (name, value)


| +--layout (Class)


| |


| +--param (name, value)


+--Logger (name, additivity)


| |


| +--level (class, value)


| | |


| | +--param (name, value)


| +--appender-ref (ref)


+--Root


|


+--param (Name, Class)


+--level


| |


| +--param (name, value)


+--appender-ref (ref)








output format definition:


%m The message specified in the output code


-%p Output Priority


%r Output The number of milliseconds it takes to output the log information

The
%c output belongs to the class, usually the full name of the class in which it is located


%t Output The thread name that generated the log event


%n output A carriage return line break, the Windows platform is "\ r \ n" and the UNIX platform is "\ n"


%d output log point-in-time date or time, the default format is ISO8601, or you can specify a format after it, such as:%d{yyy MMM dd hh:mm:ss,sss}, Output is similar: October 18, 2002 22:10:28, 921


%l where the output log event occurs, including the class name, the thread that occurred, and the number of lines in the code. Generally used it does not need to use%c.











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.