2014-06-30 6 views
7

Ich habe versucht, Appender programmatisch zu konfigurieren, aber bisher keinen Erfolg. Ich möchte nur appender ohne Datei konfigurieren, so dass es Protokolle an die Konsole oder Datei senden kann.Wie programmiere Appender programmatisch oder initialisiere Logging in log4j2?

String PATTERN = "%d [%p|%c|%C{1}] %m%n"; 
    PatternLayout layout = PatternLayout.createLayout(PATTERN, null, null, 
      null, null, null); 
    ConsoleAppender console = ConsoleAppender.createAppender(layout, null, 
      null, "console", "true", "true"); // create appender 

    AppenderRef appender = AppenderRef.createAppenderRef("console", 
      "DEBUG", null); 

    logger = (Logger) LogManager.getLogger(InitLogger.class); 
    LoggerContext context = logger.getContext(); 
    BaseConfiguration configuration =(BaseConfiguration) context.getConfiguration(); 
    configuration.addAppender(console); 
    logger.addAppender(configuration.getAppender("console")); 
+1

Sie benötigen Appen zum rootLogger zurücksetzen und hinzufügen. Siehe [diesen Beitrag] (http://stackoverflow.com/questions/8965946/configuring-log4j-loggers-programmatisch) – Stefan

+0

Dieser Beitrag spricht über log4j1, also ist dies nicht hilfreich. Diese – Akhilesh

+1

war möglich mit Hilfe von ConfigurationFactory von log4j2 [link] [1] [1]: http://stackoverflow.com/questions/20886717/configure-log4j2-programmatically-using-configurationfactory – Akhilesh

Antwort

4

Versuchen Sie den folgenden Beispielcode

import org.apache.logging.log4j.Level; 
import org.apache.logging.log4j.core.appender.ConsoleAppender; 
import org.apache.logging.log4j.core.config.AppenderRef; 
import org.apache.logging.log4j.core.config.Configuration; 
import org.apache.logging.log4j.core.config.LoggerConfig; 
import org.apache.logging.log4j.core.layout.PatternLayout; 
import org.apache.logging.log4j.spi.ExtendedLogger; 
import org.apache.logging.log4j.spi.LoggerContext; 

public class TestLog4j2 { 

    public static void main(String[] args) { 
     try { 

      ConsoleAppender console = ConsoleAppender.createAppender(PatternLayout.createDefaultLayout(), null, "SYSTEM_OUT", "console", null, null); 
      final LoggerContext ctx = (LoggerContext) new org.apache.logging.log4j.core.LoggerContext("console"); 
       final Configuration config = ((org.apache.logging.log4j.core.LoggerContext) ctx).getConfiguration(); 
       console.start(); 
       config.addAppender(console); 
       AppenderRef ref = AppenderRef.createAppenderRef("console", null, null); 
       AppenderRef[] refs = new AppenderRef[] {ref}; 
       LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.ALL, "org.apache.logging.log4j", 
        "true", refs, null, config, null); 
       loggerConfig.addAppender(console, null, null); 
       config.addLogger("org.apache.logging.log4j", loggerConfig); 
       ((org.apache.logging.log4j.core.LoggerContext) ctx).updateLoggers(); 
       ExtendedLogger logger = (ExtendedLogger) ctx.getLogger("console"); 
       logger.error("abc"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
6

Hier ist der einfachste Weg, dies zu tun:

import org.apache.logging.log4j.Level; 
import org.apache.logging.log4j.LogManager; 
import org.apache.logging.log4j.Logger; 
import org.apache.logging.log4j.core.LoggerContext; 
import org.apache.logging.log4j.core.appender.ConsoleAppender; 
import org.apache.logging.log4j.core.config.AbstractConfiguration; 
import org.apache.logging.log4j.core.config.AppenderRef; 
import org.apache.logging.log4j.core.config.LoggerConfig; 
import org.apache.logging.log4j.core.layout.PatternLayout; 

public class Main { 

    public static void main(String[] args) { 
     configure(); 
     Logger logger = LogManager.getLogger("com.company"); 
     logger.trace("Hello Word!"); 

    } 

    public static void configure() { 
     LoggerContext ctx = (LoggerContext) LogManager.getContext(false); 
     AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration(); 
     ConsoleAppender appender = ConsoleAppender.createDefaultAppenderForLayout(PatternLayout.createDefaultLayout()); 
     appender.start(); 
     config.addAppender(appender); 
     AppenderRef[] refs = new AppenderRef[] { AppenderRef.createAppenderRef(appender.getName(), null, null) }; 
     LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.ALL, LogManager.ROOT_LOGGER_NAME, "true", refs, null, config, null); 
     loggerConfig.addAppender(appender, null, null); 
     config.addLogger(LogManager.ROOT_LOGGER_NAME, loggerConfig); 
     ctx.updateLoggers(); 
    } 
} 
+0

Wie wäre es, diesen Logger ASYNC zu machen? –