log4j作为一个记录log的工具,已经广泛应用于程序中,记录程序的运行状态,帮助开发人员诊断程序发生Bug的原因。既然log4j具备记录log的功能,并且可以根据包的路径输出不同的log文件,那能不能也用log4j 记录所有路径,并且不影响原先的log输入?
比如说已有log4j.properties如下
#CONSOLE
log4j.rootLogger=info,stdout
log4j.logger.xxx.core=error,CoreLog
log4j.logger.xxx.server=error,ServerLog
# set log show on console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=<nobr>[ %p %d{yyyy-MM-dd HH:mm:ss} %c - %L - %M ] : %m%n </nobr><br/><br/>
#scionCoreLog
log4j.appender.CoreLog=org.apache.log4j.RollingFileAppender
log4j.appender.CoreLog.MaxFileSize=1024KB
log4j.appender.CoreLog.MaxBackupIndex=10
log4j.appender.CoreLog.File=${spc.root}/logs/scionCore.log
log4j.appender.CoreLog.Append=true
log4j.appender.CoreLog.layout=org.apache.log4j.PatternLayout
log4j.appender.CoreLog.layout.ConversionPattern=<nobr>[ %p %d{yyyy-MM-dd HH:mm:ss} %c - %L - %M ] : %m%n </nobr><br/><br/>
...
...
...
现在有一要求需要记录用户的操作,但这些操作有可能散布在各个包上,上面的设定貌似不能满足要求了,但又不想丢掉那么 好用的日志记录工具,于是想了办法改造了一下log4j
具体如下
1.用一个类重新设置log4j的属性,不读取log4j.properties的属性
2.把类读取的参数放置在WEB-INF/目录下
ProgramLog4j .java
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import org.apache.log4j.Hierarchy;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.spi.DefaultRepositorySelector;
import org.apache.log4j.spi.LoggerFactory;
import org.apache.log4j.spi.RepositorySelector;
import org.apache.log4j.spi.RootCategory;
public class ProgramLog4j extends Logger {
public static Properties properties;
static private RepositorySelector myRepositorySelector;
protected ProgramLog4j(String name) {
super(name);
}
static {
if (properties == null) {
Hierarchy h = new Hierarchy(new RootCategory((Level) Level.DEBUG));
myRepositorySelector = new DefaultRepositorySelector(h);
properties = new Properties();
Properties props = new Properties();
try {
String filePath = "programeLog4j.properties";
URL url= ProgramLog4j.class.getClassLoader().getResource("");
String path =url.getPath();
int lastIndex = path.lastIndexOf("classes/");
filePath =path.substring(0, lastIndex ) +filePath ;
InputStream in = new BufferedInputStream(new FileInputStream(new File( filePath )));
props.load(in);
} catch (Exception ex) {
ex.printStackTrace();
}
properties.setProperty("log4j.rootCategory", "info, R");
properties.setProperty("log4j.appender.R","org.apache.log4j.RollingFileAppender");
properties.setProperty("log4j.appender.R.File", props.getProperty("log4j.appender.R.File"));
properties.setProperty("log4j.appender.R.MaxFileSize", props.getProperty("log4j.appender.R.MaxFileSize"));// 去掉K,只忍数字
properties.setProperty("log4j.appender.R.MaxBackupIndex", props.getProperty("log4j.appender.R.MaxBackupIndex"));
properties.setProperty("log4j.appender.R.layout",props.getProperty("log4j.appender.R.layout"));
properties.setProperty("log4j.appender.R.layout.ConversionPattern",props.getProperty("log4j.appender.R.layout.ConversionPattern"));
new PropertyConfigurator().doConfigure(properties,myRepositorySelector.getLoggerRepository());
}
}
public static Logger getLogger(String name)
{
return myRepositorySelector.getLoggerRepository().getLogger(name);
}
public static Logger getLogger(Class clazz)
{
return myRepositorySelector.getLoggerRepository().getLogger(clazz.getName());
}
public static Logger getRootLogger()
{
return myRepositorySelector.getLoggerRepository().getRootLogger();
}
public static Logger getLogger(String name, LoggerFactory factory)
{
return myRepositorySelector.getLoggerRepository().getLogger(name, factory);
}
}
programeLog4j.properties
log4j.appender.R.File=${spc.root}/logs/user.log
log4j.appender.R.MaxFileSize=1024
log4j.appender.R.MaxBackupIndex=2
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[ %p %d{yyyy-MM-dd HH:mm:ss} %c - %L - %M ] : %m%n