Java web.xml 中的 Log4j2.xml 配置。IllegalArgumentException - uri 不是绝对的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21935447/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:29:04  来源:igfitidea点击:

Log4j2.xml configuration in web.xml. IllegalArgumentException - uri is not absolute

javaweb-applicationsclasspathweb.xmllog4j2

提问by slisnychyi

Before adding context-param and Log4jConfigListenerfor Log4j2, I had all logs listed in catalina.outfile.

在添加context-param 和 Log4jConfigListenerfor 之前Log4j2,我在catalina.out文件中列出了所有日志。

So now, I'm trying to configure log4jfor my web application.

所以现在,我正在尝试log4j为我的 Web 应用程序进行配置。

In web.xml, I've added necessary configuration.

在 中web.xml,我添加了必要的配置。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_2_5.xsd">
    <display-name>etl-service</display-name>

    <!-- Support for Spring -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>

    <context-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>myprofile</param-value>
    </context-param>

    <context-param>
        <param-name>log4jConfiguration</param-name>
        <param-value>/WEB-INF/classes/log4j2.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <listener>
        <listener-class>
            org.springframework.web.util.Log4jConfigListener
        </listener-class>
    </listener>


</web-app>

log4j2.xml

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace" strict="true" name="XMLConfigTest"
               packages="">         

    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>

        <RollingRandomAccessFile name="RollingRandomAccessFileDebug" fileName="/local/deploy/logs/debug.log"
                                 filePattern="logs/$${date:yyyy-MM}/etl-%d{MM-dd-yyyy}-%i.log.gz"
                                 immediateFlush="false"
                                 append="false">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="250 MB"/>
            </Policies>
        </RollingRandomAccessFile>
        <RollingRandomAccessFile name="RollingRandomAccessFile" fileName="/local/deploy/logs/info.log"
                                 filePattern="logs/$${date:yyyy-MM}/etl-%d{MM-dd-yyyy}-%i.log.gz"
                                 immediateFlush="false"
                                 append="false">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="250 MB"/>
            </Policies>
        </RollingRandomAccessFile>
        <!--<Async name="AsyncConsole">-->
        <!--<AppenderRef ref="Console"/>-->
        <!--</Async>-->
    </Appenders>
    <Loggers>
        <Root  level="TRACE">
            <AppenderRef ref="RollingRandomAccessFileDebug" level="DEBUG"/>
            <AppenderRef ref="RollingRandomAccessFile" level="INFO"/>
            <AppenderRef ref="Console"  level="TRACE"/>
        </Root>
    </Loggers>
</Configuration>

But for now I have the following error:

但现在我有以下错误:

ERROR Unable to access WEB-INF/classes/log4j2.xml java.lang.IllegalArgumentException: URI is not absolute

Maybe someone already faced with such issue. Also I've tried instead adding /WEB-INF/classes/log4j.xmluse classpath*:log4j.xml

也许有人已经面临这样的问题。我也试过添加/WEB-INF/classes/log4j.xml使用classpath*:log4j.xml

And had an errorjava.net.URISyntaxException: Illegal character in schema name at 9 index: classpath*:log4j.xml

并有错误java.net.URISyntaxException: Illegal character in schema name at 9 index: classpath*:log4j.xml

UPDATE:We need to add just descriptor file name and it works.

更新:我们只需要添加描述符文件名就可以了。

<context-param>
            <param-name>log4jConfiguration</param-name>
            <param-value>log4j2.xml</param-value>
        </context-param>

But issue remineds logs listed in catalina.out file. Not in my log4j files.

但问题提醒在 catalina.out 文件中列出的日志。不在我的 log4j 文件中。

回答by Augustin Ghauratto

Notice, that your log4j configuration file name is log4j2.xml, so trying to point to log4j.xml is quite pointless. Please find that unless using servlet container below 2.5, your log4j may be configurable by itself.

请注意,您的 log4j 配置文件名为 log4j 2.xml,因此尝试指向 log4j.xml 毫无意义。请注意,除非使用低于 2.5 的 servlet 容器,否则您的 log4j 可能可以自行配置。

