How to Re-Execute Log4j “Default Initialization Procedure”?

前端 未结 3 1506
暖寄归人
暖寄归人 2021-02-02 11:36

At runtime I often create/modify log4j Loggers, Appenders, Levels, Layouts, and time to time need to reset everything back to defaults.

Log4j

3条回答
  •  故里飘歌
    2021-02-02 12:09

    Jan Zyka's solution pointed me in the right direction, but I'm using an XML configuration file rather than a properties files. Here is the code that worked for me:

        LogManager.resetConfiguration(); // clear any existing config first
        LoggerRepository loggerRepository = LogManager.getLoggerRepository();
        DOMConfigurator domConfigurator = new DOMConfigurator();
        try (
            InputStream is = MyClassName.class.getResourceAsStream("/log4j.xml");
        ) {
            domConfigurator.doConfigure(is, loggerRepository);
        }
        LOGGER.info("abc123");
    

    I get a properly formatted log4j log entry with "abc123" as the log message.

提交回复
热议问题