org.apache.logging.log4j.core.appender.AsyncAppender Java Examples

The following examples show how to use org.apache.logging.log4j.core.appender.AsyncAppender. 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: AsyncAppenderBuilder.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
private <T extends Log4j1Configuration> Appender createAppender(String name, String level,
        String[] appenderRefs, boolean blocking, int bufferSize, boolean includeLocation,
        T configuration) {
    org.apache.logging.log4j.Level logLevel = OptionConverter.convertLevel(level,
            org.apache.logging.log4j.Level.TRACE);
    AppenderRef[] refs = new AppenderRef[appenderRefs.length];
    int index = 0;
    for (String appenderRef : appenderRefs) {
        refs[index++] = AppenderRef.createAppenderRef(appenderRef, logLevel, null);
    }
    return new AppenderWrapper(AsyncAppender.newBuilder()
            .setName(name)
            .setAppenderRefs(refs)
            .setBlocking(blocking)
            .setBufferSize(bufferSize)
            .setIncludeLocation(includeLocation)
            .setConfiguration(configuration)
            .build());
}
 
Example #2
Source File: Log4j2Helper.java    From arthas with Apache License 2.0 5 votes vote down vote up
private static List<Map<String, Object>> doGetLoggerAppenders(LoggerConfig loggerConfig) {
    List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();

    Map<String, Appender> appenders = loggerConfig.getAppenders();

    for (Entry<String, Appender> entry : appenders.entrySet()) {
        Map<String, Object> info = new HashMap<String, Object>();
        Appender appender = entry.getValue();
        info.put(LoggerHelper.name, appender.getName());
        info.put(LoggerHelper.clazz, appender.getClass());

        result.add(info);
        if (appender instanceof FileAppender) {
            info.put(LoggerHelper.file, ((FileAppender) appender).getFileName());
        } else if (appender instanceof ConsoleAppender) {
            info.put(LoggerHelper.target, ((ConsoleAppender) appender).getTarget());
        } else if (appender instanceof AsyncAppender) {

            AsyncAppender asyncAppender = ((AsyncAppender) appender);
            String[] appenderRefStrings = asyncAppender.getAppenderRefStrings();

            info.put(LoggerHelper.blocking, asyncAppender.isBlocking());
            info.put(LoggerHelper.appenderRef, Arrays.asList(appenderRefStrings));
        }
    }
    return result;
}
 
Example #3
Source File: AsyncAppenderAdmin.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new {@code AsyncAppenderAdmin} with the specified contextName
 * and async appender.
 *
 * @param contextName used in the {@code ObjectName} for this mbean
 * @param appender the instrumented object
 */
public AsyncAppenderAdmin(final String contextName, final AsyncAppender appender) {
    // super(executor); // no notifications for now
    this.contextName = Objects.requireNonNull(contextName, "contextName");
    this.asyncAppender = Objects.requireNonNull(appender, "async appender");
    try {
        final String ctxName = Server.escape(this.contextName);
        final String configName = Server.escape(appender.getName());
        final String name = String.format(PATTERN, ctxName, configName);
        objectName = new ObjectName(name);
    } catch (final Exception e) {
        throw new IllegalStateException(e);
    }
}
 
Example #4
Source File: AbstractConfiguration.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private List<Appender> getAsyncAppenders(final Appender[] all) {
    final List<Appender> result = new ArrayList<>();
    for (int i = all.length - 1; i >= 0; --i) {
        if (all[i] instanceof AsyncAppender) {
            result.add(all[i]);
        }
    }
    return result;
}
 
Example #5
Source File: ApsSystemUtils.java    From entando-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void init() throws Exception {
    String active = (String) this.systemParams.get(INIT_PROP_LOG_ACTIVE_FILE_OUTPUT);
    if (StringUtils.isEmpty(active) || !active.equalsIgnoreCase("true")) {
        return;
    }
    String appenderName = "ENTANDO";
    String conversionPattern = (String) this.systemParams.get("log4jConversionPattern");
    if (StringUtils.isBlank(conversionPattern)) {
        conversionPattern = "%d{yyyy-MM-dd HH:mm:ss.SSS} - %-5p -  %c - %m%n";
    }
    String maxFileSize = (String) this.systemParams.get(INIT_PROP_LOG_FILE_SIZE);
    if (StringUtils.isBlank(maxFileSize)) {
        maxFileSize = "1MB"; //default size
    } else {
        long mega = new Long(maxFileSize) / KILOBYTE;
        maxFileSize = mega + "KB";
    }
    String filePattern = (String) this.systemParams.get(INIT_PROP_LOG_FILE_PATTERN);
    String filename = (String) this.systemParams.get(INIT_PROP_LOG_NAME);
    int maxBackupIndex = Integer.parseInt((String) this.systemParams.get(INIT_PROP_LOG_FILES_COUNT));
    String log4jLevelString = (String) this.systemParams.get(INIT_PROP_LOG_LEVEL);
    if (StringUtils.isBlank(log4jLevelString)) {
        log4jLevelString = "INFO"; //default level
    }
    Configurator.setRootLevel(Level.getLevel(log4jLevelString));
    LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
    loggerContext.getRootLogger().setLevel(Level.getLevel(log4jLevelString));
    Configurator.setAllLevels(loggerContext.getRootLogger().getName(), Level.getLevel(log4jLevelString));
    Configuration configuration = loggerContext.getConfiguration();
    RollingFileAppender fileAppender = (RollingFileAppender) configuration.getAppender(appenderName);
    if (null == fileAppender) {
        PathCondition[] pathConditions = new PathCondition[]{IfAccumulatedFileCount.createFileCountCondition(maxBackupIndex)};
        String basePath = filePattern.substring(0, filePattern.lastIndexOf(File.separator));
        DeleteAction deleteAction = DeleteAction.createDeleteAction(basePath, true, 1, false, null, pathConditions, null, configuration);
        SizeBasedTriggeringPolicy policy = SizeBasedTriggeringPolicy.createPolicy(maxFileSize);
        PatternLayout layout = PatternLayout.newBuilder().withPattern(conversionPattern).build();
        DefaultRolloverStrategy strategy = DefaultRolloverStrategy.newBuilder()
                .withConfig(configuration).withMax(String.valueOf(maxBackupIndex))
                .withCustomActions(new Action[]{deleteAction}).build();
        fileAppender = RollingFileAppender.newBuilder()
                .withName(appenderName)
                .setConfiguration(configuration)
                .withLayout(layout)
                .withFileName(filename)
                .withFilePattern(filePattern)
                .withPolicy(policy)
                .withStrategy(strategy)
                .build();
        configuration.addAppender(fileAppender);
        Configurator.setLevel(appenderName, Level.getLevel(log4jLevelString));
        fileAppender.start();
    }
    AsyncAppender async = (AsyncAppender) loggerContext.getRootLogger().getAppenders().get("async");
    if (null == async) {
        AppenderRef ref = AppenderRef.createAppenderRef(appenderName, Level.getLevel(log4jLevelString), null);
        async = AsyncAppender.newBuilder().setName("async")
                .setConfiguration(configuration)
                .setAppenderRefs(new AppenderRef[]{ref}).build();
        configuration.addAppender(async);
        loggerContext.getRootLogger().addAppender(async);
        async.start();
    }
    loggerContext.updateLoggers();
}