Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Before adding context-param and Log4jConfigListener for Log4j2, I had all logs listed in catalina.out file.

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

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

<?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

<?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.xml use classpath*:log4j.xml

And had an error 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.

like image 721
slisnychyi Avatar asked Feb 21 '14 13:02

slisnychyi


1 Answers

You did not specify Spring version and Servler version..

Since Spring 4.2.1 the Log4jConfigListener is deprecated.

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

  1. add new dependency log4j-web,
  2. a) put log4j2.xml file into WEB-INF folder OR b) put your log4j2 configuration into classpath and define log4jConfiguration context param within web.xml with configuration file specified.

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

Here comes the Log4j2's documentation which describes it pretty well.

like image 175
franta kocourek Avatar answered Oct 04 '22 17:10

franta kocourek