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

The following examples show how to use org.apache.logging.log4j.core.appender.AbstractAppender. 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: LogTestRule.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
public void watch(Class<?> loggerClass, Level level) {
    this.loggerClass = loggerClass;
    Appender appender = new AbstractAppender(APPENDER_NAME, null, PatternLayout.createDefaultLayout()) {
        @Override
        public void append(LogEvent event) {
            logEvents.add(new RecordedLogEvent(event));
        }
    };
    appender.start();
    final LoggerContext ctx = getLoggerContext();
    LoggerConfig loggerConfig = ctx.getConfiguration().getLoggerConfig(loggerClass.getName());
    oldLevel = loggerConfig.getLevel();
    loggerConfig.setLevel(level);
    loggerConfig.addAppender(appender, level, null);
    ctx.updateLoggers();
}
 
Example #2
Source File: ESLoggerConfigFactory.java    From core-ng-project with Apache License 2.0 6 votes vote down vote up
public static void configureLogger() {
    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);

    var loggerConfig = new LoggerConfig("", Level.INFO, false); // only enable info and higher level
    loggerConfig.addAppender(appender, null, null);
    config.addLogger("", loggerConfig);
    context.updateLoggers();
}
 
Example #3
Source File: MaxLengthConverter.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
/**
 * Gets an instance of the class.
 *
 * @param config  The current Configuration.
 * @param options pattern options, an array of two elements: pattern, max length (defaults to 100 on invalid value).
 * @return instance of class.
 */
public static MaxLengthConverter newInstance(final Configuration config, final String[] options) {
    if (options.length != 2) {
        LOGGER.error("Incorrect number of options on maxLength: expected 2 received {}: {}", options.length,
            options);
        return null;
    }
    if (options[0] == null) {
        LOGGER.error("No pattern supplied on maxLength");
        return null;
    }
    if (options[1] == null) {
        LOGGER.error("No length supplied on maxLength");
        return null;
    }
    final PatternParser parser = PatternLayout.createPatternParser(config);
    final List<PatternFormatter> formatters = parser.parse(options[0]);
    return new MaxLengthConverter(formatters, AbstractAppender.parseInt(options[1], 100));
}
 
Example #4
Source File: TestLoggerResource.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected void before() throws Throwable {
	loggingEvents = new ConcurrentLinkedQueue<>();

	Appender testAppender = new AbstractAppender("test-appender", null, null, false) {
		@Override
		public void append(LogEvent event) {
			loggingEvents.add(event.getMessage().getFormattedMessage());
		}
	};
	testAppender.start();

	AppenderRef appenderRef = AppenderRef.createAppenderRef(testAppender.getName(), null, null);
	LoggerConfig logger = LoggerConfig.createLogger(
		false,
		Level.getLevel(level.name()),
		"test",
		null,
		new AppenderRef[]{appenderRef},
		null,
		LOGGER_CONTEXT.getConfiguration(),
		null);
	logger.addAppender(testAppender, null, null);

	LOGGER_CONTEXT.getConfiguration().addLogger(loggerName, logger);
	LOGGER_CONTEXT.updateLoggers();
}
 
Example #5
Source File: SmtpAppender.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Create a SmtpAppender.
 * @deprecated Use {@link #newBuilder()} to create and configure a {@link Builder} instance.
 * @see Builder
 */
public static SmtpAppender createAppender(final Configuration config, final String name, final String to,
                                          final String cc, final String bcc, final String from,
                                          final String replyTo, final String subject, final String smtpProtocol,
                                          final String smtpHost, final String smtpPortStr,
                                          final String smtpUsername, final String smtpPassword,
                                          final String smtpDebug, final String bufferSizeStr,
                                          Layout<? extends Serializable> layout, Filter filter,
                                          final String ignore) {
    if (name == null) {
        LOGGER.error("No name provided for SmtpAppender");
        return null;
    }

    final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
    final int smtpPort = AbstractAppender.parseInt(smtpPortStr, 0);
    final boolean isSmtpDebug = Boolean.parseBoolean(smtpDebug);
    final int bufferSize = bufferSizeStr == null ? DEFAULT_BUFFER_SIZE : Integer.parseInt(bufferSizeStr);

    if (layout == null) {
        layout = HtmlLayout.createDefaultLayout();
    }
    if (filter == null) {
        filter = ThresholdFilter.createFilter(null, null, null);
    }
    final Configuration configuration = config != null ? config : new DefaultConfiguration();

    final SmtpManager manager = SmtpManager.getSmtpManager(configuration, to, cc, bcc, from, replyTo, subject, smtpProtocol,
        smtpHost, smtpPort, smtpUsername, smtpPassword, isSmtpDebug, filter.toString(),  bufferSize, null);
    if (manager == null) {
        return null;
    }

    return new SmtpAppender(name, filter, layout, ignoreExceptions, Property.EMPTY_ARRAY, manager);
}
 
Example #6
Source File: RewriteAppender.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public void start() {
    for (final AppenderRef ref : appenderRefs) {
        final String name = ref.getRef();
        final Appender appender = config.getAppender(name);
        if (appender != null) {
            final Filter filter = appender instanceof AbstractAppender ?
                ((AbstractAppender) appender).getFilter() : null;
            appenders.put(name, new AppenderControl(appender, ref.getLevel(), filter));
        } else {
            LOGGER.error("Appender " + ref + " cannot be located. Reference ignored");
        }
    }
    super.start();
}