Common uses of log4j

Source: Internet
Author: User

For log4j, it is generally used as long as you save the following content as a property file:

1#log4j. rootlogger=Debug,a1,r2Log4j.rootlogger=Info,a1,r3log4j.appender.a1=Org.apache.log4j.ConsoleAppender4log4j.appender.a1.layout=Org.apache.log4j.PatternLayout5Log4j.appender.a1.layout.conversionpattern=%-d{yyyy-mm-dd hh:mm:ss,sss} [%c]-[%p]%m%N6 7Log4j.appender.r=Org.apache.log4j.RollingFileAppender8#log4j. appender.r.file=f:/log4j/Log.txt9Log4j.appender.r.maxfilesize=500KBTenlog4j.appender.r.maxbackupindex=10 Onelog4j.appender.r.layout=Org.apache.log4j.PatternLayout ALog4j.appender.r.layout.conversionpattern=%d{yyyy-mm-dd hh:mm:ss,sss} [%t] [%c] [%p]-%m%N -  -#log4j. Logger.org.springframework.transaction.interceptor=debug

Note: The 8th line above is used to generate a file named Log.txt on disk, this path has to be set up manually

Here is a brief introduction:

First, the LOG4J Foundation
1. log4j Introduction
LOG4J is an Apache open source project, through the log4j, we can record the program runtime error message, so that the software developers to analyze the cause of the error, fix the bug. LOG4J can also act as a debugger in an integrated development environment to print debugging information for the code to a file or console. Log4j can also monitor the running of a program, periodically logging to a file or database for future statistical analysis.

2. LOG4J consists of three main components
1) Logger components
The logger component is responsible for generating logs and is able to classify the logs to control what logs should be output. Multiple loggers can be defined at the same time, each logger has its own name, and the logger indicates affiliation by name (e.g. Recorder a.b is A.B.C's father). In log4j, there is a root logger (Org.apache.log4j.Logger Class) that is always present and cannot be retrieved or referenced by name, and can be obtained by its static method Getrootlogger () method. Other loggers are instantiated using the static method GetLogger (String name).
2) Appender components
In log4j, information is output to the destination through the Appender component, including consoles, files, GUI components, remote socket servers, JMS, NT event loggers, and the remote UNIX syslog daemon. As follows:
Org.apache.log4j.ConsoleAppender (console),
Org.apache.log4j.FileAppender (file),
Org.apache.log4j.DailyRollingFileAppender (generates a log file every day),
Org.apache.log4j.RollingFileAppender (creates a new file when the file size reaches the specified size),
Org.apache.log4j.WriterAppender (send log information in stream format to any specified location)
3) Layout component
The layout component is responsible for formatting the output log information, and a appender can have only one layout. LOG4J offers the following types of layout:
Org.apache.log4j.HTMLLayout (Layout in HTML table Form),
Org.apache.log4j.SimpleLayout (contains the level of log information and the information string),
Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, and so on) that the log generates.
Org.apache.log4j.xml.XMLLayout (<log4j:event> element components defined in a series of LOG4J.DTD),
Org.apache.log4j.PatternLayout (flexibility to specify layout mode, as flexible as C-language printf ())

3. Log level of the logger
The logger's log level is defined in the Org.apache.log4j.Level class, with a total of 5 possible levels, from low to high, followed by Debug,info,warn,error and fatal. If a logger does not specify a log level, it will inherit the level from the most recent ancestor of the specified level.

iii. Examples of log4j
1. Introduction of log4j
LOG4J official (This is version 2.0): http://logging.apache.org/log4j/

This is a centralized download: http://logging.apache.org/

This is the 1.0 version, generally recommended to use this http://logging.apache.org/log4j/1.2/download.html

2. Log4j.properties

Log4j The properties file configuration:

1 # # # definition Root logger log level # # #2Log4j.rootlogger =Debug,stdout,file1,file23 4 # # # output to console # # #5Log4j.appender.stdout =Org.apache.log4j.ConsoleAppender6Log4j.appender.stdout.Target =System.out7Log4j.appender.stdout.layout =Org.apache.log4j.PatternLayout8Log4j.appender.stdout.layout.ConversionPattern =%d{absolute}%5p%c{1}:%l-%m%N9 Ten # # # Output to log file # # # OneLog4j.appender.file1 =Org.apache.log4j.DailyRollingFileAppender ALog4j.appender.file1.File = logs/Log.log -Log4j.appender.file1.Append =true - # # # output debug levels above the log theLog4j.appender.file1.Threshold =DEBUG -Log4j.appender.file1.layout =Org.apache.log4j.PatternLayout -Log4j.appender.file1.layout.ConversionPattern =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%N -  + # # # Save exception information to a separate file # # # -Log4j.appender.file2 =Org.apache.log4j.DailyRollingFileAppender +Log4j.appender.file2.File = logs/Error.log ALog4j.appender.file2.Append =true at # # # Only output logs above the error level -Log4j.appender.file2.Threshold =ERROR -Log4j.appender.file2.layout =Org.apache.log4j.PatternLayout -Log4j.appender.file2.layout.ConversionPattern =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n