First of all, verify if your log4j configuration is correct. Start container with -Dlog4j.configurationFilepointing to your log4j2.xml. You can also add debug with -Dorg.apache.logging.log4j.simplelog.StatusLogger.level=debug

首先验证你的log4j配置是否正确。-Dlog4j.configurationFile指向您的 log4j2.xml启动容器。您还可以添加调试-Dorg.apache.logging.log4j.simplelog.StatusLogger.level=debug

Using tomcat, edit your ${catalina.home}/bin/setenv.sh (or .bat, if using windows) and add it to JAVA_OPTS variable.

使用 tomcat,编辑您的 ${catalina.home}/bin/setenv.sh(或 .bat,如果使用 Windows)并将其添加到 JAVA_OPTS 变量。

If your log4j will start logging both to file and console, try removing both:

如果您的 log4j 将同时开始记录到文件和控制台,请尝试删除两者:

<context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>/WEB-INF/classes/log4j2.xml</param-value>
</context-param>
<context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>/WEB-INF/classes/log4j2.xml</param-value>
</context-param>

as well as:

也:

<listener>
    <listener-class>
        org.springframework.web.util.Log4jConfigListener
    </listener-class>
</listener>
<listener>
    <listener-class>
        org.springframework.web.util.Log4jConfigListener
    </listener-class>
</listener>

Spring is based on log4j 1.x, so add dependencies to your build (maven example):

Spring 基于 log4j 1.x,因此在您的构建中添加依赖项(maven 示例):

        <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${log4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-web</artifactId>
        <version>${log4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-1.2-api</artifactId>
        <version>${log4j.version}</version>
    </dependency>

Where log4j-1.2-apiis compatibility api, and log4j-webis web application support.

log4j-1.2-api兼容性 api在哪里,以及log4j-webWeb 应用程序支持。

回答by Satyanarayana Midatala

See the source code of org.springframework.web.util.Log4jConfigListener. It expects a system property by name "log4jConfigLocation". This property should hold the location of the file if it's in classpath then its value should be classpath:/logs/log4j2.xml. If it's a file resource then it should be file://Users/logs/log4j2.xml. So add a context parm in web.xml. For example:

查看 org.springframework.web.util.Log4jConfigListener 的源代码。它需要一个名为“log4jConfigLocation”的系统属性。如果文件在类路径中,则此属性应保存文件的位置,则其值应为 classpath:/logs/log4j2.xml。如果它是文件资源,那么它应该是 file://Users/logs/log4j2.xml。所以在 web.xml 中添加一个上下文参数。例如:

<context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>file://Users/logs/log4j2.xml</param-value>
 </context-param>

Also note that spring's Log4jConfigListener is works with log4j 1.x version.

另请注意,spring 的 Log4jConfigListener 适用于 log4j 1.x 版本。

回答by franta kocourek

You did not specify Spring version and Servler version..

您没有指定 Spring 版本和服务器版本..

Since Spring 4.2.1 the Log4jConfigListeneris deprecated.

从 Spring 4.2.1 开始Log4jConfigListener不推荐使用。

Let's assume you are using Spring version 4.2.1(+), Servlet 3.0(+) and Log4j2. Now all you have to do is to:

假设您使用的是 Spring 版本 4.2.1(+)、Servlet 3.0(+) 和 Log4j2。现在您要做的就是:

  1. add new dependency log4j-web,
  2. a) put log4j2.xml file into WEB-INF folder ORb) put your log4j2 configuration into classpath and define log4jConfigurationcontext param within web.xml with configuration file specified.
  1. 添加新的依赖log4j-web
  2. a) 将log4j2.xml 文件放入 WEB-INF 文件夹b) 将您的 log4j2 配置放入类路径并log4jConfiguration在指定配置文件的 web.xml 中定义上下文参数。

So you don't have to specify any listener at all.

因此,您根本不必指定任何侦听器。

Here comes the Log4j2's documentationwhich describes it pretty well.

这是Log4j2 的文档,它很好地描述了它。