[ 
https://issues.apache.org/jira/browse/LOG4J2-510?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14201888#comment-14201888
 ] 

Emmanuel BRUN commented on LOG4J2-510:
--------------------------------------

Hi 

in fact, I really need a solution, because I discover the problem while my App 
was in full production , and it will be too complicated to go back to the 
previous
version.

So i have developped a quick and dirty patch, maybe it could help  someone else

You have to call this function *stopLogger* each time a session is closed. The 
underlying log file will be close at this time.

{code:title=LogUtils.java|borderStyle=solid}
static public void stopLogger(Logger l) 
        {

                l.info("It will be me the last line of the log file !!");

                String key = ThreadContext.get("sessionID");
                
                org.apache.logging.log4j.core.Logger coreLogger = 
(org.apache.logging.log4j.core.Logger) l; 
                org.apache.logging.log4j.core.LoggerContext context = 
(org.apache.logging.log4j.core.LoggerContext)coreLogger.getContext(); 
                RoutingAppender appender = (RoutingAppender) 
context.getConfiguration().getAppender("Routing");
                
                
                try
                {
                        Method method = 
appender.getClass().getDeclaredMethod("getControl",String.class,LogEvent.class);
                        method.setAccessible(true);
                        AppenderControl appenderControl = (AppenderControl) 
method.invoke(appender,key,null);
                        appenderControl.getAppender().stop();
                } 
                catch (NoSuchMethodException | SecurityException | 
IllegalAccessException | IllegalArgumentException | InvocationTargetException e)
                {
                        // Shouldn't happen - log anyway 
                        l.error("Unable to close the logger",e);
                }
                
        }
{code}

Tell me if you see something wrong

Note that the config file should be like this :

{code:title=log4j2.xml|borderStyle=solid}
<Routing name="Routing">
                <Routes pattern="$${ctx:sessionID}">       

                              <Route>
                                        <RollingFile 
name="Rolling-${ctx:sessionID}"
                                                
fileName="./logs/${ctx:sessionID}.log"
                                                filePattern="">
                                                <PatternLayout>
                                                        <pattern>%d: %6p : [%t] 
: [%c{1}] : %m%n</pattern>
                                                </PatternLayout>
                                                <SizeBasedTriggeringPolicy />
                                        </RollingFile>
                                </Route>
                </Routes>
</Routing>  
{code}

Note that if you perform a log after you close the logger, an error will be 
generated but it will not stop your application 


> How to close inactive log files
> -------------------------------
>
>                 Key: LOG4J2-510
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-510
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: Appenders
>    Affects Versions: 2.0-beta9, 2.1
>         Environment: linux
>            Reporter: Eric
>            Assignee: Ralph Goers
>              Labels: performance
>
> Hi There,
> in log4j2 when the App start logging it create and open a file to log into 
> and that file stay open as long as the App is running.
> in my case log4j2 create and open too many files base on ThreadContext and 
> the OS (linux) will complaint that too many files are open and my App will 
> crash and stop responding.
> My question is:
>  1 -  how do you close those files with log4j2 if they are inactive for a 
> period of time.
>  2 - How do you have access to the log4j2 handle that open those files.
>  3 - How do you tell log4j2 to open a file just for a period of time.
> Thanks
> Eric



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to