Java 通过log4j 1.x记录到应用服务器日志
我的web应用程序使用Apache log4j 1.2库进行日志记录。出于客户的安全考虑,我们希望战争应作为未爆炸战争进行。因此,使用log4j1.2.jar进行日志记录,我将以编程方式配置和启动日志记录。代码如下:Java 通过log4j 1.x记录到应用服务器日志,java,logging,log4j,Java,Logging,Log4j,我的web应用程序使用Apache log4j 1.2库进行日志记录。出于客户的安全考虑,我们希望战争应作为未爆炸战争进行。因此,使用log4j1.2.jar进行日志记录,我将以编程方式配置和启动日志记录。代码如下: //lets check the System property for log4j configuration file . Then check the class path . If not the add our custom logic String log4jS
//lets check the System property for log4j configuration file . Then check the class path . If not the add our custom logic
String log4jSystemProp;
URL log4jPropsURL;
//checking the system property
log4jSystemProp = System.getProperty("log4j.configuration");
if(log4jSystemProp == null || log4jSystemProp.trim().equalsIgnoreCase("")){
//loading the configuration file from the class path
log4jPropsURL = Loader.getResource("log4j.properties");
if(log4jPropsURL == null){
log4jPropsURL = Loader.getResource("log4j.xml");
}
if(log4jPropsURL == null){
//programatically configuring log4j.properties file
logDirPath = "C:\\debug\\log";
if(CommonUtils.isValue(logDirPath)){
logDirectory = new File(logDirPath);
}
if(logDirectory ==null || !logDirectory.exists() || (!logDirectory.canWrite())){
if(!CommonUtils.isValue(log4jSystemProp) || (log4jPropsURL == null)){
BasicConfigurator.configure();
}
} else {
//creating the file appenders for the
Logger root = Logger.getRootLogger();
createAppLogFile();
//now creating the File appenders
PatternLayout patternLayout = new PatternLayout("%r [%t] %p %c %x - %m%n");
final boolean append = true;
final boolean bufferedIO = true;
final int bufferSize = 1024;
FileAppender debugAppender = new FileAppender(patternLayout, debugLog.getPath(), append, bufferedIO, bufferSize);
debugAppender.setThreshold(Priority.DEBUG);
FileAppender errorAppender = new FileAppender(patternLayout, errorLog.getPath(), append, bufferedIO, bufferSize);
errorAppender.setThreshold(Priority.ERROR);
root.addAppender(debugAppender);
root.addAppender(errorAppender);
}
}
}
log4j 1.2的工作方式是,如果有java运行时属性集,则使用“log4j.congiguration”属性值初始化log4j日志记录
如果没有如上所述设置VM选项,它将在类路径层次结构中查找配置文件“log4j.properties”/log4j.xml。如果它找到了,它就会使用它
如果以上两个失败,我将通过代码以编程方式配置Log4jSystem
以下是我的疑问:
是否有办法在父(或应用程序服务器)类加载器中旁路VM系统属性或log4j配置文件
第二,另一个与上述相反的问题。log4j 1.2是否提供了将所有应用程序日志写入应用程序服务器日志文件的标准方法
谢谢