@Test void formatAndLogWithContextAndDuration() { Map<String, Object> ctxData = new HashMap<>(); ctxData.put("foo", true); Event event = new MyEvent(Event.Severity.INFO, Event.Category.IO, Duration.ofMillis(123), new MyContext(ctxData)); loggingEventConsumer.accept(event); verify(logger, times(1)).info("[com.couchbase.io][MyEvent][123ms] {\"foo\":true}"); }
@Override public void accept(final Event event) { if (event.severity() == Event.Severity.TRACING) { return; } StringBuilder logLineBuilder = new StringBuilder(); logLineBuilder.append("[").append(event.category()).append("]"); logLineBuilder.append("[").append(event.getClass().getSimpleName()).append("]"); if (!event.duration().isZero()) { logLineBuilder .append("[") .append(convertEventDuration(event.duration())) .append("]"); } String description = event.description(); if (description != null && !description.isEmpty()) { logLineBuilder.append(" ").append(description); } if (event.context() != null) { logLineBuilder.append(" ").append(event.context().exportAsString(Context.ExportFormat.JSON)); } String logLine = logLineBuilder.toString(); Logger logger = loggers.get(event.category()); if (logger == null) { logger = createLogger(event.category()); loggers.put(event.category(), logger); } boolean diagnosticContext = loggerConfig.diagnosticContextEnabled() && event.context() instanceof RequestContext; if (diagnosticContext) { logger.attachContext(((RequestContext) event.context()).clientContext()); } switch (event.severity()) { case VERBOSE: if (event.cause() != null) { logger.trace(logLine, event.cause()); } else { logger.trace(logLine); } break; case DEBUG: if (event.cause() != null) { logger.debug(logLine, event.cause()); } else { logger.debug(logLine); } break; case INFO: if (event.cause() != null) { logger.info(logLine, event.cause()); } else { logger.info(logLine); } break; case WARN: if (event.cause() != null) { logger.warn(logLine, event.cause()); } else { logger.warn(logLine); } break; case ERROR: if (event.cause() != null) { logger.error(logLine, event.cause()); } else { logger.error(logLine); } default: } if (diagnosticContext) { logger.clearContext(); } }
LoggingEventConsumer implements Consumer<Event> { @Override public void accept(final Event event) { if (event.severity() == Event.Severity.TRACING) { return; } StringBuilder logLineBuilder = new StringBuilder(); logLineBuilder.append("[").append(event.category()).append("]"); logLineBuilder.append("[").append(event.getClass().getSimpleName()).append("]"); if (!event.duration().isZero()) { logLineBuilder .append("[") .append(convertEventDuration(event.duration())) .append("]"); } String description = event.description(); if (description != null && !description.isEmpty()) { logLineBuilder.append(" ").append(description); } if (event.context() != null) { logLineBuilder.append(" ").append(event.context().exportAsString(Context.ExportFormat.JSON)); } String logLine = logLineBuilder.toString(); Logger logger = loggers.get(event.category()); if (logger == null) { logger = createLogger(event.category()); loggers.put(event.category(), logger); } boolean diagnosticContext = loggerConfig.diagnosticContextEnabled() && event.context() instanceof RequestContext; if (diagnosticContext) { logger.attachContext(((RequestContext) event.context()).clientContext()); } switch (event.severity()) { case VERBOSE: if (event.cause() != null) { logger.trace(logLine, event.cause()); } else { logger.trace(logLine); } break; case DEBUG: if (event.cause() != null) { logger.debug(logLine, event.cause()); } else { logger.debug(logLine); } break; case INFO: if (event.cause() != null) { logger.info(logLine, event.cause()); } else { logger.info(logLine); } break; case WARN: if (event.cause() != null) { logger.warn(logLine, event.cause()); } else { logger.warn(logLine); } break; case ERROR: if (event.cause() != null) { logger.error(logLine, event.cause()); } else { logger.error(logLine); } default: } if (diagnosticContext) { logger.clearContext(); } } }
LoggingEventConsumer implements Consumer<Event> { @Override public void accept(final Event event) { if (event.severity() == Event.Severity.TRACING) { return; } StringBuilder logLineBuilder = new StringBuilder(); logLineBuilder.append("[").append(event.category()).append("]"); logLineBuilder.append("[").append(event.getClass().getSimpleName()).append("]"); if (!event.duration().isZero()) { logLineBuilder .append("[") .append(convertEventDuration(event.duration())) .append("]"); } String description = event.description(); if (description != null && !description.isEmpty()) { logLineBuilder.append(" ").append(description); } if (event.context() != null) { logLineBuilder.append(" ").append(event.context().exportAsString(Context.ExportFormat.JSON)); } String logLine = logLineBuilder.toString(); Logger logger = loggers.get(event.category()); if (logger == null) { logger = createLogger(event.category()); loggers.put(event.category(), logger); } boolean diagnosticContext = loggerConfig.diagnosticContextEnabled() && event.context() instanceof RequestContext; if (diagnosticContext) { logger.attachContext(((RequestContext) event.context()).clientContext()); } switch (event.severity()) { case VERBOSE: if (event.cause() != null) { logger.trace(logLine, event.cause()); } else { logger.trace(logLine); } break; case DEBUG: if (event.cause() != null) { logger.debug(logLine, event.cause()); } else { logger.debug(logLine); } break; case INFO: if (event.cause() != null) { logger.info(logLine, event.cause()); } else { logger.info(logLine); } break; case WARN: if (event.cause() != null) { logger.warn(logLine, event.cause()); } else { logger.warn(logLine); } break; case ERROR: if (event.cause() != null) { logger.error(logLine, event.cause()); } else { logger.error(logLine); } default: } if (diagnosticContext) { logger.clearContext(); } } private  LoggingEventConsumer(final LoggerConfig loggerConfig); }
LoggingEventConsumer implements Consumer<Event> { @Override public void accept(final Event event) { if (event.severity() == Event.Severity.TRACING) { return; } StringBuilder logLineBuilder = new StringBuilder(); logLineBuilder.append("[").append(event.category()).append("]"); logLineBuilder.append("[").append(event.getClass().getSimpleName()).append("]"); if (!event.duration().isZero()) { logLineBuilder .append("[") .append(convertEventDuration(event.duration())) .append("]"); } String description = event.description(); if (description != null && !description.isEmpty()) { logLineBuilder.append(" ").append(description); } if (event.context() != null) { logLineBuilder.append(" ").append(event.context().exportAsString(Context.ExportFormat.JSON)); } String logLine = logLineBuilder.toString(); Logger logger = loggers.get(event.category()); if (logger == null) { logger = createLogger(event.category()); loggers.put(event.category(), logger); } boolean diagnosticContext = loggerConfig.diagnosticContextEnabled() && event.context() instanceof RequestContext; if (diagnosticContext) { logger.attachContext(((RequestContext) event.context()).clientContext()); } switch (event.severity()) { case VERBOSE: if (event.cause() != null) { logger.trace(logLine, event.cause()); } else { logger.trace(logLine); } break; case DEBUG: if (event.cause() != null) { logger.debug(logLine, event.cause()); } else { logger.debug(logLine); } break; case INFO: if (event.cause() != null) { logger.info(logLine, event.cause()); } else { logger.info(logLine); } break; case WARN: if (event.cause() != null) { logger.warn(logLine, event.cause()); } else { logger.warn(logLine); } break; case ERROR: if (event.cause() != null) { logger.error(logLine, event.cause()); } else { logger.error(logLine); } default: } if (diagnosticContext) { logger.clearContext(); } } private  LoggingEventConsumer(final LoggerConfig loggerConfig); static LoggingEventConsumer create(); static LoggingEventConsumer create(final LoggerConfig loggerConfig); @Override void accept(final Event event); }
LoggingEventConsumer implements Consumer<Event> { @Override public void accept(final Event event) { if (event.severity() == Event.Severity.TRACING) { return; } StringBuilder logLineBuilder = new StringBuilder(); logLineBuilder.append("[").append(event.category()).append("]"); logLineBuilder.append("[").append(event.getClass().getSimpleName()).append("]"); if (!event.duration().isZero()) { logLineBuilder .append("[") .append(convertEventDuration(event.duration())) .append("]"); } String description = event.description(); if (description != null && !description.isEmpty()) { logLineBuilder.append(" ").append(description); } if (event.context() != null) { logLineBuilder.append(" ").append(event.context().exportAsString(Context.ExportFormat.JSON)); } String logLine = logLineBuilder.toString(); Logger logger = loggers.get(event.category()); if (logger == null) { logger = createLogger(event.category()); loggers.put(event.category(), logger); } boolean diagnosticContext = loggerConfig.diagnosticContextEnabled() && event.context() instanceof RequestContext; if (diagnosticContext) { logger.attachContext(((RequestContext) event.context()).clientContext()); } switch (event.severity()) { case VERBOSE: if (event.cause() != null) { logger.trace(logLine, event.cause()); } else { logger.trace(logLine); } break; case DEBUG: if (event.cause() != null) { logger.debug(logLine, event.cause()); } else { logger.debug(logLine); } break; case INFO: if (event.cause() != null) { logger.info(logLine, event.cause()); } else { logger.info(logLine); } break; case WARN: if (event.cause() != null) { logger.warn(logLine, event.cause()); } else { logger.warn(logLine); } break; case ERROR: if (event.cause() != null) { logger.error(logLine, event.cause()); } else { logger.error(logLine); } default: } if (diagnosticContext) { logger.clearContext(); } } private  LoggingEventConsumer(final LoggerConfig loggerConfig); static LoggingEventConsumer create(); static LoggingEventConsumer create(final LoggerConfig loggerConfig); @Override void accept(final Event event);  }