@Override public Level getAppenderThreshold(String appenderName) throws AppenderThresholdNotSupported { for (Map.Entry<String, Appender> entry : getAllAppenders().entrySet()) { if (entry.getKey().equals(appenderName)) { Appender appender = entry.getValue(); for (Object filter : appender.getCopyOfAttachedFiltersList()) { if (filter instanceof ThresholdFilter) { try { Field field = filter.getClass().getDeclaredField("level"); field.setAccessible(true); return (Level) field.get(filter); } catch (NoSuchFieldException | IllegalAccessException e) { log.error("Error getting appender " + appenderName + " level", e); throw new AppenderThresholdNotSupported(appenderName); } } } } } throw new AppenderThresholdNotSupported(appenderName); }
@Override public void initialise() throws Exception { Logger logger = LoggerFactory.getLogger(ELK_PACKAGE_); if (logger instanceof ch.qos.logback.classic.Logger) { ch.qos.logback.classic.Logger logbackLogger = (ch.qos.logback.classic.Logger) logger; ProtegeMessageAppender appender = ProtegeMessageAppender.getInstance(); LoggerContext context = logbackLogger.getLoggerContext(); appender.setContext(context); logbackLogger.addAppender(appender); ThresholdFilter filter = new ThresholdFilter(); filter.setLevel(Level.WARN.levelStr); filter.start(); appender.addFilter(filter); appender.start(); } }
public AirbrakeLogbackAppenderTest() { captor = ArgumentCaptor.forClass(AirbrakeNotice.class); notifier = Mockito.mock(AirbrakeNotifier.class); Mockito.when(notifier.notify(captor.capture())).thenReturn(0); LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); appender = new AirbrakeLogbackAppender(notifier); appender.setApiKey("whatever"); appender.setEnv("test"); appender.setContext(context); ThresholdFilter filter = new ThresholdFilter(); filter.setLevel("ERROR"); appender.addFilter(filter); appender.start(); logger = (Logger) LoggerFactory.getLogger(getClass()); logger.addAppender(appender); logger.setAdditive(false); }
/** * Setup the logging subsystem. * <p> * Used once configuration has been loaded and validated. * * @param loggingConfiguration valid instance of {@link LoggingConfiguration} */ public static void setupLogging(LoggingConfiguration loggingConfiguration) { // reset to "all logging allowed" Logger root = getResetRootLogger(); // now, set up the entire logging // subsystem with the user-provided // configuration ThresholdFilter filter = new ThresholdFilter(); filter.setLevel(loggingConfiguration.getLevel()); // up to the specified config level filter.start(); if (loggingConfiguration.getConsole().isEnabled()) { setupConsoleLogging(root, filter); } if (loggingConfiguration.getFile().isEnabled()) { setupFileLogging(root, filter, loggingConfiguration.getFile().getLogfile()); } // there are certain subsystems that create // annoying log messages - especially in DEBUG. // silence them. setHardCodedLoggingOverrides(); }
private static void setupFileLogging(Logger root, ThresholdFilter filter, String logfilePath) { MinimalLayout formatter = new MinimalLayout(root.getLoggerContext()); formatter.start(); // setup the file appender FileAppender<ILoggingEvent> appender = new FileAppender<>(); appender.setFile(logfilePath); appender.setContext(root.getLoggerContext()); appender.setLayout(formatter); appender.start(); // wrap it in an async appender final AsyncAppender asyncAppender = new AsyncAppender(); asyncAppender.setContext(root.getLoggerContext()); asyncAppender.addAppender(appender); asyncAppender.addFilter(filter); asyncAppender.start(); // now, add it to the root logger root.addAppender(asyncAppender); }
/** * Add a sentry appender. * @param dsn the sentry dsn to use (as produced by the sentry webinterface). * @param logLevelThreshold the threshold for log events to be sent to sentry. * @return the configured sentry appender. */ public static SentryAppender addSentryAppender(final String dsn, Level logLevelThreshold) { final Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); final LoggerContext context = rootLogger.getLoggerContext(); SentryAppender appender = new SentryAppender(); appender.setDsn(dsn); appender.setContext(context); ThresholdFilter levelFilter = new ThresholdFilter(); levelFilter.setLevel(logLevelThreshold.logbackLevel.toString()); levelFilter.start(); appender.addFilter(levelFilter); appender.start(); rootLogger.addAppender(appender); return appender; }
@Override public void setAppenderThreshold(String appenderName, Level threshold) throws AppenderThresholdNotSupported { for (Map.Entry<String, Appender> entry : getAllAppenders().entrySet()) { if (entry.getKey().equals(appenderName)) { Appender appender = entry.getValue(); for (Object filter : appender.getCopyOfAttachedFiltersList()) { if (filter instanceof ThresholdFilter) { ((ThresholdFilter) filter).setLevel(threshold.levelStr); return; } } } } throw new AppenderThresholdNotSupported(appenderName); }
@Before public void setUp() { ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); setListAppender(new ListAppender<>()); setFilter(new ThresholdFilter()); setLogThreshold(Level.WARN); getFilter().start(); getListAppender().addFilter(getFilter()); getListAppender().start(); root.addAppender(getListAppender()); }
public TmmUILogAppender() { super(); ThresholdFilter filter = new ThresholdFilter(); filter.setLevel("WARN"); filter.start(); addFilter(filter); }
/** * Setup info logging to the console. * <p> * To be used only at startup before the configuration file is parsed. */ public static void setupErrorConsoleLogging() { Logger root = getResetRootLogger(); ThresholdFilter filter = new ThresholdFilter(); filter.setLevel(Level.ERROR.levelStr); // only up to ERROR filter.start(); setupConsoleLogging(root, filter); }
private static void setupConsoleLogging(Logger root, ThresholdFilter filter) { MinimalLayout formatter = new MinimalLayout(root.getLoggerContext()); formatter.start(); ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<>(); appender.addFilter(filter); appender.setContext(root.getLoggerContext()); appender.setLayout(formatter); appender.start(); root.addAppender(appender); }
private static void addThresholdFilter(FilterAttachable<ILoggingEvent> appender, Level threshold) { final ThresholdFilter filter = new ThresholdFilter(); filter.setLevel(threshold.toString()); filter.start(); appender.addFilter(filter); }
private void addThresholdFilter(final FilterAttachable<ILoggingEvent> appender, final Level threshold) { final ThresholdFilter filter = new ThresholdFilter(); filter.setLevel(threshold.toString()); filter.start(); appender.addFilter(filter); }
@Override public Appender<ILoggingEvent> createAppender(LoggerContext context, String defaultLogFormat) { final SentryAppender sentryAppender = new SentryAppender(); final ThresholdFilter thresholdFilter = new ThresholdFilter(); thresholdFilter.setLevel(minLevel); sentryAppender.addFilter(thresholdFilter); sentryAppender.start(); return sentryAppender; }