How to create multiple log file programatically in log4j2?

后端 未结 2 855
执笔经年
执笔经年 2021-01-23 05:05

I am developing a java application which communicates with lots of devices. For each device I need to create a different log file to log it\'s communication with device. This is

相关标签:
2条回答
  • 2021-01-23 05:10

    The approach log4j2 is initialize programmatically and later configuration is modified is different. And you you trying to add dynamic appender and logger using initialization approach.

    So, first you should initialize your RootLogger using initialization approach that seems correct in your code.

    After that, add dynamic appender and logger using approach mentioned here

    0 讨论(0)
  • 2021-01-23 05:14

    You said your objective is:

    For each device I need to create a different log file to log it's communication with device.

    There are many different ways to accomplish this without programmatic configuration. Programmatic configuration is bad because it forces you to depend on the logging implementation rather than the public interface.

    For example you could use a context map key in conjunction with a Routing Appender to separate your logs, similar to the example I gave in another answer. Note that in the other answer I used the variable as the folder where the log is stored but you can use it for the log name if you wish.

    Another way to do what you want would be to use a MapMessage as shown in the log4j2 manual.

    Yet another way would be to use markers in combination with a RoutingAppender. Here is some example code for this approach:

    package example;
    
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    import org.apache.logging.log4j.Marker;
    import org.apache.logging.log4j.MarkerManager;
    
    public class LogLvlByMarkerMain {
        private static final Logger log = LogManager.getLogger();
        private static final Marker DEVICE1 = MarkerManager.getMarker("DEVICE1");
        private static final Marker DEVICE2 = MarkerManager.getMarker("DEVICE2");
    
        public static void main(String[] args) {
            log.info(DEVICE1, "The first device got some input");
            log.info(DEVICE2, "The second device now has input");
        }
    }
    

    Configuration:

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="WARN">
        <Appenders>
            <Routing name="MyRoutingAppender">
                <Routes pattern="$${marker:}">
                    <Route>
                        <File
                            fileName="logs/${marker:}.txt"
                            name="appender-${marker:}">
                            <PatternLayout>
                                <Pattern>[%date{ISO8601}][%-5level][%t] %m%n</Pattern>
                            </PatternLayout>
                        </File>
                    </Route>
                </Routes>
            </Routing>
            <Console name="STDOUT" target="SYSTEM_OUT">
                <PatternLayout pattern="[%date{ISO8601}][%-5level][%t] %m%n" />
            </Console>
        </Appenders>
        <Loggers>
            <Logger name="example" level="TRACE" additivity="false">
                <AppenderRef ref="STDOUT" />
                <AppenderRef ref="MyRoutingAppender" />
            </Logger>
            <Root level="WARN">
                <AppenderRef ref="STDOUT" />
            </Root>
        </Loggers>
    </Configuration>
    

    Output:

    This will generate 2 log files - DEVICE1.txt and DEVICE2.txt as shown in the image below.

    The first log will contain only messages that were marked as DEVICE1 and the second will contain only DEVICE2 logs.

    I.e. the first log contains:

    [2017-09-21T09:52:04,171][INFO ][main] The first device got some input
    

    and the second contains:

    [2017-09-21T09:52:04,176][INFO ][main] The second device now has input
    
    0 讨论(0)
提交回复
热议问题