开发者

LOG4J Load muliple log4j files

开发者 https://www.devze.com 2023-04-12 20:42 出处:网络
I have five different log4j property files in my application for each event. I do not want the application to load the file using DOM or PropertyConfigurator. But I want to load all these properties i

I have five different log4j property files in my application for each event. I do not want the application to load the file using DOM or PropertyConfigurator. But I want to load all these properties into a Map with event name as key and Properties or Logger as the value. So that when I invoke the getLogger method with the event name the appropriate logger object based on the event name will be returned.

The implementation in this post helps me to some extent. log4j log file names? They are dynamically creating logger ob开发者_运维问答jects based on the job. But I want to make use of the static log4j file for each event and load it and give it back.

I also checked the response in this post. multiple log4j instance configuration But as the event names and the list of appenders for each event will be a huge number in my application, for better maintainability purposes, I am opting for one log4j file for each event.

Expecting your help on this. Thanks, Radhika


I have log4j files for each event defined. Pass the event name and using property configurator load the event specific log4j property file in context. The getLogger method then will have that Logger.

private static synchronized Logger getEventLogger(String eventName) {
Logger logger = null;
    try {

        logger = m_loggers.get(eventName);
        if (logger == null) {
            PropertyConfigurator.configure(eventName + ".properties");
            logger = Logger.getLogger(eventName);
            m_loggers.put(eventName, logger);

        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return logger;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号