0
votes

My routing log4j2.xml should role log file based on user login.

Assume a user logs in the application for consecutive 3 days and do his stuff for around half an hour and then logs out. So as per requirement 3 log files should be created for the logged user (one file per day basis in separate file) like e.g.

john-20-11-2015.log, 
john-21-11-2015.log, 
john-22-11-2015.log

below is the Log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Configuration status="WARN" name="MySite" monitorInterval="30">
    <Appenders>

        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout>
                <pattern>[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%c{1}] - %msg%n</pattern>
            </PatternLayout>
        </Console>

        <RollingFile name="error-log" 
            fileName="D:/myLogs/error [${date:yyyy-MM-dd}].log" filePattern="D:/myLogs/error-%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <pattern>[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%c{1}] - %msg%n</pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingFile>

        <RollingFile name="trace-log" 
            fileName="D:/myLogs/trace [${date:yyyy-MM-dd}].log" filePattern="D:/myLogs/trace-%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <pattern>[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%c{1}] - %msg%n</pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingFile>

        <Routing name="RoutingAppender">
            <Routes pattern="$${ctx:logFileName}">
                <Route>
                    <RollingFile name="Rolling-${ctx:logFileName}" append="true"
                        fileName="D:/myLogs/${ctx:logFileName}~${date:yyyy-MM-dd}.log"
                        filePattern="D:/myLogs/%d{yyyy-MM-dd}-%i.log">
                        <PatternLayout
                            pattern="SERVER-LOG: [%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%logger{1}] - %msg%n" />
                        <Policies>
                            <TimeBasedTriggeringPolicy interval="1" modulate="true" />
                        </Policies>
                    </RollingFile>
                </Route>
                <Route ref="Console" key="$${ctx:logFileName}" />
            </Routes>
        </Routing>

    </Appenders>

    <Loggers>
        <Root level="trace" additivity="false">
            <Appender-Ref ref="Console"/>
            <appender-ref ref="error-log" level="error"/>
            <appender-ref ref="trace-log" level="trace"/>
            <Appender-Ref ref="RoutingAppender" />
        </Root>
    </Loggers>
</Configuration>

I am calling

ThreadContext.put("logFileName", userName);

to append the log in the routing appender, the log were appending correctly in the respective logs and I am clearing the threadcontext during logout method

ThreadContext.remove("logFileName");
ThreadContext.clearAll();

I hosted my application in tomcat. log files were generated based on logged user for every user based on the file name pattern, but log is not generated on daily basis, instead of that its appended the current day log of users in previous day log

eg: john-20-11-2015.log contains the log of of 21st and 22nd

its roles a new log file only when tomcat is stop started .

guys help me out.. is there anything wrong

2
shouldn't the thread context variables be put on each request as ThreadContext is per thread?pinkpanther

2 Answers

1
votes

I think you want DailyRollingFileAppender here is an example

Also please look at the similar question

example:

<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
    ...
    <param name="DatePattern" value="'.'yyyy-MM-dd" />
    ...
</appender>
0
votes

The filename attribute is only evaluated when the appender is created. It does not change on each rollover. You should get a new file on each rollover that does have the correct date in it.

Is that not what you are seeing?