# Set log levels # log4j.rootLogger = All, Console, LogFile, ErrorFile # Output the log info to the Java Console # log4j.appender.Console = org.apache.log4j.ConsoleAppender log4j.appender.Console.Target = System.out log4j.appender.Console.ImmediateFlush = true log4j.appender.Console.Threshold = DEBUG log4j.appender.Console.layout = org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern = [%d{yy-MM-dd HH:mm:ss}] [%p] %c %t : %m%n # Save the log info to the log file # log4j.appender.LogFile = org.apache.log4j.RollingFileAppender log4j.appender.LogFile.File = ./logs/ftp.info.log log4j.appender.LogFile.Append = true log4j.appender.LogFile.ImmediateFlush = true log4j.appender.LogFile.MaxFileSize = 10MB log4j.appender.LogFile.MaxBackupIndex = 16 log4j.appender.LogFile.Threshold = DEBUG log4j.appender.LogFile.layout = org.apache.log4j.PatternLayout log4j.appender.LogFile.layout.ConversionPattern = [%d{yy-MM-dd HH:mm:ss}] [%p] %c %t : %m%n # Save the error info to the error file. A file one day. # log4j.appender.ErrorFile = org.apache.log4j.DailyRollingFileAppender log4j.appender.ErrorFile.File = ./logs/ftp.error.log log4j.appender.ErrorFile.Append = true log4j.appender.ErrorFile.ImmediateFlush = true log4j.appender.ErrorFile.Threshold = ERROR log4j.appender.ErrorFile.layout = org.apache.log4j.PatternLayout log4j.appender.ErrorFile.layout.ConversionPattern = [%d{yy-MM-dd HH:mm:ss}] [%p] %c %t : %m%n
Log4j rootLogger Configuration
Log4j root configuration syntax
log4j.rootLogger = [ level ] , appenderName, appenderName, …
It refers to the specified level output log information to the specified one or more locations
Here we ALL levels of output and the above information to the Console and LogFile, ErrorFile;
Log4j Append property specifies whether additional content ;
Here there is a default attribute log4j.appender.xx.Append is true is added, the result is we are here Append false documents covering the front of the
Log4j Threshold attribute specifies the output level
Sometimes we need to put some error ERROR logs alone saved to the specified file, this time, Threshold property comes in handy;
Threshold property to specify the log level
Log4j log information according to an importance, sub-OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL
log4j.appender.ErrorFile.Threshold = ERROR
log4j.appender.LogFile.Threshold = DEBUG
log4j layout log information format
layout Log4j provided are the following
org.apache.log4j.HTMLLayout (in HTML table format layout),
org.apache.log4j.PatternLayout (the flexibility to specify the layout mode),
Org.apache.log4j.SimpleLayout (containing level information and log information string),
org.apache.log4j.TTCCLayout (including the time the log generated, thread, type information, etc.)
HTMLLayout examples
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.HTMLLayout
Standard html table table format, the display information including information given thread ranking classes;
PatternLayout recommended, very flexible
There ConversionPattern attributes, flexible output configuration attributes:
% M output code specified in the message;
% M when the log output printing method name;
% P output priority, i.e. DEBUG, INFO, WARN, ERROR, FATAL;
% R output from the application starts to output the log information on the number of milliseconds spent;
% C output category belongs, where the class is usually full name;
% T thread name of the output generated log events;
Output% n Enter a newline, Windows platform "rn", Unix platform "n";
Date or time% d log output time points, the default format is ISO8601, you can specify the format followed in, for example:% d {yyyy-MM-dd HH: mm: ss, SSS}, similar output: 2002-10- 18 22: 10: 28,921;
% L output log event occurrence position and the number of lines in the code;
SimpleLayout simple layout:
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.SimpleLayout
Run output:
INFO - general info Info
DEBUG - Debug debug information
ERROR - error error information
WARN - Warnings warn Information
FATAL - fatal serious error message
ERROR - error message java.lang.IllegalArgumentException: Invalid parameter
at com.open1111.Test.main(Test.java:16)
Only the output level and information
TTCCLayout We tested at:
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.TTCCLayout
Run output: [main] INFO com.open1111.Test - general info Info
Log4j appender output type configuration
Log4j appender official gives a moment to realize several
org.apache.log4j.ConsoleAppender (console),
org.apache.log4j.FileAppender (file),
org.apache.log4j.DailyRollingFileAppender (produce a daily log file),
org.apache.log4j.RollingFileAppender (file size reaches the specified size to generate a new file),
Org.apache.log4j.WriterAppender (log stream format information is sent to any designated place)
If the amount of log data is not large, we can use every day DailyRollingFileAppender produce a log for easy viewing;
If a large amount of log data, we generally use RollingFileAppender, fixed-size log, if more than just generate a new file;