jwdvorak
jwdvorak

Reputation: 195

log4j2 files created but not written

I've recently updated log4j in a Java web service app, from v1.2 to v2.6.2; the app is hosted on Tomcat 7.0.21, using Java 7.0_67-b01; coding / compiling using Eclipse Juno (v4.2.0).

I reproduced the log4j.properties functionality in a new lo4j2.xml configuration file. Code samples are below, I'm trying to use a RollingFile appender and a File appender in addition to the standard Console appender.

The log files are being created in the expected location, but they remain empty. The console itself is logging all the messages my app throws at it; the catalina.yyyy-mm-dd.log file is logging Info level and higher. As noted, the tearsLog.log and tearsLogRF.log files are created, but no log messages are written to them at all.

log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
    <Properties>
        <Property name="log-path">${sys:catalina.home}/logs</Property>
    </Properties>
    <Appenders>
        <Console name="Console" 
                target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %t [%t] (%F:%L) - %m%n" />
        </Console>

        <File name="MyFile" 
                append="true" immediateFlush="true" 
                fileName="${log-path}/tearsLog.log">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %t [%t] (%F:%L) - %m%n" />
        </File>

        <RollingFile name="MyRollingFile" 
                append="true" immediateFlush="true" 
                fileName="${log-path}/tearsLogRF.log" 
                filePattern="${log-path}/tearsLogRF_%d{yyyy-MM-dd_HH-mm-ss}.log">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %t [%t] (%F:%L) - %m%n" />
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="100 KB" />
            </Policies>
            <DefaultRolloverStrategy max="10" />
        </RollingFile>
    </Appenders>

    <Loggers>
        <Logger name="us.ak.state.adfg.tears.service.TearsService" level="info" additivity="false">
            <AppenderRef ref="Console" />
            <AppenderRef ref="MyFile" level="trace" />
            <AppenderRef ref="MyRollingFile" />
        </Logger>

        <Logger name="us.ak.state.adfg.tears.data.GenericDao" level="info" additivity="false">
            <AppenderRef ref="MyFile" level="trace" />
            <AppenderRef ref="MyRollingFile" />
        </Logger>

        <Logger name="us.ak.state.adfg.tears.data.EmployeeDao" level="info" additivity="false">
            <AppenderRef ref="MyFile" level="trace" />
            <AppenderRef ref="MyRollingFile" />
        </Logger>

        <Logger name="us.ak.state.adfg.tears.data.TimesheetDao" level="info" additivity="false">
            <AppenderRef ref="MyFile" level="trace" />
            <AppenderRef ref="MyRollingFile" />
        </Logger>

        <Logger name="us.ak.state.adfg.tears.data.SecurityDao" level="info" additivity="false">
            <AppenderRef ref="MyFile" level="trace" />
            <AppenderRef ref="MyRollingFile" />
        </Logger>

        <Root level="debug">
            <AppenderRef ref="Console" />
            <AppenderRef ref="MyFile" level="trace" additivity="false" />
            <AppenderRef ref="MyRollingFile" additivity="false" />
        </Root>
    </Loggers>
</Configuration>

sample *.java class:

package us.ak.state.adfg.tears.service;

import java.util.Date;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


public class TearsService {
    static Logger log = LogManager.getLogger(TearsService.class);

    public TearsService() {
    }

    public int saveTimesheet(User user, Timesheet ts) throws Exception {        
        String msg = "";  
        if (user == null) {
            msg = "Null user.";
            log.error(msg);
            throw new Exception(msg);
        }
        ...
        return 0;
    }

I've been flailing away, looked at many, many code samples, Apache log4j documentation and SO questions/answers, used sample code and suggestions... can't figure out what's wrong. I know it's close, just some little gotcha is keeping me from fixing it an moving on. Would appreciate some help. Thanks in advance...

Upvotes: 0

Views: 2835

Answers (2)

jwdvorak
jwdvorak

Reputation: 195

Ultimately, restarting the Tomcat server magically fixed the issue. Odd that was necessary, the code and log4j module is all self-contained in the app, I thought simply redeploying the app would make it work (it created the files)? Anyway, it seems to be working, now.

Upvotes: 0

asch
asch

Reputation: 1963

Messages on the console you see are probably issued by tomcat. If you are running just this sample code, it is OK that there is nothing in the logs - the only log is done on error. Change your code:

public int saveTimesheet(User user, Timesheet ts) throws Exception {        
    String msg = "";  
    log.info("Message:{}",msg);
    if (user == null) {
        msg = "Null user.";
        log.error(msg);
        throw new Exception(msg);
    }
    ...
    return 0;
}

By the way, you should not define a logger for each class, it is enough a common logger for everything:

 <Loggers>
    <root level="debug">
      ...
    </root>
    <Logger name="us.ak.state.adfg.tears" level="info" additivity="false">
        <AppenderRef ref="Console" />
        <AppenderRef ref="MyFile" level="trace"/>
        <AppenderRef ref="MyRollingFile" />
    </Logger>

 </Loggers>

Upvotes: 2

Related Questions