You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by levtbinh <br...@mail.rakuten.com> on 2013/07/22 05:38:50 UTC

[Log4j2] How to change the appender file name at run time

In my application, I would want to save log of different sources to different
files without knowing the file names before hand.

How should I do that with Log4j2?

I have tried the following config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <properties>
        <property name="logDir">/home/ad/log</property>
        <property name="pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} | %-5.5p |
%-10.10t | %C:%-5.5L | %msg%n</property>
    </properties>
    <appenders>
        
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="${pattern}"/>
        </Console>
        
        
        <File name="Debug" fileName="${sys.logFileName}">
            <PatternLayout>
                <pattern>${pattern}</pattern>
            </PatternLayout>
        </File>
        <File name="Product" fileName="${sys.logFileName}">
            <PatternLayout>
                <pattern>${pattern}</pattern>
            </PatternLayout>
        </File>
    </appenders>
    <loggers>
        <logger name="productLogger" level="INFO" additivity="false">
            <appender-ref ref="Product"/>
        </logger>
        <logger name="debugLogger" level="DEBUG" additivity="false">
            <appender-ref ref="Console"/>
            <appender-ref ref="Debug"/>
        </logger>
        
        <root level="info"> 
            <appender-ref ref="Console"/> 
        </root>
    </loggers>
</configuration>

And tried to set the sys.logFileName property in the code but it doesn't
work

System.setProperty("logFileName", fileName);
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
ctx.reconfigure();

Thank you.




--
View this message in context: http://apache-logging.6191.n7.nabble.com/Log4j2-How-to-change-the-appender-file-name-at-run-time-tp39025.html
Sent from the Log4j - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: [Log4j2] How to change the appender file name at run time

Posted by Remko Popma <re...@yahoo.com>.
You can use the RoutingAppender. 
Take a look at the last comment (source & config example) in this Jira ticket:
https://issues.apache.org/jira/browse/LOG4J2-314


Sent from my iPhone

On 2013/07/22, at 12:38, levtbinh <br...@mail.rakuten.com> wrote:

> In my application, I would want to save log of different sources to different
> files without knowing the file names before hand.
> 
> How should I do that with Log4j2?
> 
> I have tried the following config
>