gpt4 book ai didi

java - Log4j 找不到 appender,但它们存在

转载 作者:行者123 更新时间:2023-11-28 22:28:35 26 4
gpt4 key购买 nike

我在使用 log4j 进行日志记录时遇到问题。我创建了我的属性文件,并在 Java 代码中打开它,一切正常,只是属性文件出现故障。我在这里浏览了几个教程和问题,但我似乎无法修复它。这是我的属性文件(称为 WSConnectorLog.properties):

##########################
# Loggers
log4J.rootLogger = FATAL, console
log4j.WSDetailedLogger = TRACE, WSDetailLog, console
log4j.WSAppLogger = INFO, WSAppLog, console

# Appenders
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.target=System.err
log4j.appender.console.layout.ConversionPattern=%d [%t] (%F:%L) %p %m%n

log4j.appender.WSDetailLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.WSDetailLog.File=logs/WSDetailed.log
log4j.appender.WSDetailLog.DatePattern='.'yyyy-MM-dd-HH
log4j.appender.WSDetailLog.layout=org.apache.log4j.PatternLayout
log4j.appender.WSDetailLog.layout.ConversionPattern=%d [%t] (%F:%L) %-5p %m%n

log4j.appender.WSAppLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.WSAppLog.File=logs/ws.log
log4j.appender.WSAppLog.DatePattern='.'yyyy-MM-dd-HH
log4j.appender.WSAppLog.layout=org.apache.log4j.PatternLayout
log4j.appender.WSAppLog.layout.ConversionPattern=%m%n

# Set root category priority to INFO and its only appender to console.
log4j.rootCategory=INFO, console

我在 Java 中这样使用它:

准备:

public class WSConnector {

private static Logger WSDetailedlog = Logger.getLogger("WSDetailedLogger");
private static Logger WSAppLog = Logger.getLogger("WSAppLogger");
private ConnectionManager cm = ConnectionManager.getInstance();
private ConfigHandler config = ConfigHandler.getInstance();
private SimpleDateFormat format = null;

/**
* Protected constructor for the Singleton pattern.
*/
protected WSConnector() {
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Properties props = new Properties();
try {
props.load(getClass().getResourceAsStream("/WSConnectorLog.properties"));
} catch (IOException e) {
e.printStackTrace();
}
PropertyConfigurator.configure(props);
}

然后写信给:

...
WSDetailedlog.info(log);
...
WSAppLog.info("random string";
...

我收到以下错误:

log4j:ERROR Could not find value for key log4j.appender.CONSOLE
log4j:ERROR Could not instantiate appender named "CONSOLE".
log4j:WARN No appenders could be found for logger (WSDetailedLogger).
log4j:WARN Please initialize the log4j system properly.
log4j:ERROR Could not find value for key log4j.appender.CONSOLE
log4j:ERROR Could not instantiate appender named "CONSOLE".
log4j:ERROR Could not find value for key log4j.appender.CONSOLE
log4j:ERROR Could not instantiate appender named "CONSOLE".
log4j:ERROR Could not find value for key log4j.appender.CONSOLE
log4j:ERROR Could not instantiate appender named "CONSOLE".
log4j:ERROR Could not find value for key log4j.appender.CONSOLE
log4j:ERROR Could not instantiate appender named "CONSOLE".
log4j:ERROR Could not find value for key log4j.appender.CONSOLE
log4j:ERROR Could not instantiate appender named "CONSOLE".
log4j:ERROR Could not find value for key log4j.appender.CONSOLE
log4j:ERROR Could not instantiate appender named "CONSOLE".

我运行它的环境是 Tomcat 7.0。我在那里导出了 jar,并将属性文件放在它旁边。我从 Eclipse Dynamic WebApp 项目调用 jar,以便它可以看到 Tomcat 的 lib 目录。问题不在于它找不到属性文件,而是它以某种方式出现了格式错误。

你能看看它,帮助我理解问题吗?

最佳答案

您不必指定控制台附加程序,您必须将其配置为日志级别,例如:

    log4j.rootLogger=INFO, stdout
# Console appender configuration
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] (%F:%L) %p %m%n
log4j.appender.stdout.filter.b=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.stdout.filter.b.LevelToMatch=FATAL
log4j.appender.stdout.filter.b.AcceptOnMatch=false

这是我用来仅打印 FATAL 日志的配置。希望对您有所帮助!

关于java - Log4j 找不到 appender,但它们存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31701552/

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