當前位置: 首頁>>代碼示例>>Java>>正文


Java Configuration.addAppender方法代碼示例

本文整理匯總了Java中org.apache.logging.log4j.core.config.Configuration.addAppender方法的典型用法代碼示例。如果您正苦於以下問題:Java Configuration.addAppender方法的具體用法?Java Configuration.addAppender怎麽用?Java Configuration.addAppender使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.logging.log4j.core.config.Configuration的用法示例。


在下文中一共展示了Configuration.addAppender方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: addFileAppender

import org.apache.logging.log4j.core.config.Configuration; //導入方法依賴的package包/類
private static void addFileAppender() throws IOException {
  final String tempDir = System.getProperty("java.io.tmpdir");
  final File logFile = new File(tempDir, "meghanada_server.log");
  final LoggerContext context = (LoggerContext) LogManager.getContext(false);
  final Configuration configuration = context.getConfiguration();
  final LoggerConfig loggerConfig = configuration.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
  final FileAppender fileAppender =
      FileAppender.newBuilder()
          .withName("file")
          .withLayout(
              PatternLayout.newBuilder()
                  .withPattern("[%d][%-5.-5p][%-14.-14c{1}:%4L] %-22.-22M - %m%n")
                  .build())
          .withFileName(logFile.getCanonicalPath())
          .build();
  configuration.addAppender(fileAppender);
  loggerConfig.addAppender(fileAppender, Level.ERROR, null);
  context.updateLoggers();
}
 
開發者ID:mopemope,項目名稱:meghanada-server,代碼行數:20,代碼來源:Main.java

示例2: configureLogging

import org.apache.logging.log4j.core.config.Configuration; //導入方法依賴的package包/類
protected void configureLogging() {
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Configuration config = ctx.getConfiguration();
    Layout layout = PatternLayout.createLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN, null, config, null, null,
            true, false, null, null);
    Appender appender = FileAppender.createAppender(workDir + "/logs/camel-standalone.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", Level.INFO, "StandaloneFileLoggerConfig",
            "true", refs, null, config, null );
    loggerConfig.addAppender(appender, null, null);
    config.addLogger("StandaloneFileLoggerConfig", loggerConfig);
    ctx.updateLoggers();
}
 
開發者ID:Steve973,項目名稱:camel-standalone,代碼行數:18,代碼來源:Standalone.java

示例3: configureLog4j

import org.apache.logging.log4j.core.config.Configuration; //導入方法依賴的package包/類
private static Logger configureLog4j() {
    LoggerContext context = (LoggerContext) LogManager.getContext();
    Configuration config = context.getConfiguration();

    PatternLayout layout = PatternLayout.createLayout("%m%n", null, null, Charset.defaultCharset(), false, false, null, null);
    Appender appender = ConsoleAppender.createAppender(layout, null, null, "CONSOLE_APPENDER", null, null);
    appender.start();
    AppenderRef ref = AppenderRef.createAppenderRef("CONSOLE_APPENDER", null, null);
    AppenderRef[] refs = new AppenderRef[]{ref};
    LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.INFO, "CONSOLE_LOGGER", "com", refs, null, null, null);
    loggerConfig.addAppender(appender, null, null);

    config.addAppender(appender);
    config.addLogger("Main.class", loggerConfig);
    context.updateLoggers(config);
    return LogManager.getContext().getLogger("Main.class");
}
 
開發者ID:vitaly-chibrikov,項目名稱:homework_tester,代碼行數:18,代碼來源:Main.java

示例4: bindLogger

import org.apache.logging.log4j.core.config.Configuration; //導入方法依賴的package包/類
public static void bindLogger() {
    LoggerContext context = (LoggerContext) LogManager.getContext(false);
    Configuration config = context.getConfiguration();

    Map<String, ESLogger> loggers = Maps.newConcurrentHashMap();
    Appender appender = new AbstractAppender("", null, null) {
        @Override
        public void append(LogEvent event) {
            String name = event.getLoggerName();
            ESLogger logger = loggers.computeIfAbsent(name, key -> new ESLogger(key, null, (LoggerImpl) LoggerFactory.getLogger(key)));
            logger.log(event.getLevel(), event.getMarker(), event.getMessage(), event.getThrown());
        }
    };
    appender.start();
    config.addAppender(appender);

    LoggerConfig loggerConfig = new LoggerConfig("", Level.ALL, false);
    loggerConfig.addAppender(appender, null, null);
    config.addLogger("", loggerConfig);
    context.updateLoggers();
}
 
