2

我有一个为个人客户运行的程序。每个客户都有一个帐户代码(我们称之为起始代码)。帐户代码通过命令行参数传递给程序。我收到了以下异常,顺便说一句,它不会导致程序崩溃,它会顺利进行。我想按客户帐户分开记录,这似乎有效。我的日志文件将被创建为“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);
4

3 回答 3

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”。所以,我暂时使用这个代码块,直到找到更好的解决方案。目前,配置将依赖于该程序的附加名称“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。

于 2013-01-22T19:13:52.467 回答
1

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

于 2013-01-20T08:37:51.370 回答
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)
    }
}

基本思想是在正确设置环境变量之前对日志系统不做任何事情。

于 2013-12-18T19:29:59.400 回答