Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am migrating application (huge web application) from log4j1.x to log4j 2.11.0.

I need help/clarification to migrate following scenarios -

--------scenario1 log4j1.x:

<appender name="import_log_file" class="xxxx">

During runtime i want to change the file so i would just do appender.setFile(...new file...). And done. log4j2: how do I migrate above code?

Few ideas but not a straight answer: Creating brand new appender via LoggerContext, Configuration might be a way but i want to update an existing appender's configuration and reload the log4j2 xml. How do I do that?

Another way could be redefining something like this

<appender name="import_log_file" class="xxxx">

And then setup "dynamic_name" property in threadcontext. But I am loosing original default file import.log

Any help is appreciated. Let me know if you have ideas.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
224 views
Welcome To Ask or Share your Answers For Others

1 Answer

During runtime i want to change the file

You can use the RoutingAppender together with a lookup to do this. See the log4j2 FAQ page for details.

Here is a very simple example of how to change the log file name at runtime:

package pkg;

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

public class Log4j2DiffFilePerCtxVarMain {
    private static final Logger LOG = LogManager.getLogger();

    public static void main(String[] args){

        ThreadContext.put("myFileNameVar", "file1");
        LOG.info("This should appear in file1.log");

        ThreadContext.put("myFileNameVar", "file2");
        LOG.info("This should appear in file2.log");
    }
}

The configuration looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Routing name="myAppender">
            <Routes pattern="$${ctx:myFileNameVar}">
                <Route>
                    <File
                        fileName="logs/${ctx:myFileNameVar}.log"
                        name="myAppender-${ctx:myFileNameVar}">
                        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
                    </File>
                </Route>
            </Routes>
        </Routing>
    </Appenders>
    <Loggers>
        <Root level="trace">
            <AppenderRef ref="myAppender" />
        </Root>
    </Loggers>
</Configuration>

The result of running the above code will be two files - file1.log an file2.log each with a single entry. The file1.log will contain the first log message and file2.log will contain the second message.

Hope this helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...