0

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

Gergely Bacso
  • 14,243
  • 2
  • 44
  • 64
Rajakrishnan
  • 161
  • 1
  • 4
  • 13

2 Answers2

1

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>
Community
  • 1
  • 1
k0ner
  • 1,086
  • 7
  • 20
  • Exactly DailyRollingFileAppender , but i want it in the log4j2.xml. i have implemented the routing concept in my xml , What i need is routing file should be rolled daily... – Rajakrishnan Nov 26 '15 at 11:23
  • So, could you post your solution below? – k0ner Nov 26 '15 at 11:28
  • I don't have any solution. but my log4j2.xml is already in the question. i want the routing appender part should role file each day as per requirement. but it doesn't . Could you have a look at it whats wrong in it ? – Rajakrishnan Nov 26 '15 at 14:33
0

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?

rgoers
  • 8,696
  • 1
  • 22
  • 24
  • I am newbie for Log4j2 concept.Internet as a source I learned a little and formed the log4j2.xml. As per my understanding the is for rolling the new file , correct me if am wrong. My issue is, new file is not rolled everyday and the logs were keep on appending in the same log file created at first. if there is a stop start in the deployed env (say tomcat) then new files were rolled with new date and keeps appending the log as stated above . – Rajakrishnan Nov 30 '15 at 11:02