@Override public void registerListener(ListenerConfig cfg) { if (history != null) throw new IllegalStateException("History already registered"); history = new CircularList<LogEvent>(cfg.size); Level threshold = (cfg.threshold != null) ? Level.toLevel(cfg.threshold) : Level.WARN; ThresholdFilter filter = ThresholdFilter.createFilter(threshold, Filter.Result.ACCEPT, Filter.Result.DENY); appender = new Log4j2Appender(this, filter, threshold); if (!appender.isStarted()) appender.start(); LoggerContext ctx = (LoggerContext) LogManager.getContext(false); LoggerConfig config = getLoggerConfig(ctx, LoggerInfo.ROOT_NAME); config.addAppender(appender, threshold, filter); ctx.updateLoggers(); }
@Test public void testConfiguration() { final Configuration configuration = context.getConfiguration(); assertNotNull(configuration); final FileAppender appender = configuration.getAppender("info"); assertNotNull(appender); final CompositeFilter compFilter = (CompositeFilter) appender.getFilter(); assertNotNull(compFilter); final Filter[] filters = compFilter.getFiltersArray(); assertNotNull(filters); boolean foundLevel = false; for (final Filter filter : filters) { final ThresholdFilter tFilter = (ThresholdFilter) filter; if (infom1Level.equals(tFilter.getLevel())) { foundLevel = true; break; } } Assert.assertTrue("Level not found: " + infom1Level, foundLevel); }
@Test public void testPropertiesConfiguration() { final Configuration config = context.getConfiguration(); assertNotNull("No configuration created", config); assertEquals("Incorrect State: " + config.getState(), config.getState(), LifeCycle.State.STARTED); final Map<String, Appender> appenders = config.getAppenders(); assertNotNull(appenders); assertTrue("Incorrect number of Appenders: " + appenders.size(), appenders.size() == 1); final Map<String, LoggerConfig> loggers = config.getLoggers(); assertNotNull(loggers); assertTrue("Incorrect number of LoggerConfigs: " + loggers.size(), loggers.size() == 2); final Filter filter = config.getFilter(); assertNotNull("No Filter", filter); assertTrue("Not a Threshold Filter", filter instanceof ThresholdFilter); final Logger logger = LogManager.getLogger(getClass()); logger.info("Welcome to Log4j!"); }
@Test public void testPropertiesConfiguration() { final Configuration config = context.getConfiguration(); assertNotNull("No configuration created", config); assertEquals("Incorrect State: " + config.getState(), config.getState(), LifeCycle.State.STARTED); final Map<String, Appender> appenders = config.getAppenders(); assertNotNull(appenders); assertTrue("Incorrect number of Appenders: " + appenders.size(), appenders.size() == 1); final Map<String, LoggerConfig> loggers = config.getLoggers(); assertNotNull(loggers); assertTrue("Incorrect number of LoggerConfigs: " + loggers.size(), loggers.size() == 1); final Filter filter = config.getFilter(); assertNotNull("No Filter", filter); assertTrue("Not a Threshold Filter", filter instanceof ThresholdFilter); final Logger logger = LogManager.getLogger(getClass()); logger.info("Welcome to Log4j!"); }
@Test public void testPropertiesConfiguration() { final Configuration config = context.getConfiguration(); assertNotNull("No configuration created", config); assertEquals("Incorrect State: " + config.getState(), config.getState(), LifeCycle.State.STARTED); final Map<String, Appender> appenders = config.getAppenders(); assertNotNull(appenders); assertTrue("Incorrect number of Appenders: " + appenders.size(), appenders.size() == 1); final Map<String, LoggerConfig> loggers = config.getLoggers(); assertNotNull(loggers); assertTrue("Incorrect number of LoggerConfigs: " + loggers.size(), loggers.size() == 2); final Filter filter = config.getFilter(); assertNotNull("No Filter", filter); assertTrue("Not a Threshold Filter", filter instanceof ThresholdFilter); final Logger logger = LogManager.getLogger(getClass()); assertEquals("Incorrect level " + logger.getLevel(), Level.DEBUG, logger.getLevel()); logger.debug("Welcome to Log4j!"); }
@Test public void testPropertiesConfiguration() { final Configuration config = context.getConfiguration(); assertNotNull("No configuration created", config); assertEquals("Incorrect State: " + config.getState(), config.getState(), LifeCycle.State.STARTED); final Map<String, Appender> appenders = config.getAppenders(); assertNotNull(appenders); assertTrue("Incorrect number of Appenders: " + appenders.size(), appenders.size() == 3); final Map<String, LoggerConfig> loggers = config.getLoggers(); assertNotNull(loggers); assertTrue("Incorrect number of LoggerConfigs: " + loggers.size(), loggers.size() == 2); final Filter filter = config.getFilter(); assertNotNull("No Filter", filter); assertTrue("Not a Threshold Filter", filter instanceof ThresholdFilter); final Logger logger = LogManager.getLogger(getClass()); logger.info("Welcome to Log4j!"); }
public static Builder createTestElasticsearchAppenderBuilder() { Builder builder = ElasticsearchAppender.newBuilder(); builder.withName(TEST_APPENDER_NAME); builder.withFilter(ThresholdFilter.createFilter(Level.INFO, Filter.Result.ACCEPT, Filter.Result.DENY)); builder.withIgnoreExceptions(false); builder.withBatchDelivery(mock(AsyncBatchDelivery.class)); builder.withMessageOnly(false); IndexNameFormatter indexNameFormatter = mock(IndexNameFormatter.class); when(indexNameFormatter.format(any(LogEvent.class))).thenReturn("testIndexName"); builder.withIndexNameFormatter(indexNameFormatter); return builder; }
private void validate(final Configuration config) { assertNotNull(config); assertNotNull(config.getName()); assertFalse(config.getName().isEmpty()); assertNotNull("No configuration created", config); assertEquals("Incorrect State: " + config.getState(), config.getState(), LifeCycle.State.STARTED); final Map<String, Appender> appenders = config.getAppenders(); assertNotNull(appenders); assertTrue("Incorrect number of Appenders: " + appenders.size(), appenders.size() == 2); final KafkaAppender kafkaAppender = (KafkaAppender) appenders.get("Kafka"); final GelfLayout gelfLayout = (GelfLayout) kafkaAppender.getLayout(); final Map<String, LoggerConfig> loggers = config.getLoggers(); assertNotNull(loggers); assertTrue("Incorrect number of LoggerConfigs: " + loggers.size(), loggers.size() == 2); final LoggerConfig rootLoggerConfig = loggers.get(""); assertEquals(Level.ERROR, rootLoggerConfig.getLevel()); assertFalse(rootLoggerConfig.isIncludeLocation()); final LoggerConfig loggerConfig = loggers.get("org.apache.logging.log4j"); assertEquals(Level.DEBUG, loggerConfig.getLevel()); assertTrue(loggerConfig.isIncludeLocation()); final Filter filter = config.getFilter(); assertNotNull("No Filter", filter); assertTrue("Not a Threshold Filter", filter instanceof ThresholdFilter); final List<CustomLevelConfig> customLevels = config.getCustomLevels(); assertNotNull("No CustomLevels", filter); assertEquals(1, customLevels.size()); final CustomLevelConfig customLevel = customLevels.get(0); assertEquals("Panic", customLevel.getLevelName()); assertEquals(17, customLevel.getIntLevel()); final Logger logger = LogManager.getLogger(getClass()); logger.info("Welcome to Log4j!"); }
private void validate(final Configuration config) { assertNotNull(config); assertNotNull(config.getName()); assertFalse(config.getName().isEmpty()); assertNotNull("No configuration created", config); assertEquals("Incorrect State: " + config.getState(), config.getState(), LifeCycle.State.STARTED); final Map<String, Appender> appenders = config.getAppenders(); assertNotNull(appenders); assertTrue("Incorrect number of Appenders: " + appenders.size(), appenders.size() == 1); final ConsoleAppender consoleAppender = (ConsoleAppender)appenders.get("Stdout"); final PatternLayout gelfLayout = (PatternLayout)consoleAppender.getLayout(); final Map<String, LoggerConfig> loggers = config.getLoggers(); assertNotNull(loggers); assertTrue("Incorrect number of LoggerConfigs: " + loggers.size(), loggers.size() == 2); final LoggerConfig rootLoggerConfig = loggers.get(""); assertEquals(Level.ERROR, rootLoggerConfig.getLevel()); assertFalse(rootLoggerConfig.isIncludeLocation()); final LoggerConfig loggerConfig = loggers.get("org.apache.logging.log4j"); assertEquals(Level.DEBUG, loggerConfig.getLevel()); assertTrue(loggerConfig.isIncludeLocation()); final Filter filter = config.getFilter(); assertNotNull("No Filter", filter); assertTrue("Not a Threshold Filter", filter instanceof ThresholdFilter); final List<CustomLevelConfig> customLevels = config.getCustomLevels(); assertNotNull("No CustomLevels", filter); assertEquals(1, customLevels.size()); final CustomLevelConfig customLevel = customLevels.get(0); assertEquals("Panic", customLevel.getLevelName()); assertEquals(17, customLevel.getIntLevel()); final Logger logger = LogManager.getLogger(getClass()); logger.info("Welcome to Log4j!"); }
@SuppressWarnings({ "rawtypes", "unchecked" }) public static synchronized void init() { final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); final Configuration config = ctx.getConfiguration(); Layout layout = PatternLayout.newBuilder().withPattern("%d{yyyy-MM-dd HH:mm:ss}:%p %t %c - %m%n") .withConfiguration(config).withRegexReplacement(null).withCharset(null).withAlwaysWriteExceptions(true) .withNoConsoleNoAnsi(false).withHeader(null).withFooter(null).build(); // file info Filter fileInfoFilter = ThresholdFilter.createFilter(Level.ERROR, Result.DENY, Result.ACCEPT); Appender fileInfoAppender = RollingFileAppender.createAppender(LOG_ROOT + "/zebra.log", LOG_ROOT + "/zebra.log.%d{yyyy-MM-dd}.gz", "true", "FileInfo", "true", "4000", "true", TimeBasedTriggeringPolicy.createPolicy("1", "true"), ZebraRolloverStrategy.createStrategy("30", "1", null, Deflater.DEFAULT_COMPRESSION + "", config), layout, fileInfoFilter, "false", null, null, config); config.addAppender(fileInfoAppender); fileInfoAppender.start(); AppenderRef fileInfoRef = AppenderRef.createAppenderRef("FileInfo", null, fileInfoFilter); // console error Appender consoleErrorAppender = ConsoleAppender.createAppender(layout, null, "SYSTEM_ERR", "ConsoleError", "false", "false"); config.addAppender(consoleErrorAppender); consoleErrorAppender.start(); // console info Filter consoleWarnFilter = ThresholdFilter.createFilter(Level.ERROR, Result.DENY, Result.NEUTRAL); Appender consoleWarnAppender = ConsoleAppender.createAppender(layout, consoleWarnFilter, "SYSTEM_OUT", "ConsoleWarn", "false", "false"); config.addAppender(consoleWarnAppender); consoleWarnAppender.start(); AppenderRef consoleWarnAppenderRef = AppenderRef.createAppenderRef("ConsoleWarn", Level.WARN, consoleWarnFilter); AppenderRef consoleErrorAppenderRef = AppenderRef.createAppenderRef("ConsoleError", Level.WARN, null); AppenderRef[] refs = new AppenderRef[] { consoleErrorAppenderRef, consoleWarnAppenderRef, fileInfoRef }; LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.INFO, "com.dianping.zebra", "true", refs, null, config, null); loggerConfig.addAppender(consoleErrorAppender, Level.ERROR, null); loggerConfig.addAppender(consoleWarnAppender, Level.INFO, null); loggerConfig.addAppender(fileInfoAppender, Level.INFO, null); config.addLogger("com.dianping.zebra", loggerConfig); ctx.updateLoggers(); context = ctx; }
public DirectConsumingAppender(String name, Consumer<byte[]> logEventConsumer, Level threshold) { super(name, ThresholdFilter.createFilter(threshold, Filter.Result.ACCEPT, Filter.Result.DENY), LAYOUT, false); this.logEventConsumer = requireNonNull(logEventConsumer); }
@Test public void appenderUsesThresholdFilter() throws Exception { assertThat(appender.getFilter()).isInstanceOf(ThresholdFilter.class); }
Log4j2Appender(Log4j2Watcher watcher, ThresholdFilter filter, Level threshold) { super("Log4j2WatcherAppender", filter, null); this.watcher = watcher; this.filter = filter; this.threshold = threshold; }
public void setThreshold(Level threshold) { this.threshold = threshold; removeFilter(filter); filter = ThresholdFilter.createFilter(threshold, Filter.Result.ACCEPT, Filter.Result.DENY); addFilter(filter); }
@PluginFactory public static <S extends Serializable> GelfConsoleAppender<S> createAppender( @PluginAttribute("name") final String name, @PluginAttribute("facility") final String facility, @PluginAttribute("target") final String t, @PluginAttribute("extractStacktrace") final String extractStacktrace, @PluginAttribute("originHost") final String originHost, @PluginAttribute("addExtendedInformation") final String addExtendedInformation, @PluginAttribute("includeLocation") final String includeLocation, @PluginAttribute("additionalFields") final String additionalFields, @PluginElement("layout") Layout<S> layout, @PluginElement("filter") Filter filter, @PluginAttribute("suppressExceptions") final String suppressExceptions) throws IOException { if (name == null) { LOGGER.error("No name provided for GelfConsoleAppender"); return null; } GelfSender gelfSender = null; final boolean isHandleExceptions = suppressExceptions == null ? true : Boolean.valueOf(suppressExceptions); if (layout == null) { @SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"}) Layout<S> l = (Layout<S>) HTMLLayout.createLayout(null, null, null, null, null, null); layout = l; } if (filter == null) { filter = ThresholdFilter.createFilter("INFO", null, null); } // If target is set properly, use it. Otherwise use SYSTEM_OUT final Target target = t == null ? Target.SYSTEM_OUT : Target.valueOf(t); gelfSender = getGelfConsoleSender(target); if (gelfSender == null) { return null; } GelfConsoleAppender gelfConsoleAppender = new GelfConsoleAppender<S>(name, filter, layout, gelfSender, isHandleExceptions); gelfConsoleAppender.setFacility(facility); gelfConsoleAppender.setExtractStacktrace(Boolean.parseBoolean(extractStacktrace)); gelfConsoleAppender.setOriginHost(originHost); gelfConsoleAppender.setAddExtendedInformation(Boolean.parseBoolean(addExtendedInformation)); gelfConsoleAppender.setIncludeLocation(Boolean.parseBoolean(includeLocation)); gelfConsoleAppender.setAdditionalFields(additionalFields); return gelfConsoleAppender; }
public Log4JConsoleStream(UniversalMinecraftAPIInterface uma) { uma.getStreamManager().registerStream("console"); Logger rootLogger = (Logger) LogManager.getRootLogger(); Layout<? extends Serializable> layout = Optional.ofNullable(rootLogger.getAppenders().get("Console")).map(Appender::getLayout).orElse(null); if (layout == null) { layout = PatternLayout.createLayout("[%d{HH:mm:ss} %level] [%logger{1}]: %msg%n", null, null, null, null); } Filter infoFilter = ThresholdFilter.createFilter("INFO", "ACCEPT", "DENY"); Filter warnFilter = ThresholdFilter.createFilter("WARN", "ACCEPT", "DENY"); rootLogger.addAppender(new Log4JConsoleStreamAppender(uma, ThresholdFilter.createFilter("INFO", "ACCEPT", "DENY"), layout)); Logger minecraftLogger = (Logger) LogManager.getLogger("net.minecraft"); Layout<? extends Serializable> minecraftLayout; minecraftLayout = Optional.ofNullable(minecraftLogger.getAppenders().get("Console")).map(Appender::getLayout).orElse(null); if (minecraftLayout == null) { minecraftLayout = PatternLayout.createLayout("[%d{HH:mm:ss} %level]: %msg%n", null, null, null, null); } Appender minecraftAppender = new Log4JConsoleStreamAppender(uma, infoFilter, minecraftLayout); minecraftLogger.addAppender(minecraftAppender); ((Logger) LogManager.getLogger("com.mojang")).addAppender(minecraftAppender); Appender warnAppender = new Log4JConsoleStreamAppender(uma, warnFilter, layout); ((Logger) LogManager.getLogger("LaunchWrapper")).addAppender(warnAppender); ((Logger) LogManager.getLogger("com.zaxxer.hikari")).addAppender(warnAppender); }