private void setMetricsMarkerLogbackFilter(LoggerContext context) { log.info("Filtering metrics logs from all appenders except the {} appender", LOGSTASH_APPENDER_NAME); OnMarkerEvaluator onMarkerMetricsEvaluator = new OnMarkerEvaluator(); onMarkerMetricsEvaluator.setContext(context); onMarkerMetricsEvaluator.addMarker("metrics"); onMarkerMetricsEvaluator.start(); EvaluatorFilter<ILoggingEvent> metricsFilter = new EvaluatorFilter<>(); metricsFilter.setContext(context); metricsFilter.setEvaluator(onMarkerMetricsEvaluator); metricsFilter.setOnMatch(FilterReply.DENY); metricsFilter.start(); for (ch.qos.logback.classic.Logger logger : context.getLoggerList()) { for (Iterator<Appender<ILoggingEvent>> it = logger.iteratorForAppenders(); it.hasNext();) { Appender<ILoggingEvent> appender = it.next(); if (!appender.getName().equals(ASYNC_LOGSTASH_APPENDER_NAME)) { log.debug("Filter metrics logs from the {} appender", appender.getName()); appender.setContext(context); appender.addFilter(metricsFilter); appender.start(); } } } }
private void setMetricsMarkerLogbackFilter(LoggerContext context) { log.info("Filtering metrics logs from all appenders except the {} appender", LOGSTASH_APPENDER_NAME); OnMarkerEvaluator onMarkerMetricsEvaluator = new OnMarkerEvaluator(); onMarkerMetricsEvaluator.setContext(context); onMarkerMetricsEvaluator.addMarker("metrics"); onMarkerMetricsEvaluator.start(); EvaluatorFilter<ILoggingEvent> metricsFilter = new EvaluatorFilter<>(); metricsFilter.setContext(context); metricsFilter.setEvaluator(onMarkerMetricsEvaluator); metricsFilter.setOnMatch(FilterReply.DENY); metricsFilter.start(); for (ch.qos.logback.classic.Logger logger : context.getLoggerList()) { for (Iterator<Appender<ILoggingEvent>> it = logger.iteratorForAppenders(); it.hasNext(); ) { Appender<ILoggingEvent> appender = it.next(); if (!appender.getName().equals(ASYNC_LOGSTASH_APPENDER_NAME)) { log.debug("Filter metrics logs from the {} appender", appender.getName()); appender.setContext(context); appender.addFilter(metricsFilter); appender.start(); } } } }
@SuppressWarnings("unchecked") private static void auditLoggerInitializer(String filePath) { // This an instance of the LoggerFactory bound internally at runtime time. LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); // Check to see if using default configuration, if so don't output to console. if (loggerContext.getLoggerList().size() == 1) { Logger root = loggerContext.getLogger("ROOT"); root.detachAppender("console"); } // Just a simple pattern for outputting the messages, includes the ISO8601 formatted timestamp and marker. PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(loggerContext); encoder.setPattern("[%d{ISO8601}] %msg%n"); encoder.start(); // This is the appender, the object that actually outputs to the file. It gets attached to the Logger. RollingFileAppender appender = new RollingFileAppender(); appender.setName(ROTATE_APPENDER_NAME); appender.setContext(loggerContext); appender.setEncoder(encoder); configureAppender(filePath, appender); // Use the external rotate policy ExternalRotateRollingPolicy policy = new ExternalRotateRollingPolicy(appender); policy.setContext(loggerContext); policy.start(); appender.setRollingPolicy(policy); // This filter checks each log for the AUDIT marker. It will only accept those logs with the AUDIT marker. EvaluatorFilter evalFilter= new EvaluatorFilter(); OnMarkerEvaluator markerEval= new OnMarkerEvaluator(); markerEval.addMarker("AUDIT"); markerEval.start(); evalFilter.setEvaluator(markerEval); evalFilter.setOnMatch(FilterReply.ACCEPT); evalFilter.setOnMismatch(FilterReply.DENY); evalFilter.start(); appender.addFilter(evalFilter); // Get a new logger from the loggerContext then make sure there are no other appenders and attach rfAppender. logbackLogger.detachAndStopAllAppenders(); appender.start(); logbackLogger.addAppender(appender); logbackLogger.setLevel(Level.INFO); logbackLogger.setAdditive(false); hasInitialized = true; }