"1" Starts from scratch
a). New Java project>> new package>> new Java class;
b). Import jar Package ( one is Enough ), here I use Log4j-1.2.14.jar,
c). Create a new log4j.properties, placed under project root ;
Log4j.rootlogger=info, Serverdailyrollingfile, stdout
Log4j.appender.serverdailyrollingfile=org.apache.log4j.dailyrollingfileappender
Log4j.appender.serverdailyrollingfile.datepattern= '. ' Yyyy-mm-dd
Log4j.appender.serverdailyrollingfile.file=c://logs/notify-subscription.log
Log4j.appender.serverdailyrollingfile.layout=org.apache.log4j.patternlayout
log4j.appender.serverdailyrollingfile.layout.conversionpattern=%d-%m%n
Log4j.appender.serverdailyrollingfile.append=true
Log4j.appender.stdout=org.apache.log4j.consoleappender
Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.STDOUT.LAYOUT.CONVERSIONPATTERN=%D{YYYY-MM-DD HH:mm:ss}%p [%c]%m%n
d). In main (), load the log4j:
Propertyconfigurator.configure ("Log4j.properties");
e). Write a small program test, OK, let's see the effect:
"God horse" can't find file exception?
1). With absolute path, really not recommended Ah, too not elegant;
2). Place the log4j file in the bin/directory:
a). In the Code, Propertyconfigurator.configure ("bin/log4j.properties");
b). In the Code, propertyconfigurator.configure (classloader.getsystemresource("log4j.properties"));
c). Note that it must be located in the bin Direct directory and not in the deeper directory of the bin. But is this a god-horse pinch?
Refer to: http://blog.sina.com.cn/s/blog_3f4755c70100jco1.html
3) must kill skill:
private static void initlog4j () {
Properties prop = new properties ();
Prop.setproperty ("Log4j.rootlogger", "DEBUG, CONSOLE");
Prop.setproperty ("Log4j.appender.CONSOLE", "Org.apache.log4j.ConsoleAppender");
Prop.setproperty ("Log4j.appender.CONSOLE.layout", "org.apache.log4j.PatternLayout");
Prop.setproperty ("Log4j.appender.CONSOLE.layout.ConversionPattern", "%d{hh:mm:ss,sss} [%t]%-5p%c{1}:%m%n");
Propertyconfigurator.configure (prop);
}
"2" log4j format detailed
Log4j.rootlogger= log level, Appender1, Appender2, ....
Log level:all<debug<info<warn<error<fatal<off, case insensitive
Note that you need to enter in the console by simply defining one of the Appender as stdout
Note that the Rootlogger default is for the entire project to take effect
Note that if you only want to operate on certain packages , then: Log4j.logger. Com.hutu=info, stdout, indicates that the log is in effect for the package Com.hutu
Note that this can be distinguished from the dev/line, or the performance impact can be reduced:if (log.isdebugenabled ()){log.debug ();}
Log4j.appender.appender1=org.apache.log4j. Where does the log output go?
consoleappender(console)
fileappender(file)
Dailyrollingfileappender(Generate a log file every day)
Rollingfileappender(creates a new file when the file size reaches the specified size)
writeappender(send log information in stream format to any specified location)
jdbcappender(saving log information to the database)
log4j.appender.appender1.file= file directory and files
${user.home}/logs/...
log4j.appender.appender1.maxfilesize= Maximum file size
log4j.appender.appender1.maxbackupindex= Number of backup files
Where the appender1 is defined in the first line;
File directories and files, for example,/home/admin/logs/hutudan.log
Maximum file size, for example, 100KB
Number of backup files, for example, 1
log4j.appender.serverdailyrollingfile.datepattern= Log Suffix format
log4j.appender.appender1.layout=org.apache.log4j. Log Layout format
Htmllayout (Layout in HTML table Form)
Simplelayout (contains level and information strings for log information)
Ttcclayout (contains information such as the time that the log was generated, threads, categories, etc.)
Patternlayout (can flexibly specify layout format, commonly used )
log4j.appender.appender1.layout.conversionpattern= Log Output format
For example,%d-%m%n or%d{yyyy-mm-dd HH:mm:ss}%p [%c]%m%n
The full name of the class to which the %c output log information belongs
%d the date or time of the output log time, the default format is ISO8601, or the format can be specified later, for example:%d{yyy-m-dd HH:mm:ss}, output similar to: 2002-10-18-22:10:28
%f The class name of the class to which the output log information belongs
%l The occurrence of the output log event where the statement that outputs the log information is in the first line of the class it is in
%m The information specified in the output code, such as a message in log (message)
%n Output a carriage return newline character, Windows platform is "RN", UNIX platform is "n"
%p output priority, or debug,info,warn,error,fatal. Debug if the debug () output is called, and so on
%r The number of milliseconds that the output from the application starts to output this log information
%t Output The name of the thread that generated the log event
Refer to: http://blog.sina.com.cn/s/blog_4e4dd5570100qowy.html
Log4j.appender.serverdailyrollingfile.append=true
To summarize:
Logger class: Complete logging, set log information level
Appender class: Decide log whereabouts, terminal, DB, hard disk
Layout class: Determines the style of the log output, such as containing the current thread, line number, time
Eclipse, log4j configuration (really detailed ~)