3. Testing

1  Packagetest;2 3 ImportOrg.apache.log4j.Logger;4 5  Public classTest {6     Private Final StaticLogger Logger = Logger.getlogger (Test.class);7 8      Public Static voidMain (string[] args) {9         //TODO auto-generated Method StubTenSystem.out.println ("Test begin!"); OneLogger.debug ("1. Debug "); ALogger.info ("2. Info "); -Logger.warn ("3. Warn "); -Logger.error ("4. Error "); theLogger.fatal ("5. Fatal "); -     } -}

Iv. How to configure XML files

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE log4j:configuration Public "-//apache//dtd log4j 1.2//en" "Log4j.dtd" ><log4j:configuration xmlns:log4j= "http://jakarta.apache.org/log4j/" configdebug= "true" > <appender name= "CONSOLE"class= "Org.apache.log4j.ConsoleAppender" > <layoutclass= "Org.apache.log4j.PatternLayout" > <param name= "Conversionpattern" value= "[%-5p]%d [%x{track}] [%x{model }]%m [%t][%c:%l]%n]/> </layout> <filterclass= "Org.apache.log4j.varia.LevelRangeFilter" > <param name= "levelmin" value= "DEBUG"/> <para M name= "Acceptonmatch" value= "true"/> </filter> </appender> <appender name= "ERROR"class= "Org.apache.log4j.DailyRollingFileAppender" > <param name= "datepattern" value= "'. ' Yyyy-mm-dd-hh "/> <param name=" File "value=" Logs/error.log "/> <param name=" Encoding "value=" UTF-8 "/> <layoutclass= "Org.apache.log4j.PatternLayout" > <param name= "Conversionpattern" value= "[%-5p]%d [%x{track}] [%x{model }]%m [%t][%c:%l]%n]/> </layout> <filterclass= "Org.apache.log4j.varia.LevelRangeFilter" > <param name= "Levelmax" value= "FATAL"/> <para    M name= "Levelmin" value= "ERROR"/> </filter> </appender> <!--info.log file output only info-- <appender name= "INFO"class= "Org.apache.log4j.DailyRollingFileAppender" > <param name= "datepattern" value= "'. ' Yyyy-mm-dd-hh "/> <param name=" File "value=" Logs/info.log "/> <param name=" Encoding "value=" UTF-8 " /> <layoutclass= "Org.apache.log4j.PatternLayout" > <param name= "Conversionpattern" value= "[%-5p]%d [%x{track}] [%x{model }]%m [%t][%c:%l]%n]/> </layout> <filterclass= "Org.apache.log4j.varia.LevelRangeFilter" > <param name= "Levelmax" value= "INFO"/> <param Name= "Levelmin" value= "INFO"/> </filter> </appender> <appender name= "WARN"class= "Org.apache.log4j.DailyRollingFileAppender" > <param name= "datepattern" value= "'. ' Yyyy-mm-dd-hh "/> <param name=" File "value=" Logs/warn.log "/> <param name=" Encoding "value=" UTF-8 " /> <layoutclass= "Org.apache.log4j.PatternLayout" > <param name= "Conversionpattern" value= "[%-5p]%d [%x{track}] [%x{model }]%m [%t][%c:%l]%n]/> </layout> <filterclass= "Org.apache.log4j.varia.LevelRangeFilter" > <param name= "Levelmax" value= "WARN"/> <param Name= "Levelmin" value= "WARN"/> </filter> </appender> <appender name= "DEBUG"class= "Org.apache.log4j.DailyRollingFileAppender" > <param name= "datepattern" value= "'. ' Yyyy-mm-dd-hh "/> <param name=" File "value=" Logs/debug.log "/> <param name=" Encoding "value=" UTF-8 "/> <layoutclass= "Org.apache.log4j.PatternLayout" > <param name= "Conversionpattern" value= "[%-5p]%d [%x{track}] [%x{model }]%m [%t][%c:%l]%n]/> </layout> <filterclass= "Org.apache.log4j.varia.LevelRangeFilter" > <param name= "Levelmax" value= "DEBUG"/> <para M name= "Levelmin" value= "DEBUG"/> </filter> </appender> <root> <priority value = "Debug"/> <appender-ref ref= "Debug"/> <appender-ref ref= "INFO"/> <appender-ref ref = "WARN"/> <appender-ref ref= "ERROR"/> <appender-ref ref= "CONSOLE"/> </root></log4 J:configuration>

Common uses of log4j

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.