Log4j configuration information

Source: Internet
Author: User
First, create a new log4j. properties file in the project classes;

In actual programming, you must define the configuration file before making log4j really run in the system. The definition step is to use logger, appender, and layout respectively. Log4j supports two configuration file formats: XML and Java properties (Key = value) [Java feature file (Key = value )]. (Only the properties file is described here)

1. Configure the root Logger

Its syntax is:
Log4j. rootlogger = [level], appendername1, appendername2 ,...
Level: indicates the log record priority, which can be off, fatal, error, warn, info, debug, all, or the level you define. We recommend that you use only four levels of log4j. The priority ranges from high to low: error, warn, info, and debug. By defining the level here, you can control the switch to the corresponding level of log information in the application. For example, if the info level is defined here, all debug-level logs in the application will not be printed. Appendername: specifies where the log information is output. You can specify multiple output destinations at the same time.
Example: log4j. rootlogger = info, A1, B2, C3

2. Configure the log output destination

Its syntax is:
Log4j. appender. appendername = fully. Qualified. Name. Of. appender. Class //
"Fully. Qualified. Name. Of. appender. Class" can be used to specify one of the following five destinations:

1.org. Apache. log4j. leleappender (console)
2.org. Apache. log4j. fileappender (file)
3.org. Apache. log4j. dailyrollingfileappender (a log file is generated every day)
4.org. Apache. log4j. rollingfileappender (a new file is generated when the file size reaches the specified size)
5.org. Apache. log4j. writerappender (send log information to any specified place in stream format)

1. leleappender options
Threshold = warn: specify the minimum output level of log messages.
Immediateflush = true: the default value is true, meaning that all messages will be output immediately.
Target = system. Err: system. Out by default, which specifies the output Console
2. fileappender options
Threshold = warn: specify the minimum output level of log messages.
Immediateflush = true: the default value is true, meaning that all messages will be output immediately.
File1_mylog.txt: refers to the output to the mylog.txt file.
Append = false: The default value is true. To add a message to a specified file, false means to overwrite the specified file content.
3. dailyrollingfileappender Option
Threshold = warn: specify the minimum output level of log messages.
Immediateflush = true: the default value is true, meaning that all messages will be output immediately.
File1_mylog.txt: refers to the output to the mylog.txt file.
Append = false: The default value is true. To add a message to a specified file, false means to overwrite the specified file content.
Datepattern = ''.'' yyyy-ww: The file is rolled every week, that is, a new file is generated every week. You can also specify monthly, weekly, daily, and hour/minute. The corresponding format is as follows:
1) ''.'' yyyy-MM: monthly
2) ''.'' yyyy-ww: weekly
3) ''.'' yyyy-mm-DD: daily
4) ''.'' yyyy-mm-dd-A: twice a day
5) ''.'' yyyy-mm-dd-hh: Hourly
6) ''.'' yyyy-mm-dd-hh-MM: minute
4. rollingfileappender options
Threshold = warn: specify the minimum output level of log messages.
Immediateflush = true: the default value is true, meaning that all messages will be output immediately.
File1_mylog.txt: refers to the output to the mylog.txt file.
Append = false: The default value is true. To add a message to a specified file, false means to overwrite the specified file content.
Maxfilesize = 100kb: The suffix can be kb, MB, or GB. When the log file reaches this hour, it will automatically scroll, moving the original content to the mylog. log.1 file.
Maxbackupindex = 2: specify the maximum number of rolling files that can be generated.

3. Configure the log information format

Its syntax is:
1). log4j. appender. appendername. layout = fully. Qualified. Name. Of. layout. Class
"Fully. Qualified. Name. Of. layout. Class" can be used to specify one of the following four formats:
1.org. Apache. log4j. htmllayout (in the form of HTML tables ),
2.org. Apache. log4j. patternlayout (you can flexibly specify the layout mode ),
3.org. Apache. log4j. simplelayout (including the log information level and information string ),
4.org. Apache. log4j. ttcclayout (including the log generation time, thread, category, and other information)
1. htmllayout options
Locationinfo = true: the default value is false. The Java file name and row number are output.
Title = My app file: The default value is log4j log messages.
2. patternlayout Option
Conversionpattern = % m % N: specify how to format the specified message.
3. xmllayout Option
Locationinfo = true: the default value is false, and the Java file and row number are output.
2). log4j. appender. a1.layout. conversionpattern = %-4r %-5 p % d {yyyy-mm-dd hh: mm: SSS} % C % m % N
Here, we need to explain the meanings of several symbols in the log information format:
-X: left aligned when X information is output;
% P: Priority of output log information, that is, debug, info, warn, error, fatal,
% D: date or time of the log output time point. The default format is iso8601. You can also specify the format after the time point, for example, % d {YYY Mmm dd hh: mm: SS, SSS}, output is similar to: October 18, 2002 22:10:28, 921
% R: The number of milliseconds it takes to output the log information from application startup to application startup.
% C: the category of the output log, usually the full name of the class.
% T: name of the thread that outputs the log event
% L: the location where the output log event occurs. It is equivalent to a combination of % C. % m (% F: % L), including the category name, the thread that occurs, and the number of lines in the code. Example: testlog4.main (testlog4.java: 10)
% X: NDC associated with the current thread (nested Diagnostic Environment), especially used in applications with multiple clients and multithreading such as Java Servlets.
%: Output A "%" Character
% F: name of the file in which the output Log message is generated
% L: the row number in the output code
% M: output the specific log information of the specified message in the code.
% N: Output A line break, which is"
", UNIX platform is"
"Line feed of output log information
You can add modifiers between the % and mode characters to control the alignment of the minimum width, maximum width, and text. For example:
1) % 20c: specify the name of the output category. The minimum width is 20. If the category name is smaller than 20, it is right aligned by default.
2) %-20C: specify the name of the output category. The minimum width is 20. If the category name is smaller than 20, the "-" sign specifies the left alignment.
3) %. 30C: specify the name of the output category. The maximum width is 30. If the category name is greater than 30, the extra characters on the left will be truncated, but there will be no spaces if the value is less than 30.
4) % Category 30C: If the category name is less than 20, fill in spaces and align right. If the category name is longer than 30 characters, cut off the characters sold on the left.

Detailed examples

Log4j. rootlogger = info, consoleappender, logfile, mail
Log4j.addivity.org. Apache = true

# Leleappender, console output
# Fileappender: file log output
# Smtpappender: Send mail output log
# Socketappender, socket log
# Nteventlogappender, window nt log
# Syslogappender,
# Jmsappender,
# Asyncappender,
# Nullappender

