log4j.properties文件的配置
#info是最低级的
log4j.rootLogger=info, ServerDailyRollingFile, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%n%-d{yyyy-MM-dd HH:mm:ss}%n[%p]-[Thread: %t]-[%C.%M()]: %m%n
log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ServerDailyRollingFile.DatePattern='.'yyyy-MM-dd
log4j.appender.ServerDailyRollingFile.File=/data/htdocs/appadmob/logs/notify-subscription.log
#log4j.appender.ServerDailyRollingFile.File=C\://logs/notify-subscription.log
log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout
####################################################################[%p]:error info
####################################################################[%C\:%M()]:类名 方法名
log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=%n%-d{yyyy-MM-dd HH\:mm\:ss}%n[%p]-[Thread\: %t]-[%C\:%M()]\: %m%n
log4j.appender.ServerDailyRollingFile.Append=true
测试demo
package com;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Test {
Logger log = Logger.getLogger(Test.class);
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.properties");
Test test = new Test();
test.aaa();
}
public void aaa(){
try {
String s = "";
s.substring(2);
} catch (Exception e) {
e.printStackTrace();
log.error("",e);//参数一定是两个 如果只填写一个参数e,看不到报错的行数
}
}
}