Java log4j:WARN 在 web.xml 中找不到记录器的附加程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1266139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
log4j:WARN No appenders could be found for logger in web.xml
提问by cometta
I already put the log4jConfigLocation in web.xml, but I still get the following warning:
我已经将 log4jConfigLocation 放在 web.xml 中,但我仍然收到以下警告:
log4j:WARN No appenders could be found for logger ?
? (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
What did I miss?
我错过了什么?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>suara2</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>suara2</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
采纳答案by CodeGoat
If that's the entire log4j.properties file it looks like you're never actually creating a logger. You need a line like:
如果那是整个 log4j.properties 文件,那么您似乎从未真正创建过记录器。你需要这样一行:
log4j.rootLogger=debug,A1
回答by Aaron Digulla
You'll see this warning if log4j can't find a file "log4j.properties" or "log4j.xml" anywhere.
如果 log4j 在任何地方都找不到文件“log4j.properties”或“log4j.xml”,您将看到此警告。
回答by prabhat
You may get this error when your log4j.properties
are not present in the classpath.
当您log4j.properties
不在类路径中时,您可能会收到此错误。
This means you have to move the log4j.properties
into the src folder and set the output to the bin folder so that at run time log4j.properties
will read from the bin folder and your error will be resolved easily.
这意味着您必须将 移动log4j.properties
到 src 文件夹并将输出设置为 bin 文件夹,以便在运行时log4j.properties
将从 bin 文件夹中读取,并且您的错误将很容易解决。
回答by Alain O'Dea
I had log4j.properties in the correct place in the classpath and still got this warning with anything that used it directly. Code using log4j through commons-logging seemed to be fine for some reason.
我在类路径中的正确位置有 log4j.properties 并且仍然收到此警告,任何直接使用它的东西。由于某种原因,通过 commons-logging 使用 log4j 的代码似乎没问题。
If you have:
如果你有:
log4j.rootLogger=WARN
Change it to:
将其更改为:
log4j.rootLogger=WARN, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.conversionPattern=%5p [%t] (%F:%L) - %m%n
According to http://logging.apache.org/log4j/1.2/manual.html:
根据http://logging.apache.org/log4j/1.2/manual.html:
The root logger is anonymous but can be accessed with the Logger.getRootLogger() method. There is no default appender attached to root.
根记录器是匿名的,但可以使用 Logger.getRootLogger() 方法访问。没有附加到 root 的默认 appender。
What this means is that you need to specify some appender, any appender, to the root logger to get logging to happen.
这意味着您需要为根记录器指定一些附加程序,任何附加程序才能进行日志记录。
Adding that consoleappender to the rootLogger gets this complaint to disappear.
将该控制台附加程序添加到 rootLogger 会使此投诉消失。
回答by Marcelo Daniel
If still help, verify the name of archive, it must be exact "log4j.properties" or "log4j.xml" (case sensitive), and follow the hint by "Alain O'Dea". I was geting the same error, but after make these changes everthing works fine. just like a charm :-). hope this helps.
如果仍然有帮助,请验证存档名称,它必须是“log4j.properties”或“log4j.xml”(区分大小写),并按照“Alain O'Dea”的提示进行操作。我遇到了同样的错误,但在进行这些更改后一切正常。就像一个魅力:-)。希望这可以帮助。
回答by MikeF
Or, you could be doing what I did and define the logger before the log configuration file has been loaded. This would be as they say: "Putting the cart before the horse."
或者,您可以按照我所做的那样在加载日志配置文件之前定义记录器。这就像他们说的:“把车放在马之前。”
In the code:
在代码中:
public static Logger logger = Logger.getLogger("RbReport");
... later on
... 稍后的
PropertyConfigurator.configure(l4j);
logger = Logger.getLogger("RbReport");
Fix was to initialize the logger after the configuration was loaded.
修复是在加载配置后初始化记录器。
For the geeks it was "Putting Descarte b4 d horse".
对于极客来说,它是“Putting Descarte b4 d horse”。
回答by jbatista
In my case the solution was easy. You don't need to declare anything in your web.xml
.
就我而言,解决方案很简单。你不需要在你的web.xml
.
Because your project is a web application, the config file should be on WEB-INF/classes
after deployment.
I advise you to create a Java resource folder (src/main/resources
) to do that (best pratice). Another approach is to put the config file in your src/main/java
.
因为你的项目是一个 web 应用程序,配置文件应该WEB-INF/classes
在部署后打开。我建议您创建一个 Java 资源文件夹 ( src/main/resources
) 来执行此操作(最佳实践)。另一种方法是将配置文件放在您的src/main/java
.
Beware with the configuration file name. If you are using XML, the file name is log4j.xml
, otherwise log4j.properties
.
注意配置文件名。如果您使用的是 XML,则文件名为log4j.xml
,否则为log4j.properties
.
回答by GARIMA KILLEDAR
If you want to configure for the standalone log4j applications, you can use the BasicConfigurator. This solution won't be good for the web applications like Spring environment.
如果要为独立的 log4j 应用程序进行配置,可以使用 BasicConfigurator。该解决方案不适用于 Spring 环境等 Web 应用程序。
You need to write-
你需要写——
BasicConfigurator.configure();
or
或者
ServletContext sc = config.getServletContext();
String log4jLocation = config.getInitParameter("log4j-properties-location");
String webAppPath = sc.getRealPath("/");
String log4jProp = webAppPath + log4jLocation;
PropertyConfigurator.configure(log4jProp);
回答by MariemJab
Put these lines in the beginning of web.xml
将这些行放在 web.xml 的开头
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:/main/resources/log4j.xml</param-value>
</context-param>
See this link for more detail :http://mariemjabloun.blogspot.com/2014/04/resolved-log4jwarn-no-appenders-could.html
有关更多详细信息,请参阅此链接:http: //mariemjabloun.blogspot.com/2014/04/resolved-log4jwarn-no-appenders-could.html
回答by Roshan Khandelwal
I had the same problem . Same configuration settings and same warning message . What worked for me was : Changing the order of the entries .
我有同样的问题 。相同的配置设置和相同的警告消息。对我有用的是: 更改条目的顺序。
- I put the entries for the log configuration [ the context param and the listener ] on the top of the file [ before the entry for the applicationContext.xml ] and it worked .
- 我将日志配置条目 [上下文参数和侦听器] 放在文件顶部 [在 applicationContext.xml 条目之前] 并且它起作用了。
The Order matters , i guess .
订单很重要,我猜。