# File output: rollingfileappender
# Log4j. rootlogger = info, logfile
Log4j. appender. logfile = org. Apache. log4j. rollingfileappender
Log4j. appender. logfile. Threshold = info
# Output the above info Information
Log4j. appender. logfile. File = info_log.html
# Save the log file path
Log4j. appender. logfile. append = true
# The default value is true, which is added to the end, and false is overwritten at each startup.
Log4j. appender. logfile. maxfilesize = 1 MB
# The size of a log file. If the size is exceeded, another log is generated # kb, MB, GB
Log4j. appender. logfile. maxbackupindex = 3
# Saving up to three file backups
Log4j. appender. logfile. layout = org. Apache. log4j. htmllayout
# Output file format
Log4j. appender. logfile. layout. locationinfo = true
# Whether to display the Class Name and number of rows
Log4j. appender. logfile. layout. Title = title: \ u63d0 \ u9192 \ u60a8 \ uff1a \ u7cfb \ u7edf \ u53d1 \ u751f \ u4e86 \ Users \ u91cd \ u9519
# <Title>
############################ Samplelayout ######### ###########################
# Log4j. appender. logfile. layout = org. Apache. log4j. samplelayout
############################ Patternlayout ######### ##########################
# Log4j. appender. logfile. layout = org. Apache. log4j. patternlayout
# Log4j. appender. logfile. layout. conversionpattern = % d % P [% C]-% m % N % d
############################# Xmllayout ######### ##############################
# Log4j. appender. logfile. layout = org. Apache. log4j. xmllayout
# Log4j. appender. logfile. layout. locationinfo = true # Whether to display the Class Name and number of rows
############################ Ttcclayout ######### #############################
# Log4j. appender. logfile. layout = org. Apache. log4j. ttcclayout
# Log4j. appender. logfile. layout. dateformat = iso8601
# Null, relative, absolute, date or iso8601.
# Log4j. appender. logfile. layout. timezoneid = GMT-8: 00
# Log4j. appender. logfile. layout. categoryprefixing = false # The default value is true. Print the category name.
# Log4j. appender. logfile. layout. contextprinting = false # The default value is true to print the context information.
# Log4j. appender. logfile. layout. threadprinting = false # The default value is true to print the thread name.
# Print the following information:
#2007-09-13 14: 45: 39,765 [http-8080-1] error com. poxool. Test. Test-error successfully closes the link
######################################## #######################################
# Daily file output: dailyrollingfileappender
# Log4j. rootlogger = info, errorlogfile
Log4j. appender. errorlogfile = org. Apache. log4j. dailyrollingfileappender
Log4j. appender. errorlogfile. Threshold = Error
Log4j. appender. errorlogfile. File = ../logs/error_log
Log4j. appender. errorlogfile. append = true
# The default value is true, which is added to the end, and false is overwritten at each startup.
Log4j. appender. errorlogfile. immediateflush = true
# Direct output without caching
# '. 'Yyyy-MM: updates a log every month.
# '. 'Yyyy-ww: updates a log every week.
# '. 'Yyyy-mm-DD: updates a log every day.
# '. 'Yyyy-mm-dd-A: updates a log every day at midnight and midday.
# '. 'Yyyy-mm-dd-hh: updates a log every hour.
# '. 'Yyyy-mm-dd-hh-MM: updates a log every minute.
Log4j. appender. errorlogfile. datepattern = '. 'yyyy-mm-dd'. Log'
# File name format
Log4j. appender. errorlogfile. layout = org. Apache. log4j. patternlayout
Log4j. appender. errorlogfile. layout. conversionpattern = % d % P [% C]-% m % N % d

# Console output:
# Log4j. rootlogger = info, consoleappender
Log4j. appender. consoleappender = org. Apache. log4j. leleappender
Log4j. appender. leleappender. Threshold = Error
Log4j. appender. leleappender. layout = org. Apache. log4j. patternlayout
Log4j. appender. leleappender. layout. conversionpattern = % d %-5 p % m % N
Log4j. appender. leleappender. immediateflush = true

# Direct output without caching
Log4j. appender. leleappender. Target = system. Err
# The default output is system. Out.

# Send an email: smtpappender
# Log4j. rootlogger = info, mail
Log4j. appender. Mail = org.apache.log4j.net. smtpappender
Log4j. appender. Mail. Threshold = info
Log4j. appender. Mail. buffersize = 10
Log4j. appender. Mail. From = [email protected]
Log4j. appender. Mail. smtphost = smtp.gmail.com
Log4j. appender. Mail. Subject = log4j message
Log4j. appender. Mail. To = [email protected]
Log4j. appender. Mail. layout = org. Apache. log4j. patternlayout
Log4j. appender. Mail. layout. conversionpattern = % d-% C-%-4r [% T] %-5 p % C % x-% m % N

# Database: jdbcappender
Log4j. appender. Database = org. Apache. log4j. JDBC. jdbcappender
Log4j. appender. database. url = JDBC: oracle: thin: @ 210.51. 173.94: 1521: ydb
Log4j. appender. database. Driver = oracle. JDBC. Driver. oracledriver
Log4j. appender. database. User = ydbuser
Log4j. appender. database. Password = ydbuser
Log4j. appender. database. SQL = insert into A1 (title3) values ('% d-% C %-5 p % C % x-% m % n ')
Log4j. appender. database. layout = org. Apache. log4j. patternlayout
Log4j. appender. database. layout. conversionpattern = % d-% C-%-4r [% T] %-5 p % C % x-% m % N
# There may be a problem with the database link. You can rewrite the getconnection () of org. Apache. log4j. JDBC. jdbcappender to use the database link pool to get the link, so that you can avoid inserting a link to the database once.

Log4j configuration information

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.