How to configure Logger Programmatically in log4j2.02?

后端 未结 3 415
北恋
北恋 2021-02-05 14:05

I want to use log4j without any configure file. What I wan to do is something as:

logger = (Logger) LogManager.getLogger(this.getClass());
Strin         


        
3条回答
  •  一生所求
    2021-02-05 14:44

    The official documentation shows an example : Programatically Adding to the Current Configuration

    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Configuration config = ctx.getConfiguration();
    
    Layout layout = PatternLayout.createLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN, config, null, null,null, null);
    Appender appender = FileAppender.createAppender("target/test.log", "false", "false", "File", "true", "false", "false", "4000", layout, null, "false", null, config);
    appender.start();
    config.addAppender(appender);
    
    AppenderRef ref = AppenderRef.createAppenderRef("File", null, null);
    AppenderRef[] refs = new AppenderRef[] {ref};
    LoggerConfig loggerConfig = LoggerConfig.createLogger("false", "info", "org.apache.logging.log4j", "true", refs, null, config, null );
    loggerConfig.addAppender(appender, null, null);
    config.addLogger("org.apache.logging.log4j", loggerConfig);
    ctx.updateLoggers();
    

    With these limitations :

    1. If the configuration file is changed the configuration will be reloaded and the manual changes will be lost.
    2. Modification to the running configuration requires that all the methods being called (addAppender and addLogger) be synchronized.

    This solution avoids to use method from the core implementation org.apache.logging.log4j.core.Logger, and it avoids dirty cast like that :

    import org.apache.logging.log4j.Logger;
    
    Logger logger = (Logger) LogManager.getLogger(this.getClass());
    ((org.apache.logging.log4j.core.Logger) logger).addAppender(...); // Bypassing the public API 
    

提交回复
热议问题