log configuration

Original link: http://blog.sina.com.cn/s/blog_520998920100af7h.html

 

1. Configure the root Logger

        Its syntax is:
        log4j.rootLogger = [ level ] , appenderName1, appenderName2, …
        level : is the priority of logging, which is divided into OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL or the level you define. Log4j recommends using only four levels, the priority from high to low is ERROR, WARN, INFO, DEBUG. With the level defined 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 log information in the application will not be printed. appenderName: is to specify where the log information is output to. You can specify multiple output destinations at the same time.
       For example: log4j.rootLogger=info,A1,B2,C3

2. Configure the 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 (
            generates a log file every day)
            4.org.apache.log4j.RollingFileAppender (When the file size reaches the specified size, a new file is generated)
            5.org.apache.log4j.WriterAppender (send log information to any specified place in stream format)

               1. ConsoleAppender option
                      Threshold=WARN: Specifies the lowest level of output of log messages.
                      ImmediateFlush=true: The default value is true, which means that all messages will be output immediately.
                      Target=System.err: The default is: System.out, specify the output console
                2.FileAppender option
                      Threshold=WARN: Specify the lowest level of output log messages.
                      ImmediateFlush=true: The default value is true, which means that all messages will be output immediately.
                      File=mylog.txt: Specifies that messages are output to the mylog.txt file.
                      Append=false: The default value is true, which means adding the message to the specified file, and false means overwriting the content of the specified file with the message.
              3.DailyRollingFileAppender option
                      Threshold=WARN: Specifies the lowest level of output of log messages.
                      ImmediateFlush=true: The default value is true, which means that all messages will be output immediately.
                      File=mylog.txt: Specifies that messages are output to the mylog.txt file.
                      Append=false: The default value is true, which means adding the message to the specified file, and false means overwriting the content of the specified file with the message.
                      DatePattern=''.''yyyy-ww: Scroll the file once a week, that is, generate a new file every week. Of course, you can also specify months, weeks, days, hours and minutes. That is, 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 day5
                      )''.''yyyy-MM-dd-HH: every hour6
                      )''.''yyyy-MM-dd-HH-mm: every minute
              4.RollingFileAppender option
                      Threshold=WARN: Specifies the lowest level of output for log messages.
                      ImmediateFlush=true: The default value is true, which means that all messages will be output immediately.
                      File=mylog.txt: Specifies that messages are output to the mylog.txt file.
                      Append=false: The default value is true, which means adding the message to the specified file, and false means overwriting the content of the specified file with the message.
                      MaxFileSize=100KB: The suffix can be KB, MB or GB. When the log file reaches this size, it will be automatically rolled, that is, the original content will be moved to the mylog.log.1 file.
                      MaxBackupIndex=2: Specifies the maximum number of rolling files that can be generated.

3. Configure the format of the 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 four formats:
                 1 .org.apache.log4j.HTMLLayout (layout in HTML form),
           2.org.apache.log4j.PatternLayout (layout patterns can be flexibly specified ), 3.org.apache.log4j.SimpleLayout (
           level and Information string),
           4.org.apache.log4j.TTCCLayout (including log generation time, thread, category, etc.)
                     1.HTMLLayout option
                        LocationInfo=true: the default value is false, output java file name and line number
                        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, 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 needs to be explained here is the meaning of several symbols in the log information format:
             -X number: X information is left-aligned when outputting;
                     %p: The priority of output log information, namely DEBUG, INFO, WARN, ERROR, FATAL,
                     %d: The date or time of the log time point, the default format is ISO8601, you can also specify the format after it, for example: %d{yyy MMM dd HH:mm:ss,SSS}, the output is similar to : October 18, 2002, 22:10:28, 921
                     %r: The number of milliseconds it takes to output the log information from the application startup to outputting the log information
                     %c: The category to which the log information belongs, usually the full name of the class it belongs to
                     %t : Output the thread name that generated the log event
                     %l: Output the location of the log event, which is equivalent to the combination of %C.%M(%F:%L), including the category name, the thread that occurred, and the code in the code Rows. Example: Testlog4.main(TestLog4.java:10)
                     %x: Output the NDC (Nested Diagnostic Context) associated with the current thread, especially used in multi-client multi-threaded applications like java servlets.
                     %%: output a "%" character
                     %F: output the name of the file where the log message was generated
                     %L: output the line number in the code
                     %m: output the message specified in the code, the log specific information generated
                     %n: output a Carriage return line feed, Windows platform is "
", Unix platform is "
" Output log information newline
              can add modifiers between % and pattern characters to control its 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 name of the category is less than 20, it will be right-aligned by default.
                       2)%-20c: Specify the name of the output category. The minimum width is 20. If the name of the category is less than 20, the "-" sign specifies left alignment.
                       3)%.30c: Specify the name of the output category, the maximum width is 30, if the name of the category is greater than 30, the extra characters on the left will be truncated, but if it is less than 30, there will be no spaces.
                       4)%20.30c: If the name of the category is less than 20, fill it with spaces and align it to the right. If the name of the category is longer than 30 characters, it will be truncated from the left character.

 

 

 

more detailed example

 

log4j.rootLogger = INFO, appAppender, logfile, MAIL
log4j.addivity.org.apache = true

#ConsoleAppender, Console Output
#FileAppender, File Log Output
#SMTPAppender, Email 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
log file path
log4j.appender.logfile.Append = true   
# The default is true, add to the end, false is overwritten at each startup
log4j.appender.logfile.MaxFileSize = 1MB
# The size of a log file, more than This size will generate another log # KB, MB, GB
log4j.appender.logfile.MaxBackupIndex = 3  
# save up to 3 files for backup
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 lines
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 ##default is true print category name
# log4j.appender.logfile .layout.ContextPrinting = false ##defaults to true to print context information
# log4j.appender.logfile.layout.ThreadPrinting = false ##defaults to true to print thread name
# The printing information is as follows:
# 2007-09-13 14:45:39 , 765 [http - 8080 - 1 ] ERROR com.poxool.test.test - error Successfully closed 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 is true, add to the end, false is overwritten at each startup
log4j.appender.errorlogfile.ImmediateFlush = true   #Direct
output, no caching
# ' . ' 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 every day at midnight and noon
# ' . ' yyyy - MM - dd - HH: Update a log every hour
# ' . ' yyyy - MM - dd - HH - mm: Update a 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 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

#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 %-5p % c %x - %m%n ' )
log4j.appender.DATABASE.layout = org.apache.log4j.PatternLayout
log4j.appender.DATABASE.layout.ConversionPattern =% d - %c -%- 4r [% t] %- 5p % c % x - % m % n
#There will 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, which can avoid inserting a link to the database once

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326292948&siteId=291194637