动态命名log4j日志文件 - 工作,但不是没有例外,来自setFile()

时间:2013-01-18 18:23:37

标签: java logging log4j

我有一个针对个人客户运行的程序。每个客户都有一个帐户代码(我们称之为起始代码)。帐户代码通过命令行参数传递给程序。我正在向我抛出以下异常,顺便说一句,它不会导致程序崩溃,它会很愉快。我想按客户帐户分开日志记录,这似乎有效。我的日志文件将被创建为“Balancer_990004.log”,并且日志消息肯定会建立到正在创建的Balancer_990004.log.7点,但不是没有此异常。抓住我的头。我已经包含了log4j.properties位和代码位。

log4j:ERROR setFile(null,true) call failed.
java.io.FileNotFoundException:  (A file or directory in the path name does not exist.)
 at java.io.FileOutputStream.openAppend(Native Method)
 at java.io.FileOutputStream.<init>(FileOutputStream.java:203)
 at java.io.FileOutputStream.<init>(FileOutputStream.java:127)
 at org.apache.log4j.FileAppender.setFile(FileAppender.java:294)
 at org.apache.log4j.RollingFileAppender.setFile(RollingFileAppender.java:207)
 at org.apache.log4j.FileAppender.activateOptions(FileAppender.java:165)
 at org.apache.log4j.config.PropertySetter.activate(PropertySetter.java:307)
 at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:172)
 at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:104)
 at org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigurator.java:809)
 at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:735)
 at org.apache.log4j.PropertyConfigurator.parseCatsAndRenderers(PropertyConfigurator.java:639)
 at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:504)
 at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:547)
 at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:483)
 at org.apache.log4j.LogManager.<clinit>(LogManager.java:127)
 at java.lang.J9VMInternals.initializeImpl(Native Method)
 at java.lang.J9VMInternals.initialize(J9VMInternals.java:200)
 at org.apache.log4j.Logger.getLogger(Logger.java:117)
 at Balancer.<init>(Balancer.java:292)
 at Balancer.main(Balancer.java:2370)

*** Sample bit from log4j.properties.  The name of the Java program in question is "Balancer" ***

log4j.rootLogger=debug, devnull        
log4j.logger.Balancer=info, BalancerLog

log4j.appender.BalancerLog=org.apache.log4j.RollingFileAppender
log4j.appender.BalancerLog.File=${balancer.log}                
log4j.appender.BalancerLog.MaxFileSize=10000KB                                
log4j.appender.BalancerLog.MaxBackupIndex=10                                  
log4j.appender.BalancerLog.layout=org.apache.log4j.PatternLayout              
log4j.appender.BalancerLog.layout.ConversionPattern=%5p %d [%t] (%F:%L) - %m%n

*** How I'm building my log filename and initializing the Logger ***

public class Balancer {
    static Logger log = null;
    ....

// In constructor...

StringBuilder logName = new StringBuilder("Balancer_");
logName.append(startCode.toString());
logName.append(".log");

// Hopefully in log4j.properties our appender section contains this line:
// log4j.appender.BalancerLog.File=${balancer.log}.  This ${} bit means to get a system property to set the log file
// name.  We set that system property here, then initialize the Logger object.
System.setProperty("balancer.log", logName.toString()); 

// This is line 292 in the exception trace.
log = Logger.getLogger(Balancer.class);

3 个答案:

答案 0 :(得分:1)

如果您可以切换到logback,我建议您改用SiftingAppender。用log4j进行这种调整是不自然的。

答案 1 :(得分:1)

我发现了为什么我从setFile()获取异常。也许这在.properties log4j配置中不受支持,我必须使用XML方法,但如果我使用推荐的语法设置“.File”属性来使用系统属性,我会收到错误(即使它已经出现了无论如何都要提前工作)。例如:

log4j.appender.BalancerLog=org.apache.log4j.RollingFileAppender
log4j.appender.BalancerLog.File=${balancer.log}
log4j.appender.BalancerLog.MaxFileSize=10000KB
log4j.appender.BalancerLog.MaxBackupIndex=10
log4j.appender.BalancerLog.layout=org.apache.log4j.PatternLayout
log4j.appender.BalancerLog.layout.ConversionPattern=%5p %d [%t] (%F:%L) - %m%n

给我错误。回到标准:

log4j.appender.BalancerLog=org.apache.log4j.RollingFileAppender
log4j.appender.BalancerLog.File=Balancer.log
log4j.appender.BalancerLog.MaxFileSize=10000KB
log4j.appender.BalancerLog.MaxBackupIndex=10
log4j.appender.BalancerLog.layout=org.apache.log4j.PatternLayout
log4j.appender.BalancerLog.layout.ConversionPattern=%5p %d [%t] (%F:%L) - %m%n

和“alle ist klar herr kommissar”。所以,我现在正在使用这个代码块,直到我找到更好的解决方案。目前,配置依赖于此程序的appender名称为“BalancerLog”。

// We are dynamically building the name of our log file, if the log4j.properties file is set correctly.
// Should look like "Balancer_997232.log" or any other startcode.  Log file will be created in whatever the current
// IFS directory is when the job gets kicked off.
StringBuilder logName = new StringBuilder("Balancer_");
logName.append(startCode.toString());
logName.append(".log");

// This isn't perfect I guess and am sure it will see revisions in the future.
// What I'm doing here is asking the log object if it has an appender called BalancerLog. Look in 
// the log4j.properties file.  You should see a line similiar to :
// log4j.logger.Balancer=info, BalancerLog
// We're getting the the appender object and setting it's file name to be what was built above.
// We don't want to use the default log file name.  You'll see it in the log4j.properties file like:
// log4j.appender.BalancerLog.File=Balancer.log
try {
    Appender appender = log.getAppender("BalancerLog");
    if (appender != null && appender instanceof org.apache.log4j.FileAppender) {
        FileAppender roll = (FileAppender) appender;
        roll.setFile(logName.toString());
        roll.activateOptions();
    }
}
catch (Exception e) {
    System.out.println(e);
}

随意分开并提出任何建议。顺便说一句,在这里使用log4j 1.2。

答案 2 :(得分:1)

在设置环境变量之前,您似乎正在尝试访问日志。

如果没有进行单个日志调用,此代码将失败:

class Whatever {
    private static Logger log = Logger.getLogger(Whatever.class)
    public Whatever() {
        System.setProperty("balancer.log","C:\\log.txt")
    }
}

这样做:

class Whatever {
    private static Logger log = null
    public Whatever() {
        System.setProperty("balancer.log","C:\\log.txt")
        log = Logger.getLogger(Whatever.class)
    }
}

基本思路是在正确设置环境变量之前,不要使用日志系统执行任何