简体   繁体   English

3.0 servlet中的Log4J2配置

[英]Log4J2 configuration in 3.0 servlet

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 web.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 /类/ 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)

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. 在我的例子中,我没有在web.xml中配置任何context-param。 The only thing I had to do was to set the display name: 我唯一要做的就是设置显示名称:

<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. 我也使用log4j2.yaml而不是xml文件,文件不在战争中。 Also notice that in this page https://logging.apache.org/log4j/2.x/manual/webapp.html they say there is a problem with versions of Tomcat < 7.0.43. 另请注意,在此页面https://logging.apache.org/log4j/2.x/manual/webapp.html中,他们说Tomcat <7.0.43版本存在问题。 So either you have to use a newer version of do a specific configuration. 因此,您必须使用较新版本的特定配置。

Log4J will look for the log4j2.xml config file in the classpath, unless a location is specified. 除非指定了位置, 否则 Log4J将在类路径中查找log4j2.xml配置文件。 Have you tried not specifying 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? 您是否尝试过指定log4j2.xml文件的位置(即从web.xml删除context-param内容),而只是依赖于将配置放在类路径中? ( app.war/WEB-INF/classes/log4j2.xml looks fine to me) app.war/WEB-INF/classes/log4j2.xml对我来说app.war/WEB-INF/classes/log4j2.xml看)

Note that the file must be named log4j2.xml and not log4j.xml . 请注意,该文件必须命名为log4j2.xml而不是log4j.xml

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

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