Question

I'm trying to configure log4j2 to write a rolling log file to disk, but I can't get it to work. No log file appears at the given path and the Glassfish server.log doesn't show any Spring logging at all. I've read a lot of similar questions on SO, but none of the proposed solutions have worked in this case. Can anyone help me? I'm using Spring 3.0 on a Glassfish 3.1 application server.

From my pom.xml:

<properties>
    <junit.version>4.11</junit.version>
    <tiles.version>3.0.3</tiles.version>
    <slf4j.version>1.7.5</slf4j.version>
    <log4j.version>2.0-beta9</log4j.version>
</properties>
<dependencies>
    <dependency>
        <artifactId>jcl-over-slf4j</artifactId>
        <groupId>org.slf4j</groupId>
        <version>${slf4j.version}</version>
    </dependency>
    <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-slf4j-impl</artifactId>
        <version>${log4j.version}</version>
    </dependency>
</dependencies>

And here's my log4j.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Properties>
        <Property name="fileName" value="C:/temp/rolling-file.log"/>
        <Property name="fileNamePattern" value="C:/temp/rolling-file-$d{dd-MM-yyyy}-%i.log"/>
        <Property name="logPattern" value="%d{dd-MM-yyyy HH:mm:ss,SSS} [%t] %-5p %c - %m%n"/>
    </Properties>

    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="${logPattern}"/>
        </Console>
        <RollingFile name="RollingFile" fileName="${fileName}" filePattern="${fileNamePattern}">
            <PatternLayout pattern="${logPattern}"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="20MB"/>
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Logger name="my.root.package" level="info" additivity="false">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="RollingFile"/>
        </Logger>
        <Root level="info">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>
Was it helpful?

Solution

Besides the excellent point made by M. Deinum, it turns out my log4j2.xml needed to be changed to the following:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
    <Properties>
        <Property name="fileName">C:/temp/rolling-file.log</Property>
        <Property name="fileNamePattern">C:/temp/rolling-file-%d{dd-MM-yyyy}-%i.log</Property>
        <Property name="logPattern">%d{dd-MM-yyyy HH:mm:ss,SSS} [%t] %-5p %c - %m%n</Property>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="${logPattern}"/>
        </Console>
        <RollingFile name="MyRollingFile" fileName="${fileName}" filePattern="${fileNamePattern}">
            <PatternLayout pattern="${logPattern}"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="20MB"/>
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Logger name="my.root.package" level="info" additivity="false">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="MyRollingFile"/>
        </Logger>
        <Root level="info">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

Performed changes:

  • The value attributes of the Property elements were not parsed correctly. Specifying them as body of the Property element seems to solve this.
  • There was a mistake in the value of the fileNamePattern property. I used $d to denote the date, but it should be %d.
  • Apparently log4j2 doesn't like a RollingFile element with the name "RollingFile". After I changed it, logging started working.

OTHER TIPS

You might need to include log4j-api to the pom dependencies (http://logging.apache.org/log4j/2.x/faq.html#which_jars ).

About the properties: if the problem goes away by putting the values in the config directly:

<RollingFile name="RollingFile" fileName="C:/temp/rolling-file.log" 
             filePattern="C:/temp/rolling-file-$d{dd-MM-yyyy}-%i.log">
    <PatternLayout pattern="%d{dd-MM-yyyy HH:mm:ss,SSS} [%t] %-5p %c - %m%n"/>

then you may have found a bug. In that case, could your raise this issue in the log4j2 Jira issue tracker?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top