Java Code Examples for org.apache.logging.log4j.core.appender.FileAppender#Builder

The following examples show how to use org.apache.logging.log4j.core.appender.FileAppender#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LogConfiguration.java    From wekaDeeplearning4j with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Apply the logging configuration.
 */
public void apply() {
  LoggerContext context = getLoggerContext();
  Configuration config = context.getConfiguration();
  ConfigurationSource configSource = config.getConfigurationSource();
  String packageHomeDir = WekaPackageManager.getPackageHome().getPath();
  if (ConfigurationSource.NULL_SOURCE.equals(configSource)) {
    // Use log4j2.xml shipped with the package ...
    URI uri = Paths.get(packageHomeDir, "wekaDeeplearning4j", "src", "main", "resources",
        "log4j2.xml").toUri();
    context.setConfigLocation(uri);
    log.info("Logging configuration loaded from source: {}", uri.toString());
  }

  String fileAppenderName = "fileAppender";
  if (!context.getRootLogger().getAppenders().containsKey(fileAppenderName)) {
    // Get console appender layout
    Appender consoleAppender = context.getLogger(log.getName()).getAppenders().get("Console");
    Layout<? extends Serializable> layout = consoleAppender.getLayout();

    // Add file appender
    String filePath = resolveLogFilePath();
    FileAppender.Builder appenderBuilder = new FileAppender.Builder();
    appenderBuilder.withFileName(filePath);
    appenderBuilder.withAppend(append);
    appenderBuilder.withName(fileAppenderName);
    appenderBuilder.withLayout(layout);
    FileAppender appender = appenderBuilder.build();
    appender.start();
    context.getRootLogger().addAppender(appender);
  }
}
 
Example 2
Source File: LogConfiguration.java    From wekaDeeplearning4j with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Apply the logging configuration.
 */
public void apply() {
  LoggerContext context = getLoggerContext();
  Configuration config = context.getConfiguration();
  ConfigurationSource configSource = config.getConfigurationSource();
  String packageHomeDir = WekaPackageManager.getPackageHome().getPath();
  if (ConfigurationSource.NULL_SOURCE.equals(configSource)) {
    // Use log4j2.xml shipped with the package ...
    URI uri = Paths.get(packageHomeDir, "wekaDeeplearning4j", "src", "main", "resources",
        "log4j2.xml").toUri();
    context.setConfigLocation(uri);
    log.info("Logging configuration loaded from source: {}", uri.toString());
  }

  String fileAppenderName = "fileAppender";
  if (!context.getRootLogger().getAppenders().containsKey(fileAppenderName)) {
    // Get console appender layout
    Appender consoleAppender = context.getLogger(log.getName()).getAppenders().get("Console");
    Layout<? extends Serializable> layout = consoleAppender.getLayout();

    // Add file appender
    String filePath = resolveLogFilePath();
    FileAppender.Builder appenderBuilder = new FileAppender.Builder();
    appenderBuilder.withFileName(filePath);
    appenderBuilder.withAppend(append);
    appenderBuilder.withName(fileAppenderName);
    appenderBuilder.withLayout(layout);
    FileAppender appender = appenderBuilder.build();
    appender.start();
    context.getRootLogger().addAppender(appender);
  }
}
 
Example 3
Source File: BaseService.java    From support-diagnostics with Apache License 2.0 5 votes vote down vote up
protected void createFileAppender(String logDir, String logFile) {

        logPath = logDir + SystemProperties.fileSeparator + logFile;

        Layout layout = PatternLayout.newBuilder()
                .withConfiguration(logConfig)
                .withPattern("%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n")
                .build();

        FileAppender.Builder builder = FileAppender.newBuilder();
        builder.setConfiguration(logConfig);
        builder.withFileName(logPath);
        builder.withAppend(false);
        builder.withLocking(false);
        builder.setName("packaged");
        builder.setIgnoreExceptions(false);
        builder.withImmediateFlush(true);
        builder.withBufferedIo(false);
        builder.withBufferSize(0);
        builder.setLayout(layout);
        Appender diagAppender = builder.build();

        Appender oldAppender = logConfig.getAppender("packaged");
        if(oldAppender != null && oldAppender.isStarted()){
            oldAppender.stop();
            logConfig.getRootLogger().removeAppender("packaged");
        }

        diagAppender.start();
        logConfig.addAppender(diagAppender);
        AppenderRef.createAppenderRef("packaged", null, null);
        logConfig.getRootLogger().addAppender(diagAppender, null, null);
        loggerContext.updateLoggers();
        logger.info(Constants.CONSOLE, "Diagnostic logger reconfigured for inclusion into archive");

    }