開發者ID:neowu,項目名稱:core-ng-project,代碼行數:22,代碼來源:ESLoggerConfigFactory.java

示例5: setUpAppender

import org.apache.logging.log4j.core.config.Configuration; //導入方法依賴的package包/類
@Before
public void setUpAppender() throws IOException {
    file = folder.newFile();

    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Configuration config = ctx.getConfiguration();
    final LoggerConfig root = ((AbstractConfiguration) config).getRootLogger();

    PatternLayout layout = PatternLayout.createLayout("[%p] %m%n", config, null, null, true, false, null, null);
    FileAppender appender = FileAppender.createAppender(file.getAbsolutePath(), "true", "false", "TestLogFile", "true", "false", "false", "8192", layout, null, "false", null, config);

    appender.start();
    config.addAppender(appender);

    root.addAppender(appender, null, null);
    ctx.updateLoggers();
}
 
開發者ID:DjDCH,項目名稱:Log4j-StaticShutdown,代碼行數:18,代碼來源:Log4jShutdownTest.java

示例6: createFileAppender

import org.apache.logging.log4j.core.config.Configuration; //導入方法依賴的package包/類
private void createFileAppender(DiagnosticContext ctx) {

      String logDir = ctx.getTempDir() + SystemProperties.fileSeparator + "diagnostics.log";

      final LoggerContext context = (LoggerContext) LogManager.getContext(false);
      final Configuration config = context.getConfiguration();
      Layout layout = PatternLayout.createLayout("%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n", null, config, null,
         null,true, true, null, null );
      Appender appender = FileAppender.createAppender(logDir, "false", "false", "File", "true",
         "false", "false", "0", layout, null, "false", null, config);
      appender.start();
      config.addAppender(appender);
      AppenderRef ref = AppenderRef.createAppenderRef("File", null, null);
      AppenderRef[] refs = new AppenderRef[] {ref};

      config.getRootLogger().addAppender(appender, null, null);
      context.updateLoggers();
      Logger testLog = LogManager.getLogger();
      testLog.error("Testing");
   }
 
開發者ID:elastic,項目名稱:elasticsearch-support-diagnostics,代碼行數:21,代碼來源:Diagnostic.java

示例7: testUpdatePatternWithFileAppender

import org.apache.logging.log4j.core.config.Configuration; //導入方法依賴的package包/類
/**
 * Validates that the code pattern we use to add an appender on the fly
 * works with a basic appender that is not the new OutputStream appender or
 * new Writer appender.
 */
@Test
public void testUpdatePatternWithFileAppender() {
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Configuration config = ctx.getConfiguration();
    // @formatter:off
    final Appender appender = FileAppender.newBuilder()
        .withFileName("target/" + getClass().getName() + ".log")
        .withAppend(false)
        .withName("File")
        .withIgnoreExceptions(false)
        .withBufferedIo(false)
        .withBufferSize(4000)
        .setConfiguration(config)
        .build();
    // @formatter:on
    appender.start();
    config.addAppender(appender);
    ConfigurationTestUtils.updateLoggers(appender, config);
    LogManager.getLogger().error("FOO MSG");
}
 
開發者ID:apache,項目名稱:logging-log4j2,代碼行數:26,代碼來源:OutputStreamAppenderTest.java

示例8: addFollowConsoleAppender

import org.apache.logging.log4j.core.config.Configuration; //導入方法依賴的package包/類
private void addFollowConsoleAppender() {
    final LoggerContext context = LoggerContext.getContext(false);
    final Configuration config = context.getConfiguration();
    final PatternLayout layout = PatternLayout.newBuilder().withPattern("%-5level %c{-4} - %msg%n").build();
    Appender appender = ConsoleAppender.newBuilder().setFollow(true).setTarget(ConsoleAppender.Target.SYSTEM_OUT)
            .withName("ExecutionRecorder").withLayout(layout).build();
    appender.start();
    config.addAppender(appender);
    config.getRootLogger().addAppender(appender, logLevel, null);
}
 
開發者ID:RUB-NDS,項目名稱:TLS-Attacker,代碼行數:11,代碼來源:ExecutionRecorder.java

示例9: createConsoleLogger

import org.apache.logging.log4j.core.config.Configuration; //導入方法依賴的package包/類
/**
 * Creates console appender with some reasonable default logging settings.
 *
 * @return Logger with auto configured console appender.
 */
