Java 3.0 servlet 中的 Log4J2 配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21899836/
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
Log4J2 configuration in 3.0 servlet
提问by Leos Literak
I tried to set up LOG4J according documentation (and related SO questions), but it does not create supposed file, but there is such log in WildFly:
我试图根据文档(和相关的 SO 问题)设置 LOG4J,但它没有创建假定的文件,但 WildFly 中有这样的日志:
No Log4j context configuration provided. This is very unusual
web.xml
网页.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j2.xml</param-value>
</context-param>
app.war/WEB-INF/classes/log4j2.xml
app.war/WEB-INF/classes/log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="30">
<!-- http://logging.apache.org/log4j/2.x/manual/configuration.html -->
<Properties>
<Property name="filename">c:/oauth.log</Property>
</Properties>
<Filter type="ThresholdFilter" level="trace"/>
<Appenders>
<Appender type="File" name="File" fileName="${filename}">
<Layout type="PatternLayout">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</Layout>
</Appender>
<File name="MyFile" fileName="c:/oauth2.log" immediateFlush="true">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Logger name="cz.literak.demo" level="debug" additivity="true">
<AppenderRef ref="File"/>
</Logger>
<Root level="error">
<AppenderRef ref="MyFile"/>
</Root>
</Loggers>
</Configuration>
app.war/WEB-INF/lib
app.war/WEB-INF/lib
commons-logging-1.1.3.jar
json-smart-1.1.1.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
log4j-jcl-2.0-rc1.jar
Could you tell me what is wrong? I tried to comment out context param in web.xml and rely on autoconfiguration but there is no change.
你能告诉我有什么问题吗?我试图在 web.xml 中注释掉上下文参数并依赖自动配置,但没有任何变化。
EDIT
编辑
when I added following code
当我添加以下代码时
<context-param>
<param-name>log4jContextName</param-name>
<param-value>oauthDemo</param-value>
</context-param>
it failed differently (I do not have time to investigate now)
它以不同的方式失败(我现在没有时间进行调查)
07:41:29,269 INFO [io.undertow.servlet] (MSC service thread 1-12) Log4jServletContainerInitializer starting up Log4j in Servlet 3.0+ environment.
07:41:29,644 INFO [stdout] (MSC service thread 1-12) 2014-02-20 07:41:29,643 ERROR FileManager (c:/oauth2.log) java.io.FileNotFoundException: c:\oauth2.log (P???-stup byl odep??en)
07:41:29,646 INFO [stdout] (MSC service thread 1-12) 2014-02-20 07:41:29,645 ERROR Unable to invoke method createAppender in class org.apache.logging.log4j.core.appender.FileAppender for element File 07:41:29,647 INFO [stdout] (MSC service thread 1-12) at org.apache.logging.log4j.core.config.BaseConfiguration.createPluginObject(BaseConfiguration.java:913)
回答by Remko Popma
Log4J will look for the log4j2.xml config file in the classpath, unlessa location is specified.
Have you tried notspecifying the location of the log4j2.xml file (that is, remove the context-param
stuff from web.xml
), and simply relying on putting the config in the classpath? (app.war/WEB-INF/classes/log4j2.xml
looks fine to me)
Log4J 将在类路径中查找 log4j2.xml 配置文件,除非指定了位置。您是否尝试过不指定 log4j2.xml 文件的位置(即context-param
从 中删除内容web.xml
),而仅仅依靠将配置放在类路径中?(app.war/WEB-INF/classes/log4j2.xml
对我来说看起来不错)
Note that the file mustbe named log4j2.xml
and not log4j.xml
.
请注意,该文件必须命名,log4j2.xml
而不是log4j.xml
.
回答by Constantino Cronemberger
As a reference this page describes how to configure Log4j2:
作为参考,此页面描述了如何配置 Log4j2:
https://logging.apache.org/log4j/2.x/manual/webapp.html#ContextParams
https://logging.apache.org/log4j/2.x/manual/webapp.html#ContextParams
In my case I have not configured any context-param in web.xml. The only thing I had to do was to set the display name:
就我而言,我没有在 web.xml 中配置任何上下文参数。我唯一要做的就是设置显示名称:
<display-name>My Admin API</display-name>
I am also using log4j2.yaml instead of xml files and the file is not inside the war. Also notice that in this page https://logging.apache.org/log4j/2.x/manual/webapp.htmlthey say there is a problem with versions of Tomcat < 7.0.43. So either you have to use a newer version of do a specific configuration.
我也使用 log4j2.yaml 而不是 xml 文件,并且该文件不在战争中。另请注意,在此页面https://logging.apache.org/log4j/2.x/manual/webapp.html 中,他们说 Tomcat < 7.0.43 的版本存在问题。所以要么你必须使用较新版本的做一个特定的配置。