Configuration information for log4j

Source: Internet
Author: User

First, a new log4j.properties file can be created in the classes of the project;

In the actual programming, in order to make the log4j really run in the system in advance also to define the configuration file. The defining step is to use the logger, Appender, and layout separately. LOG4J supports two configuration file formats, one in XML format and one in Java properties (key=value) Java attribute file (key = value). (Only the properties file is indicated here)

1. Configure Root Logger

Its syntax is:
Log4j.rootlogger = [level], appenderName1, appenderName2, ...
Level: Is the priority of logging and is divided into off, FATAL, ERROR, WARN, INFO, DEBUG, all, or levels you define. LOG4J recommends using only four levels, with the priority from high to low being error, WARN, INFO, DEBUG. By defining the level here, you can control the switch to the appropriate level of log information in your application. For example, if the info level is defined here, the log information for all debug levels in the application will not be printed. Appendername: Specifies where the log information is to be exported. You can specify multiple output destinations at the same time.
Example: LOG4J.ROOTLOGGER=INFO,A1,B2,C3

2. Configure log information Output destination

Its syntax is:
Log4j.appender.appenderName = fully.qualified.name.of.appender.class//
"Fully.qualified.name.of.appender.class" can specify one of the following five destinations:

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)

1.ConsoleAppender Options
Threshold=warn: 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
2.FileAppender Options
Threshold=warn: 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.
3.DailyRollingFileAppender Options
Threshold=warn: 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.
Datepattern= "." YYYY-WW: Scrolls a file once a week, which results in a new file every week. You can also specify monthly, weekly, Days, hours, and minutes. The corresponding format is as follows:
1) "." YYYY-MM: Monthly
2) "." YYYY-WW: Weekly
3) "." YYYY-MM-DD: Every day
4) "." Yyyy-mm-dd-a: two times a day
5) "." YYYY-MM-DD-HH: Per hour
6) "." YYYY-MM-DD-HH-MM: Per minute
4.RollingFileAppender Options
Threshold=warn: 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.

3. Format of configuration log information

Its syntax is:
1). Log4j.appender.appenderName.layout = Fully.qualified.name.of.layout.class
"Fully.qualified.name.of.layout.class" can specify one of the following 4 formats:
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
1.HTMLLayout Options
Locationinfo=true: Default value is false, output Java file name and line number
Title=my app File: The default value is log4j Log Messages.
2.PatternLayout Options
conversionpattern=%m%n: Specifies how the specified message is formatted.
3.XMLLayout Options
Locationinfo=true: Default value is false, output Java file and line number
2). log4j.appender.a1.layout.conversionpattern=%-4r%-5p%d{yyyy-mm-dd HH:mm:ssS}%c%m%n
What you need to explain here is 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 newline character, the Windows platform is "
", the UNIX platform is"
"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 exported from the left hand, the characters are cut off

A more detailed example

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

#ConsoleAppender, console output
#FileAppender, file log output
#SMTPAppender, outgoing mail output log
#SocketAppender, Socket log
#NTEventLogAppender, Window NT log
#SyslogAppender,
#JMSAppender,
#AsyncAppender,
#NullAppender

#文件输出: Rollingfileappender
#log4j. Rootlogger = Info,logfile
Log4j.appender.logfile = Org.apache.log4j.RollingFileAppender
Log4j.appender.logfile.Threshold = INFO
# Output the above info info
Log4j.appender.logfile.File = info_log.html
#保存log文件路径
Log4j.appender.logfile.Append = True
# default is True, add to end, false overwrite on each boot
Log4j.appender.logfile.MaxFileSize = 1MB
# The size of a log file, more than this size will generate 1 log # KB, MB,GB
Log4j.appender.logfile.MaxBackupIndex = 3
# Save up to 3 files in a backup
Log4j.appender.logfile.layout = Org.apache.log4j.HTMLLayout
# Format of output file
Log4j.appender.logfile.layout.LocationInfo = True
#是否显示类名和行数
Log4j.appender.logfile.layout.Title =title:\u63d0\u9192\u60a8\uff1a\u7cfb\u7edf\u53d1\u751f\u4e86\u4e25\u91cd\ U9519\u8bef
#html页面的 < 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 #是否显示类名和行数
############################## 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 # #默认为true Print class Alias
# log4j.appender.logfile.layout.ContextPrinting = False # #默认为true Printing context information
# log4j.appender.logfile.layout.ThreadPrinting = false # #默认为true Print thread name
# Print information as follows:
#2007 -09-13 14:45:39, 765 [http-8080-1] ERROR com.poxool.test.test-error successfully closed link
###############################################################################
#每天文件的输出: 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
#默认为true, add to end, false overwrite on each start
Log4j.appender.errorlogfile.ImmediateFlush = True
#直接输出, do not cache
# ‘ . ' YYYY-MM: Update a log log every month
# ‘ . ' YYYY-WW: Update a log log every week
# ‘ . ' YYYY-MM-DD: Update a log log every day
# ‘ . ' YYYY-MM-DD-A: Update a log log every day at midnight and noon
# ‘ . ' YYYY-MM-DD-HH: Update one log log per hour
# ‘ . ' YYYY-MM-DD-HH-MM: Update a log log every minute
Log4j.appender.errorlogfile.DatePattern = '. ' Yyyy-mm-dd '. Log '
#文件名称的格式
Log4j.appender.errorlogfile.layout = Org.apache.log4j.PatternLayout
Log4j.appender.errorlogfile.layout.ConversionPattern =%d%p [%c]-%m%n%d

#控制台输出:
#log4j. Rootlogger = Info,consoleappender
Log4j.appender.consoleAppender = Org.apache.log4j.ConsoleAppender
Log4j.appender.consoleAppender.Threshold = ERROR
Log4j.appender.consoleAppender.layout = Org.apache.log4j.PatternLayout
Log4j.appender.consoleAppender.layout.ConversionPattern =%d%-5p%m%n
Log4j.appender.consoleAppender.ImmediateFlush = True

# Direct output, no caching
Log4j.appender.consoleAppender.Target = System.err
# default is System.out mode output

#发送邮件: 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]%-5p%c%x-%m%n

#数据库: 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%-5p%c-%m%n ')
Log4j.appender.DATABASE.layout = Org.apache.log4j.PatternLayout
Log4j.appender.DATABASE.layout.ConversionPattern =% D-% c-%-4r [% t]%-5p% c% x-% m% n
#数据库的链接会有问题, you can rewrite Org.apache.log4j.jdbc.JDBCAppender's getconnection () to use the database link pool to get the link, you can avoid insert a link to the database

Http://www.cnblogs.com/zhuawang/p/3999035.html

Configuration information for log4j (RPM)

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.