public Logger createConsoleLogger() {
    // from http://logging.apache.org/log4j/2.x/manual/customconfig.html
    final LoggerContext ctx = impl.getContext();

    final Configuration cfg = ctx.getConfiguration();

    PatternLayout.Builder builder = PatternLayout.newBuilder()
        .withPattern("%d{ISO8601}][%-5p][%t][%c{1}] %m%n")
        .withCharset(Charset.defaultCharset())
        .withAlwaysWriteExceptions(false)
        .withNoConsoleNoAnsi(false);

    PatternLayout layout = builder.build();

    ConsoleAppender.Builder consoleAppenderBuilder = ConsoleAppender.newBuilder()
        .withName(CONSOLE_APPENDER)
        .withLayout(layout);

    ConsoleAppender consoleApp = consoleAppenderBuilder.build();

    consoleApp.start();

    cfg.addAppender(consoleApp);
    cfg.getRootLogger().addAppender(consoleApp, Level.TRACE, null);

    ctx.updateLoggers(cfg);

    return ctx.getRootLogger();
}
 
開發者ID:apache,項目名稱:ignite,代碼行數:35,代碼來源:Log4J2Logger.java

示例10: install

import org.apache.logging.log4j.core.config.Configuration; //導入方法依賴的package包/類
public void install() {
    if (installed.compareAndSet(false, true)) {
        LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        Configuration config = ctx.getConfiguration();
        config.addAppender(this);

        for (LoggerConfig loggerConfig : config.getLoggers().values()) {
            loggerConfig.addAppender(this, Level.INFO, null);
        }
    }

    start();
}
 
開發者ID:jivesoftware,項目名稱:miru,代碼行數:14,代碼來源:HttpKinesisLogAppender.java

示例11: install

import org.apache.logging.log4j.core.config.Configuration; //導入方法依賴的package包/類
public void install() {
    if (installed.compareAndSet(false, true)) {
        LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        Configuration config = ctx.getConfiguration();
        config.addAppender(this);

        for (LoggerConfig loggerConfig : config.getLoggers().values()) {
            loggerConfig.addAppender(this, Level.INFO, null); // TODO expose to config
        }
    }

    start();
}
 
開發者ID:jivesoftware,項目名稱:miru,代碼行數:14,代碼來源:MiruStumptownInternalLogAppender.java

示例12: addRootApender

import org.apache.logging.log4j.core.config.Configuration; //導入方法依賴的package包/類
/**
 * Adds the specified appender to the root logger.
 * 
 * @param appender
 *            the appender
 * @param level
 *            the logging level to assign to the appender. Default is INFO
 */
public static synchronized void addRootApender(Appender appender, String level) {
	Configuration config = getConfiguration();
	appender.start();
	config.addAppender(appender);
	Level l = Level.toLevel(level, Level.INFO);

	LoggerConfig root = config.getRootLogger();
	if (!root.getLevel().isLessSpecificThan(l)) {
		if (originalRootLevel == null) {
			originalRootLevel = root.getLevel();
			Iterator<AppenderRef> it = root.getAppenderRefs().iterator();
			while (it.hasNext()) {
				AppenderRef ar = it.next();
				if (ar.getLevel() == null) {
					Appender a = root.getAppenders().get(ar.getRef());
					root.removeAppender(a.getName());
					root.addAppender(a, originalRootLevel, ar.getFilter());
				}
			}
		}
		root.setLevel(l);
	}
	root.addAppender(appender, l, null);
	addedAppenders++;
	getContext().updateLoggers();
}
 
開發者ID:sfera-labs,項目名稱:sfera,代碼行數:35,代碼來源:LoggerUtils.java

示例13: addAppender

import org.apache.logging.log4j.core.config.Configuration; //導入方法依賴的package包/類
private void addAppender(final Writer writer, final String writerName) {
    final LoggerContext context = LoggerContext.getContext(false);
    final Configuration config = context.getConfiguration();
    final PatternLayout layout = PatternLayout.createDefaultLayout(config);
    final Appender appender = WriterAppender.createAppender(layout, null, writer, writerName, false, true);
    appender.start();
    config.addAppender(appender);
    ConfigurationTestUtils.updateLoggers(appender, config);
}
 
開發者ID:apache,項目名稱:logging-log4j2,代碼行數:10,代碼來源:WriterAppenderTest.java

示例14: addAppender

