简体   繁体   English

log4j未初始化,未找到附加程序

[英]log4j not initialized and no appenders found

I'm trying to use log4j in my Java application (no web-app). 我正在尝试在Java应用程序(无Web应用程序)中使用log4j。 Therefore I added the configuration file directly within my /src folder. 因此,我直接在/ src文件夹中添加了配置文件。

log4j.properties log4j.properties

log4j.rootLogger=DEBUG

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{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

My main class is in a package with a sub-namespace and within there I'm trying to log as follows: 我的主类在一个带有子命名空间的包中,并且在其中尝试记录如下:

final static Logger logger = Logger.getLogger(MainClass.class);
public static void main(String[] args) {
    //BasicConfigurator.configure();
    logger.warn("some warning");
}

But still I'm getting an error message and I don't know what to do. 但是我仍然收到错误消息,不知道该怎么办。 You see the commented line BasicConfigurator above. 您会在上方看到注释行BasicConfigurator As far as I know, the log4j.properties should be read without calling that line, right? 据我所知,应该在不调用该行的情况下读取log4j.properties,对吗?

log4j:WARN No appenders could be found for logger (at.company.project.poi.MainClass). log4j:WARN找不到记录器的附加程序(at.company.project.poi.MainClass)。 log4j:WARN Please initialize the log4j system properly. log4j:WARN请正确初始化log4j系统。 log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. log4j:WARN有关更多信息,请参见http://logging.apache.org/log4j/1.2/faq.html#noconfig

According to documentation, file should be on classpath and named 'log4j 2 .properties' 根据文档,文件应位于类路径上,并命名为“ log4j 2 .properties”

You can also set directly the configuration by setting -Dlog4j.configurationFile="<your config filepath>" 您还可以通过设置-Dlog4j.configurationFile="<your config filepath>"直接设置-Dlog4j.configurationFile="<your config filepath>"

Source: https://logging.apache.org/log4j/2.x/manual/configuration.html 资料来源: https : //logging.apache.org/log4j/2.x/manual/configuration.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM