Log4j file configuration tutorial Daquan

Log4j file configuration tutorial Daquan~


Log4J's configuration file (Configuration File) is used to set the level, storage and layout of the logger . It can receive settings in key=value format or setting information in xml format. Through configuration, you can create a Log4J runtime environment.


1. Introduction to the overall framework:

#Configure root Logger
log4j.rootLogger  =   [ level ]   ,  appenderName1 ,  appenderName2 ,  …

#Configure log information output destination Appender
log4j.appender.appenderName  =  fully.qualified.name.of.appender.class
  log4j.appender.appenderName.option1  =  value1
  …
  log4j.appender.appenderName.optionN = valueN

#Configure the format (layout) of the log information
    log4j.appender.appenderName.layout  =  fully.qualified.name.of.layout.class
  log4j.appender.appenderName.layout.option1  =  value1
  …
  log4j.appender.appenderName.layout.optionN = valueN
2. Log output level:
ERROR, WARN, INFO, DEBUG
ERROR is a serious error, mainly a program error
WARN is a general warning, such as session loss
INFO is a general information to be displayed, such as login and logout
DEBUG
configures the log information output destination for the debugging information of the program

Where [level] is the log output level, there are 5 levels:
FATAL      0  
ERROR      3  
WARN       4  
INFO       6  
DEBUG      7

3.  Configure the log information output destination

Appender is the log output destination. The appenders provided by Log4j are as follows:
org.apache.log4j.ConsoleAppender (console) ,
org.apache.log4j.FileAppender (file) ,
org.apache.log4j.DailyRollingFileAppender (generates a log file 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)
4. Configure the format of the log information:
Layout: Log output format. The layouts provided by Log4j are as follows:
org.apache.log4j.HTMLLayout (layout in HTML table format) ,
org.apache.log4j.PatternLayout (the layout pattern can be specified flexibly),
org.apache.log4j. SimpleLayout (contains the level and information string of log information), 
org.apache.log4j.TTCCLayout (contains log generation time, thread, category, etc.)
5. The meanings represented by several symbols in the log information format:
Print parameters: Log4J uses a print format similar to the printf function in C language to format log information, as follows:
   %m prints the message specified in the code
  %p output priority, ie DEBUG, INFO, WARN, ERROR, FATAL
  %r The number of milliseconds it took to output the log information since the application was started
  %c The category to which the output belongs, usually the full name of the category in which it belongs
  %t output the name of the thread that generated the log event
  %n outputs a carriage return and line feed, "\r\n" for Windows platforms and "\n" for Unix platforms
  %d Output 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  
  %l Output where the log event occurred, including the category name, the thread in which it occurred, and the number of lines in the code. Example: Testlog4.main(TestLog4.java: 10 )

6. Options common to output destinations

Console common options
Threshold=DEBUG: 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: By default: System.out, specifies the output console

FileAppender options
Threshold=DEBUF: 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. (The difference between adding and covering)

RollingFileAppender option
Threshold=DEBUG: 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.
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 mylog.lo


 7. easy sample~ 
 
### set log levels ###
log4j.rootLogger = debug ,  stdout ,  D ,  E

### Output to console ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =  %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n

### Output to log file ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG ## Output logs above DEBUG level
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### Save exception information to a separate file ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/error.log ## Exception log file name
log4j.appender.D.Append = true
log4j.appender.D.Threshold = ERROR ## Only output logs above ERROR level!!!
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
Use in code:
public   class  TestLog4j  {
     public   static   void  main(String[] args)  {
        PropertyConfigurator.configure( " D:/Code/conf/log4j.properties " );
        Logger logger = Logger.getLogger (TestLog4j. Class);
        logger.debug( " debug " );
        logger.error( " error " );
    }
}

8  another sample 

 
 
log4j.rootLogger=DEBUG,A1,R
# log4j.rootLogger = INFO, A1, R
# ConsoleAppender output
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m%n

# File outputs one file a day, the output path can be customized, usually under the root path
log4j.appender.R = org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=blog_log.txt
log4j.appender.R.MaxFileSize=500KB
log4j.appender.R.MaxBackupIndex=10
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%n

9. sample3

The Log4J configuration file given below implements a full set of functions such as outputting to the console, files, rolling back files, sending log emails, outputting to database log tables, and custom labels.
log4j.rootLogger=DEBUG,CONSOLE,A1,im
#DEBUG,CONSOLE,FILE,ROLLING_FILE,MAIL,DATABASE
log4j.addivity.org.apache = true
###################
# Console Appender
output to console
################### log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.Threshold=DEBUG log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n #log4j.appender.CONSOLE.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n##################### # File Appender
output to file
##################### log4j.appender.FILE=org.apache.log4j.FileAppender log4j.appender.FILE.File=file.log log4j.appender.FILE.Append=false log4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n # Use this layout for LogFactor 5 analysis######################## # Rolling File
rollback file
######################## log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender log4j.appender.ROLLING_FILE.Threshold=ERROR log4j.appender.ROLLING_FILE.File=rolling.log log4j.appender.ROLLING_FILE.Append=true log4j.appender.ROLLING_FILE.MaxFileSize=10KB log4j.appender.ROLLING_FILE.MaxBackupIndex=1 log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n#################### # Socket Appender #################### log4j.appender.SOCKET=org.apache.log4j.RollingFileAppender log4j.appender.SOCKET.RemoteHost=localhost log4j.appender.SOCKET.Port=5001 log4j.appender.SOCKET.LocationInfo=true # Set up for Log Facter 5 log4j.appender.SOCKET.layout=org.apache.log4j.PatternLayout log4j.appender.SOCET.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD]%n%c[CATEGORY]%n%m[MESSAGE]%n%n######################## # Log Factor 5 Appender ######################## log4j.appender.LF5_APPENDER=org.apache.log4j.lf5.LF5Appender log4j.appender.LF5_APPENDER.MaxNumberOfRecords=2000######################## # SMTP Appender
Send log mail
####################### log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender log4j.appender.MAIL.Threshold=FATAL log4j.appender.MAIL.BufferSize=10 log4j.appender.MAIL.From=chenyl@yeqiangwei.comlog4j.appender.MAIL.SMTPHost=mail.hollycrm.com log4j.appender.MAIL.Subject=Log4J Message log4j.appender.MAIL.To=chenyl@yeqiangwei.comlog4j.appender.MAIL.layout=org.apache.log4j.PatternLayout log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n######################## # JDBC Appender
Output to database log table
####################### log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306/test log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver log4j.appender.DATABASE.user=root log4j.appender.DATABASE.password= log4j.appender.DATABASE.sql=INSERT INTO LOG4J (Message) VALUES ('[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n') log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout log4j.appender.DATABASE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%nlog4j.appender.A1=org.apache.log4j.DailyRollingFileAppender log4j.appender.A1.File=SampleMessages.log4j log4j.appender.A1.DatePattern=yyyyMMdd-HH'.log4j' log4j.appender.A1.layout=org.apache.log4j.xml.XMLLayout################### #自定义Appender (自定义标签~)################### log4j.appender.im = net.cybercorlin.util.logger.appender.IMAppenderlog4j.appender.im.host = mail.cybercorlin.net log4j.appender.im.username = username log4j.appender.im.password = password log4j.appender.im.recipient = [email protected]=org.apache.log4j.PatternLayout log4j.appender.im.layout.ConversionPattern =[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n

 Reference blog post: https://blog.csdn.net/azheng270/article/details/2173430/ 
 




Guess you like

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