import org.apache.logging.log4j.core.config.Configuration; //導入方法依賴的package包/類
/**
 * Tests that you can add an output stream appender dynamically.
 */
private void addAppender(final OutputStream outputStream, final String outputStreamName) {
    final LoggerContext context = LoggerContext.getContext(false);
    final Configuration config = context.getConfiguration();
    final PatternLayout layout = PatternLayout.createDefaultLayout(config);
    final Appender appender = OutputStreamAppender.createAppender(layout, null, outputStream, outputStreamName, false, true);
    appender.start();
    config.addAppender(appender);
    ConfigurationTestUtils.updateLoggers(appender, config);
}
 
開發者ID:apache,項目名稱:logging-log4j2,代碼行數:13,代碼來源:OutputStreamAppenderTest.java

示例15: getLogger

import org.apache.logging.log4j.core.config.Configuration; //導入方法依賴的package包/類
/**
 * Get the logger used for output
 *
 * @param validatedArgs with output file, silent, and verbose options that
 *                      are used to configure the logger
 * @return Logger with dynamically-generated configuration
 */
public static Logger getLogger(ValidatedArgs validatedArgs) {
    // This approach is ugly, but it circumvents the need for multiple log4j
    // configuration files and simplifies writing results to the console and the output file
    // Silence StatusLogger
    System.setProperty("org.apache.logging.log4j.simplelog.StatusLogger.level", "FATAL");
    // Setup context
    LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
    Configuration configuration = loggerContext.getConfiguration();
    // Define layout
    PatternLayout patternLayout = PatternLayout.newBuilder()
            .withConfiguration(configuration)
            // uncomment this pattern for debugging
            //.withPattern("%d{ISO8601} [%level] [%F:%L] %msg%n")
            .build();
    // Add appenders
    AppenderRef[] appenderRefs;
    //// Create console appender unless silent
    ConsoleAppender consoleAppender = null;
    AppenderRef consoleAppenderRef = null;
    if (!validatedArgs.silent) {
        consoleAppender = ConsoleAppender.newBuilder()
                .setConfiguration(configuration)
                .withLayout(patternLayout)
                .withName(CONSOLE_APPENDER)
                .build();
        consoleAppender.start();
        configuration.addAppender(consoleAppender);
        consoleAppenderRef = AppenderRef.createAppenderRef(CONSOLE_APPENDER, null, null);
    }
    //// Create file appender if output file specified
    FileAppender fileAppender = null;
    AppenderRef fileAppenderRef = null;
    if (validatedArgs.outputFile != null) {
        fileAppender = FileAppender.newBuilder()
                .setConfiguration(configuration)
                .withLayout(patternLayout)
                .withName(FILE_APPENDER)
                .withFileName(validatedArgs.outputFile.getAbsolutePath())
                .build();
        fileAppender.start();
        configuration.addAppender(fileAppender);
        fileAppenderRef = AppenderRef.createAppenderRef(FILE_APPENDER, null, null);
    }
    if ((consoleAppenderRef != null) && (fileAppenderRef != null)) {
        appenderRefs = new AppenderRef[]{consoleAppenderRef, fileAppenderRef};
    } else if (consoleAppenderRef != null) {
        appenderRefs = new AppenderRef[]{consoleAppenderRef};
    } else if (fileAppenderRef != null) {
        appenderRefs = new AppenderRef[]{fileAppenderRef};
    } else {
        throw new IllegalStateException("At least one appender must be configured to provide output!");
    }
    // Build and update the LoggerConfig
    Level levelToUse = validatedArgs.verbose ? VERBOSE_LEVEL : DEFAULT_LEVEL;
    LoggerConfig loggerConfig = LoggerConfig.createLogger(false, levelToUse, LOGGER_NAME, "true", appenderRefs, null, configuration, null);
    if (consoleAppender != null) {
        loggerConfig.addAppender(consoleAppender, null, null);
    }
    if (fileAppender != null) {
        loggerConfig.addAppender(fileAppender, null, null);
    }
    configuration.addLogger(LOGGER_NAME, loggerConfig);
    loggerContext.updateLoggers();
    return (Logger) LogManager.getLogger(LOGGER_NAME);
}
 
開發者ID:rmcnew,項目名稱:LiquidFortressPacketAnalyzer,代碼行數:73,代碼來源:LoggerFactory.java


注:本文中的org.apache.logging.log4j.core.config.Configuration.addAppender方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。