本文翻译自:No appenders could be found for logger(log4j)?
I have put log4j to my buildpath, but I get the following message when I run my application: 我已经将log4j放到了我的构建路径中,但是在运行应用程序时收到以下消息:
log4j:WARN No appenders could be found for logger (dao.hsqlmanager).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
What do these warnings mean? 这些警告是什么意思? Whats the appender here? 这里的追加器是什么?
#1楼
参考:https://stackoom.com/question/qaEV/找不到用于logger-log-j-的附加程序
#2楼
You use the Logger
in your code to log a message. 您可以在代码中使用Logger
来记录消息。 The Appender
is a Object appended to a Logger
to write the message to a specific target. Appender
是附加到Logger
的对象,用于将消息写入特定目标。 There are FileAppender
to write to text-files or the ConsoleAppender
to write to the Console. 有FileAppender
可以写入文本文件,也有ConsoleAppender
可以写入控制台。 You need to show your code of the Logger and Appender setup for more help. 您需要显示Logger和Appender设置的代码以获取更多帮助。
please read the tutorial for a better understanding of the interaction of Logger and Appender. 请阅读该教程 ,以更好地了解Logger和Appender的交互。
#3楼
This Short introduction to log4j guide is a little bit old but still valid. 这个log4j指南的简短介绍有些陈旧,但仍然有效。
That guide will give you some information about how to use loggers and appenders. 该指南将为您提供有关如何使用记录器和附加器的信息。
Just to get you going you have two simple approaches you can take. 为了让您步入正轨,您可以采用两种简单的方法。
First one is to just add this line to your main method: 首先是将这一行添加到您的main方法中:
BasicConfigurator.configure();
Second approach is to add this standard log4j.properties
(taken from the above mentioned guide) file to your classpath: 第二种方法是将这个标准的log4j.properties
(从上述指南中获取)添加到您的类路径中:
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
#4楼
I get the same error. 我犯了同样的错误。 Here the problem which leads to this error message: 这是导致此错误消息的问题:
I create some objects which use the Logger before I configure the log4j: 在配置log4j之前,我创建了一些使用Logger的对象:
Logger.getLogger(Lang.class.getName()).debug("Loading language: " + filename);
Solution: Configure the log4j at the beginning in the main method: 解决方案:在main方法的开头配置log4j:
PropertyConfigurator.configure(xmlLog4JConfigFile);
// or BasicConfigurator.configure(); if you dont have a config file
#5楼
It looks like you need to add the location of your log4j.properties
file to the Classpath in Eclipse. 看来您需要将log4j.properties
文件的位置添加到Eclipse中的Classpath中。
Make sure your project is open in Eclipse, then click on the "Run" menu at the top of Eclipse and click on the following: 确保您的项目在Eclipse中打开,然后单击Eclipse顶部的“运行”菜单,然后单击以下内容:
- Run 跑
- Run Configurations 运行配置
- Classpath (tab) 类路径(标签)
- User Entries 用户条目
- Advanced (button on the right) 高级(右侧按钮)
- Add Folders 新增资料夹
- then navigate to the folder that contains your log4j.properties file 然后导航到包含log4j.properties文件的文件夹
- Apply 应用
- Run 跑
The error message should no longer appear. 错误消息应该不再出现。
#6楼
Quick solution: 快速解决方案:
add code to main function : 向主要功能添加代码:
String log4jConfPath = "/path/to/log4j.properties"; PropertyConfigurator.configure(log4jConfPath);
create a file named log4j.properties at /path/to 在/ path / to创建一个名为log4j.properties的文件
log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n