Basic configuration and usage of log4j

Source: Internet
Author: User

Log4j is an open-source log component developed by Apache. log4j can control the delivery of log information to the console, files, and GUI components, and control the output format of each log; the configuration file can be used for flexible configuration to control the log generation process in greater detail. The following is a record of my basic usage of log4j.

1. First import the frame package of the log4j-1.2.14.jar into the project.

2. Create the log4j. properties configuration file under the src directory of the project (the log4j configuration file can also use the XML file ).

How to configure the log4j. properties File

Log4j. rootlogger = info, stdout, R

# Configure the root logger. info indicates the logger level (including off, fatal, error, warn, info, debug, and all ).

# Or a Custom Level. We recommend that you use only four levels of log4j. The priority ranges from high to low: error, warn, info, and debug. # The level defined here, you can control the switch to the corresponding level of log information in the application. For example, info is defined here.

# No. logs of all debug levels in the application will not be printed if they are equal to or higher than this level.

#) Stdout and R are the places where the log files are to be output. They can have multiple names that are customized.

 

 

Log4j. appender. stdout = org. Apache. log4j. leleappender

# Define org. Apache. log4j. leleappender for the above stdout to output the log information to the console.

# Log4j provides the following appender types:
# Org. Apache. log4j. leleappender (console ),
# Org. Apache. log4j. fileappender (file ),
# Org. Apache. log4j. dailyrollingfileappender (a log file is generated every day ),
# Org. Apache. log4j. rollingfileappender (a new file is generated when the file size reaches the specified size ),
# Org. Apache. log4j. writerappender (send log information to any specified place in stream format)

 

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

# Log4j provides the following layout types:
# Org. Apache. log4j. htmllayout (in the form of HTML tables ),
# Org. Apache. log4j. patternlayout (you can flexibly specify the layout mode ),
# Org. Apache. log4j. simplelayout (including the log information level and information string ),
# Org. Apache. log4j. ttcclayout (including the log generation time, thread, category, and other information)

# Log4j. appender. stdout. layout. conversionpattern = [QC] % P [% T] % C. % m (% L) | % m % N

 

Log4j. appender. r = org. Apache. log4j. dailyrollingfileappender

 

 

Log4j. appender. R. File = logs. Log

# Specify the log file output path (logs. log is under the project directory)

 

 

Log4j. appender. R. layout = org. Apache. log4j. patternlayout

# You can use log4j. appender. R. maxfilesize = 100kb to set the file size, or use # log4j. appender. R. maxbackupindex = 1 to save a backup file.

Log4j. appender. R. layout. conversionpattern = % d-[ts] % P % T % C-% m % N

# Formatting log information
# Log4j uses a printing format similar to the printf function in C language to format log information. The print parameters are as follows:
# % M output the specified message in the code
# % P output priority, namely debug, info, warn, error, fatal
# % R the number of milliseconds it takes to output the log information from application startup to output
# % C output category, usually the full name of the class
# % T name of the thread that outputs the log event
# % N output a carriage return line break. For Windows, the return value is "RN", and for UNIX, the return value is "N"
# % D date or time of the log output time point. The default format is iso8601. You can also specify the format after it, for example, % d {yyyy mmm # dd hh: mm: SS, SSS}, output is similar to: October 18, 2002 22:10:28, 921
# % L location of log event output, including category name, thread, and number of lines in the code.

 

 

Log4j. Logger. log4j = warn

# Set the level of all classes in the log4j package to warn.

Create a new log4j package in the project and write a test testlog. Java in this package.

PackageLog4j;

 

ImportOrg. Apache. log4j. level;

ImportOrg. Apache. log4j. Logger;

Public ClassTestlog {

Public Static VoidMain (string [] ARGs ){

Logger log = logger.Getlogger(Testlog.Class);

Log. setlevel (level.Info); // Sets the log level to info, and in the configuration file, // configure log4j. Logger. log4j = warn. Only the log information in if can be printed out.

 

System.Out. Println (log. getlevel ());

If(Log. isinfoenabled ()){

Log. Warn ("this is warn ");

Log.info ("this is Info ");

Log. Error ("this is error ");

}

}

}

 

These are just some simple applications. The proper and skillful use of log4j will make your program more nice. You can refer to other materials to learn more about 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.