Log4j Log Tool demo and configuration file instance

Source: Internet
Author: User
Tags log4j

The production of a log in the project is necessary for a project, now used in a more extensive, more popular log tools have log4j; This log tool is actually very convenient to use, and when used, it was created like Java.util.logging.Logger, The JDK log logging information is obtained and exported using the handler object, and the exported information is formatted by formatter object and log4j log information is imported, and the format is configured in the configuration file. Specifically look at the following:

1, the need to import the rack package: (1), Log4j.jar (2), Commons-pool.jar

2. Create a new project in Eclipse and create a log4j.properties file in the root directory of SRC, the filename must be log4j, here Configure the address of the log information export, and the definition of the exported format.

3, configuration file, configuration log information export address and format:

#log4j. Rootlogger = DEBUG, A1
#log4j. Rootlogger = INFO, a1,b1,c1
Log4j.rootlogger = ERROR, A1
#这里定义的是error级别的, in fact, with the info-class on it, anyway, there are 6 levels you look at using;

# log4j.rootlogger= After two parameters, you can also define multiple parameters, but the first is to define the level of the log, and the following is to define the log information export address such as A1,B1,C1 these are everywhere address

# Org.apache.log4j.ConsoleAppender Export log information to the console
#log4j. appender.a1 = Org.apache.log4j.ConsoleAppender
# Org.apache.log4j.FileAppender Export log information to a file (disk)

Log4j.appender.A1 = Org.apache.log4j.FileAppender

Log4j.appender.A1 = Org.apache.log4j.RollingFileAppender

#定义日志的存放路径和文件名;
Log4j.appender.A1.File = D:/log4j.log

log4j.appender.a1.maxfilesize=500kb
#当log文件大于500K的时候删除重新建;
log4j.appender.a1.maxbackupindex=10
Log4j.appender.A1.layout = Org.apache.log4j.PatternLayout
#使用的是自定义的日志格式, here are some of the custom parameters;
#log4j. Appender.A1.layout.ConversionPattern =%-4r [%t]%-5p%c%x-%m%n
Log4j.appender.A1.layout.ConversionPattern = [%d] [%t]%m%n

4. Test class

Package com.soufun;

Import Org.apache.log4j.Logger;

public class Testlog4j {

public void log4j () {

Logger Log=logger.getlogger (This.getclass (). GetName ());

System.out.println ("Test log4j");

Log.info ("testlog4j");

}


public static void Main (string[] args) {

testlog4j tl=new testlog4j ();

Tl.log4j ();

}

}

Here the simple log configuration is successful, and log information can be logged ...

log4j instance of configuration file

# INFO is the level of the log, there are Debug,info, warn,error,fatal and the following are output addresses

Log4j.rootlogger=info, stdout, R, L

# The log is made up of logger,appender and layout.

# Output to Console

Log4j.appender.stdout=org.apache.log4j.consoleappender

# Redefine Log level

Log4j.appender.stdout.threshold=info

# Control of Output color

Log4j.appender.stdout.Target = System.out

# defines the output format, i.e. layout defines the format of the output

Log4j.appender.stdout.layout=org.apache.log4j.patternlayout

# Output The format of the time

Log4j.appender.stdout.layout.conversionpattern=%d{yyyy-mm-dd HH\:MM\:SS} [%5p]-%c-%f (%L)-%m%n

# Output to file, specify output address

Log4j.appender.r=org.apache.log4j.rollingfileappender

# Redefine output level, only works on R

Log4j.appender.r.threshold=info

# The output file is specified

Log4j.appender.r.file=f\:/ebooking.log

# define output format

Log4j.appender.r.layout=org.apache.log4j.patternlayout

# define Time format

Log4j.appender.r.layout.conversionpattern=%d{yyyy-mm-dd HH\:MM\:SS} [%5p]-%c-%f (%l)-%m%n

# defines the size of the storage log file

Log4j.appender.r.maxfilesize=1mb

# The log output log is defined

Log4j.appender.l=org.apache.log4j.dailyrollingfileappender

# Defines the level of the log

Log4j.appender.l.threshold=info

# define log output file name and path

Log4j.appender.l.file=f\:/ebooking_error.log

Log4j.appender.l.datepattern= ' _ ' yyyy-mm-dd '. Log '

# define Log Output format

Log4j.appender.l.layout=org.apache.log4j.patternlayout

# Format of time in output log

Log4j.appender.l.layout.conversionpattern=%d{yyyy-mm-dd HH\:MM\:SS} [%5p]-%c-%f (%l)-%m%n


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.