gpt4 book ai didi

java - 动态命名 log4j 日志文件 - 有效,但 setFile() 并非没有异常(exception)

转载 作者:行者123 更新时间:2023-12-04 05:14:56 24 4
gpt4 key购买 nike

我有一个为个人客户运行的程序。每个客户都有一个帐户代码(我们称之为起始代码)。帐户代码通过命令行参数传递给程序。我收到以下异常返回给我,顺便说一下,这不会导致程序崩溃,它会愉快地进行。我想按客户帐户分离日志记录,这似乎有效。我的日志文件将被创建为“Balancer_990004.log”,并且日志消息肯定会在创建 Balancer_990004.log.7 时建立,但是,并非没有这个异常(exception)。在这个问题上抓我的头。我已经包含了 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);

最佳答案

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

关于java - 动态命名 log4j 日志文件 - 有效,但 setFile() 并非没有异常(exception),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14405212/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com