log4j The official Appender gives a few implementations
Org.apache.log4j.ConsoleAppender (console),
Org.apache.log4j.FileAppender (file),
Org.apache.log4j.DailyRollingFileAppender (generates a log file every day),
Org.apache.log4j.RollingFileAppender (creates a new file when the file size reaches the specified size),
Org.apache.log4j.WriterAppender (send log information in stream format to any specified location)
Actual development we use 1th, 3rd and 4th implementations;
If the amount of log data is not very large, we can use Dailyrollingfileappender to generate a daily log, convenient to view;
If the amount of log data is very large, we generally use rollingfileappender, fixed size of the log, if more than the creation of a new file;
We give some examples here;
Log4j.rootlogger=DEBUG, Console, File, Dailyrollingfile, rollingfile #Console log4j.appender.Console=Org.apache.log4j.ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout Log4j.appender.Console.layout.ConversionPattern=%d [%t]%-5p [%c]-%m%n #Filelog4j. Appender.file=Org.apache.log4j.FileAppenderlog4j.appender.File.File= C://Log2.logLog4j.appender.File.layout =Org.apache.log4j.PatternLayoutlog4j.appender.File.layout.ConversionPattern=%d [%t]%-5p [%c]-%m%n #DailyRollingFilelog4j. Appender.dailyrollingfile=Org.apache.log4j.DailyRollingFileAppenderlog4j.appender.DailyRollingFile.File= C://Log3.logLog4j.appender.DailyRollingFile.layout =Org.apache.log4j.PatternLayoutlog4j.appender.DailyRollingFile.layout.ConversionPattern=%d [%t]%-5p [%c]-%m%n #RollingFilelog4j. Appender.rollingfile=Org.apache.log4j.RollingFileAppenderlog4j.appender.RollingFile.File= C://Log4.logLog4j.appender.rollingfile.maxfilesize=1kblog4j.appender.rollingfile.maxbackupindex=3Log4j.appender.RollingFile.layout=Org.apache.log4j.PatternLayoutlog4j.appender.RollingFile.layout.ConversionPattern=%d [%t]%-5p [%c]-%m%n
Test code:
Package com.open1111; import Org.apache.log4j.Logger; Public classTest {Private StaticLogger Logger=logger.getlogger (Test.class);//Get Logger instance Public Static voidMain (string[] args) {Logger.info ("General Info Info"); Logger.debug ("Debugging Debug Information"); Logger.error ("Error Message"); Logger.warn ("warning warn information"); Logger.fatal ("fatal error fatal information"); Logger.error ("error Message",NewIllegalArgumentException ("Illegal parameters")); intI=0; while(i<10000) {Logger.debug ("rollingfile Debugging Debug Information"); Logger.debug ("rollingfile Debugging Debug Information"); Logger.debug ("rollingfile Debugging Debug Information"); Logger.debug ("rollingfile Debugging Debug Information"); Logger.debug ("rollingfile Debugging Debug Information"); I++; } } }
Here are two new configuration items explained below:
MaxFileSize is the maximum size of the log file and 10KB 100KB according to the actual demand.
Maxbackupindex is the number of log files, if more than the coverage, the main consideration is the capacity of the hard disk problem, according to the actual demand for example 100 500;
This gives the effect of the log file:
Dailyrollingfileappender Effect:
Rollingfileappender Effect:
log4j Appender Output Type configuration