The function of log4j.properties and its use method

Source: Internet
Author: User
Tags log4j

First, the role of log4j.properties

LOG4J is an Apache open source project, by using log4j, we can control the destination of log information delivery is console, file, GUI component, even socket server, NT Event recorder, Unixsyslog daemon, etc. We can also control the output format of each log, and by defining the level of each log information, we can control the log generation process more carefully. Most interesting of all, these can be configured flexibly with a single configuration file, without the need to modify the code of the application.

In addition, by log4j other language interfaces, you can use log4j in C, C + +,. Net, PL/SQL programs with syntax and usage as in Java programs, enabling a unified, consistent Log component module for multilingual distributed systems. and by using a variety of third-party extensions, you can easily integrate log4j into a Java EE, Jini, or even SNMP application. (Transferred from: http://blog.sina.com.cn/s/blog_761110d701017yuu.html)

Second, log4j.properties use method
. Description of the parameter meaning:
Types of output levels
ERROR, WARN, INFO, DEBUG
Error is a critical error, mainly a program
WARN for general warnings, such as session loss
Info is the general information to display, such as login log out
Debug information for the program
Configure log information Output destination
Log4j.appender.appenderName = Fully.qualified.name.of.appender.class
1.org.apache.log4j.consoleappender (console)
2.org.apache.log4j.fileappender (file)
3.org.apache.log4j.dailyrollingfileappender (Generate a log file every day)
4.org.apache.log4j.rollingfileappender (creates a new file when the file size reaches the specified size)
5.org.apache.log4j.writerappender (send log information in stream format to any specified location)
Configure the format of log information
Log4j.appender.appenderName.layout = Fully.qualified.name.of.layout.class
1.org.apache.log4j.htmllayout (Layout in HTML table Form),
2.org.apache.log4j.patternlayout (flexibility to specify layout mode),
3.org.apache.log4j.simplelayout (contains the level of log information and the information string),
4.org.apache.log4j.ttcclayout (contains information about the time, thread, category, etc.) of the log
Console options
Threshold=debug: Specifies the lowest level of output for log messages.
Immediateflush=true: The default value is true, meaning that all messages will be output immediately.
Target=system.err: By default: System.out, specify output console
Fileappender Options
THRESHOLD=DEBUF: Specifies the lowest level of output for log messages.
Immediateflush=true: The default value is true, meaning that all messages will be output immediately.
File=mylog.txt: Specifies the message output to the Mylog.txt file.
Append=false: The default value is True, the message is added to the specified file, and false refers to overwriting the message with the specified file content.
Rollingfileappender Options
Threshold=debug: Specifies the lowest level of output for log messages.
Immediateflush=true: The default value is true, meaning that all messages will be output immediately.
File=mylog.txt: Specifies the message output to the Mylog.txt file.
Append=false: The default value is True, the message is added to the specified file, and false refers to overwriting the message with the specified file content.
MAXFILESIZE=100KB: The suffix can be kb, MB, or GB. When the log file reaches this size, it will automatically scroll to move the original content to the Mylog.log.1 file.
maxbackupindex=2: Specifies the maximum number of scroll files that can be produced.
log4j.appender.a1.layout.conversionpattern=%-4r%-5p%d{yyyy-mm-dd HH:mm:ssS}%c%m%n
The meaning of several symbols in the log Information format:
-X: Left-aligned when the information is output;
%p: Output log information priority, i.e. Debug,info,warn,error,fatal,
%d: the date or time of the output log time, the default format is ISO8601, can also be specified after the format, such as:%d{yyy MMM dd hh:mm:ss,sss}, output similar: October 18, 2002 22:10:28,921
%r: The number of milliseconds to output the log information from the application boot to output
%c: The class in which the output log information belongs, usually the full name of the class in which it is located
%t: Output The name of the thread that generated the log event
%l: The location of the output log event, which corresponds to the combination of%c.%m (%f:%l), including the class name, the thread that occurred, and the number of rows in the code. Example: Testlog4.main (testlog4.java:10)
%x: The NDC (nested diagnostic environment) associated with the output and current line threads, especially for multi-client multithreaded applications such as Java Servlets.
Percent: Output a "%" character
%F: The name of the file where the output log message was generated
%l: Line numbers in the output code
%M: The specified message in the output code, resulting in the log specific information
%n: Output a carriage return line break, Windows platform is "\ r \ n", Unix platform for "\ n" Output log information line-wrapping
You can add modifiers between% and pattern characters to control their minimum width, maximum width, and text alignment. Such as:
1)%20c: Specify the name of the output category, the minimum width is 20, if the category name is less than 20, the default is the right alignment.
2)%-20c: Specify the name of the output category, the minimum width is 20, if the category name is less than 20, the "-" number specifies left-aligned.
3)%.30c: Specify the name of the output category, the maximum width is 30, if the category name is greater than 30, will be the left more than the character of the cut off, but less than 30, there will be no spaces.

4)%20.30c: If the category name is less than 20, fill in the blanks, and right-aligned, if its name is longer than 30 characters, it is truncated from the far left character.

Three, the common configuration demo

# DEBUG < INFO < WARN < ERROR < FATAL
# Global logging configuration
log4j.rootlogger=info, Stdout,fil E
# My logging configuration ...
#log4j. Logger.com.tocersoft.school=debug
#log4j. Logger.net.sf.hibernate.cache=debug
# Console output ...
Log4j.appender.stdout=org.apache.log4j.consoleappender
log4j.appender.stdout.layout= Org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.conversionpattern=%5p%d%c:%m%n

Log4j.appender.file=org.apache.log4j.fileappender
log4j.appender.file.file=. /logs/iask.log
log4j.appender.file.layout=org.apache.log4j.patternlayout
Log4j.appender.file.layout.conversionpattern=%d{yyyy-mm-dd HH:mm:ss}  %l  %m%n

Log4j.logger.org.apache.activemq.spring=warn
#see Cache info
log4j.logger.org.hibernate.cache=info




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.