public void logError(Exception ex, HttpServletRequest request, LogLevel logLevel) { Map<String, String> map = Maps.newHashMap(); map.put("message", ex.getMessage()); map.put("from", request.getRemoteAddr()); final String queryString = request.getQueryString(); map.put("path", queryString != null ? (request.getRequestURI() + "?" + queryString) : request.getRequestURI()); switch (logLevel) { case ERROR: logger.error(JSON.toJSONString(map), ex); break; case WARN: logger.warn(JSON.toJSONString(map)); break; } }
private void log(org.slf4j.Logger logger, LogLevel level, String message) { Objects.requireNonNull(level, "LogLevel must not be null."); switch (level) { case TRACE: logger.trace(message); break; case DEBUG: logger.debug(message); break; case INFO: logger.info(message); break; case WARN: logger.warn(message); break; case ERROR: case FATAL: logger.error(message); break; default: break; } }
private void log(org.slf4j.Logger logger, LogLevel level, String message, Throwable err) { Objects.requireNonNull(level, "LogLevel must not be null."); switch (level) { case TRACE: logger.trace(message, err); break; case DEBUG: logger.debug(message, err); break; case INFO: logger.info(message, err); break; case WARN: logger.warn(message, err); break; case ERROR: case FATAL: logger.error(message, err); break; default: break; } }
private boolean isEnabled(org.slf4j.Logger logger, LogLevel level) { Objects.requireNonNull(level, "LogLevel must not be null."); switch (level) { case TRACE: return logger.isTraceEnabled(); case DEBUG: return logger.isDebugEnabled(); case INFO: return logger.isInfoEnabled(); case WARN: return logger.isWarnEnabled(); case ERROR: case FATAL: return logger.isErrorEnabled(); default: throw new IllegalArgumentException("LogLevel must be one of the enabled levels."); } }
private static Level convert(final LogLevel logLevel) { switch (logLevel) { case FATAL: return Level.FATAL; case ERROR: return Level.ERROR; case WARN: return Level.WARN; case INFO: return Level.INFO; case DEBUG: return Level.DEBUG; case TRACE: return Level.TRACE; case OFF: return Level.OFF; default: return Level.toLevel(logLevel.name()); } }
@RequestMapping( value = "${edison.application.management.base-path:/internal}/loggers/{name:.*}", consumes = { ActuatorMediaType.V2_JSON, APPLICATION_JSON_VALUE}, produces = { ActuatorMediaType.V2_JSON, APPLICATION_JSON_VALUE}, method = POST) @ResponseBody public Object post(@PathVariable String name, @RequestBody Map<String, String> configuration) { final String level = configuration.get("configuredLevel"); final LogLevel logLevel = level == null ? null : LogLevel.valueOf(level.toUpperCase()); loggersEndpoint.configureLogLevel(name, logLevel); return HttpEntity.EMPTY; }
@PostConstruct protected void construct() { warnPoints = new ConcurrentSkipListSet<>(); warnService = Executors.newSingleThreadScheduledExecutor(); warnService.scheduleAtFixedRate(() -> { for (WarnPoint wp : warnPoints) { long duration = System.nanoTime() - wp.getStart(); if (isOver(duration, wp.getLoggable())) { log(LogLevel.WARN, formatter.warnBefore(wp.getPoint(), wp.getLoggable(), duration), wp.getPoint(), wp.getLoggable()); warnPoints.remove(wp); } } }, 1L, 1L, TimeUnit.SECONDS); }
public Object logMethod(ProceedingJoinPoint joinPoint, Loggable loggable) throws Throwable { long start = System.nanoTime(); WarnPoint warnPoint = null; Object returnVal = null; if (isLevelEnabled(joinPoint, loggable) && loggable.warnOver() >= 0) { warnPoint = new WarnPoint(joinPoint, loggable, start); warnPoints.add(warnPoint); } if (loggable.entered()) { log(loggable.value(), formatter.enter(joinPoint, loggable), joinPoint, loggable); } try { returnVal = joinPoint.proceed(); long nano = System.nanoTime() - start; if (isOver(nano, loggable)) { log(LogLevel.WARN, formatter.warnAfter(joinPoint, loggable, returnVal, nano), joinPoint, loggable); } else { log(loggable.value(), formatter.after(joinPoint, loggable, returnVal, nano), joinPoint, loggable); } return returnVal; } catch (Throwable ex) { if (contains(loggable.ignore(), ex)) { log(LogLevel.ERROR, formatter.error(joinPoint, loggable, returnVal, System.nanoTime() - start, ex), joinPoint, loggable); } else { log(formatter.error(joinPoint, loggable, returnVal, System.nanoTime() - start, ex), joinPoint, loggable, ex); } throw ex; } finally { if (warnPoint != null) { warnPoints.remove(warnPoint); } } }
private void log(LogLevel level, String message, ProceedingJoinPoint joinPoint, Loggable loggable) { if (loggable.name().isEmpty()) { logger.log(level, MethodSignature.class.cast(joinPoint.getSignature()).getMethod().getDeclaringClass(), message); } else { logger.log(level, loggable.name(), message); } }
private void log(String message, ProceedingJoinPoint joinPoint, Loggable loggable, Throwable ex) { if (loggable.name().isEmpty()) { logger.log( MethodSignature.class.cast(joinPoint.getSignature()).getMethod().getDeclaringClass(), message, ex); } else { logger.log(LogLevel.ERROR, loggable.name(), message, ex); } }
@Test public void logTrace() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.TRACE); logger.log(LogLevel.TRACE, "logger name", "trace message"); assertThat(capture.toString(), containsString("TRACE logger name - trace message")); }
@Test public void logTraceError() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.TRACE); logger.log(LogLevel.TRACE, "logger name", "trace message", new Exception("error message")); Pattern pattern = Pattern.compile("TRACE logger name - trace message.*java.lang.Exception: error message", Pattern.DOTALL | Pattern.MULTILINE); assertTrue(pattern.matcher(capture.toString()).find()); }
@Test public void logDebug() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.DEBUG); logger.log(LogLevel.DEBUG, "logger name", "debug message"); assertThat(capture.toString(), containsString("DEBUG logger name - debug message")); }
@Test public void logDebugError() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.DEBUG); logger.log(LogLevel.DEBUG, "logger name", "debug message", new Exception("error message")); Pattern pattern = Pattern.compile("DEBUG logger name - debug message.*java.lang.Exception: error message", Pattern.DOTALL | Pattern.MULTILINE); assertTrue(pattern.matcher(capture.toString()).find()); }
@Test public void logInfo() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.INFO); logger.log(LogLevel.INFO, "logger name", "info message"); assertThat(capture.toString(), containsString("INFO logger name - info message")); }
@Test public void logInfoError() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.INFO); logger.log(LogLevel.INFO, "logger name", "info message", new Exception("error message")); Pattern pattern = Pattern.compile("INFO logger name - info message.*java.lang.Exception: error message", Pattern.DOTALL | Pattern.MULTILINE); assertTrue(pattern.matcher(capture.toString()).find()); }
@Test public void logWarning() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.WARN); logger.log(LogLevel.WARN, "logger name", "warn message"); assertThat(capture.toString(), containsString("WARN logger name - warn message")); }
@Test public void logWarningError() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.WARN); logger.log(LogLevel.WARN, "logger name", "warn message", new Exception("error message")); Pattern pattern = Pattern.compile("WARN logger name - warn message.*java.lang.Exception: error message", Pattern.DOTALL | Pattern.MULTILINE); assertTrue(pattern.matcher(capture.toString()).find()); }
@Test public void logError() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.ERROR); logger.log(LogLevel.ERROR, "logger name", "error message"); assertThat(capture.toString(), containsString("ERROR logger name - error message")); }
@Test public void logErrorError() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.ERROR); logger.log(LogLevel.ERROR, "logger name", "error message", new Exception("error message")); Pattern pattern = Pattern.compile("ERROR logger name - error message.*java.lang.Exception: error message", Pattern.DOTALL | Pattern.MULTILINE); assertTrue(pattern.matcher(capture.toString()).find()); }
@Test public void logFatal() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.FATAL); logger.log(LogLevel.FATAL, "logger name", "fatal message"); assertThat(capture.toString(), containsString("ERROR logger name - fatal message")); }
@Test public void logFatalError() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.FATAL); logger.log(LogLevel.FATAL, "logger name", "fatal message", new Exception("error message")); Pattern pattern = Pattern.compile("ERROR logger name - fatal message.*java.lang.Exception: error message", Pattern.DOTALL | Pattern.MULTILINE); assertTrue(pattern.matcher(capture.toString()).find()); }
@Test public void logTraceErrorOff() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.OFF); logger.log(LogLevel.TRACE, "logger name", "trace message", new Exception("error message")); Pattern pattern = Pattern.compile("TRACE logger name - trace message.*java.lang.Exception: error message", Pattern.DOTALL | Pattern.MULTILINE); assertFalse(pattern.matcher(capture.toString()).find()); }
@Override public void setLogLevel(final String loggerName, final LogLevel logLevel) { if (loggerContext != null) { final Logger logger = loggerContext.getLogger(loggerName); final LoggerConfig config = logger.get(); final Level level = convert(logLevel); if (config.getLevel() != level) { config.setLevel(level); } } }
public static void setEnabled(boolean enabled) { if (enabled) { LoggingSystem system = LoggingSystem .get(Thread.currentThread().getContextClassLoader()); system.setLogLevel("org.springframework", LogLevel.DEBUG); system.setLogLevel("io.spring.concourse.artifactoryresource", LogLevel.DEBUG); } }
public static void write(Class clazz, LogLevel logLevel, String message) { Logger logger = LoggerFactory.getLogger(clazz); switch (logLevel) { case TRACE: logger.trace(message); break; case DEBUG: logger.debug(message); break; case INFO: logger.info(message); break; case WARN: logger.warn(message); break; case ERROR: logger.error(message); break; case FATAL: Marker marker = MarkerFactory.getMarker("FATAL"); logger.error(marker, message); break; default: logger.warn("No suitable log level found"); break; } }
@Override public Mono<Empty> changeLevel(Mono<Logger> request) { return request .doOnSuccess(logger -> this.loggingSystem.setLogLevel( logger.getName(), Level.UNDEFINED == logger.getLevel() ? null : LogLevel.valueOf(logger.getLevel().toString())) ) .map(l -> Empty.newBuilder().build()); }
@Override public void setLogLevel(String loggerName, LogLevel level) { Assert.notNull(level, "Level must not be null"); String name = (StringUtils.hasText(loggerName) ? loggerName : ""); Logger logger = Logger.getLogger(name); logger.setLevel(LEVELS.get(level)); }
@Test public void setLevel() throws Exception { this.loggingSystem.beforeInitialize(); this.loggingSystem.initialize(null, null, null); this.logger.debug("Hello"); this.loggingSystem.setLogLevel("org.springframework.boot", LogLevel.DEBUG); this.logger.debug("Hello"); assertThat(StringUtils.countOccurrencesOf(this.output.toString(), "Hello")) .isEqualTo(1); }
@Test public void setLevel() throws Exception { this.loggingSystem.beforeInitialize(); this.loggingSystem.initialize(this.initializationContext, null, null); this.logger.debug("Hello"); this.loggingSystem.setLogLevel("org.springframework.boot", LogLevel.DEBUG); this.logger.debug("Hello"); assertThat(StringUtils.countOccurrencesOf(this.output.toString(), "Hello")) .isEqualTo(1); }
@Test public void loggingLevelIsPropagatedToJul() { this.loggingSystem.beforeInitialize(); this.loggingSystem.initialize(this.initializationContext, null, null); this.loggingSystem.setLogLevel(getClass().getName(), LogLevel.DEBUG); java.util.logging.Logger julLogger = java.util.logging.Logger .getLogger(getClass().getName()); julLogger.fine("Hello debug world"); String output = this.output.toString().trim(); assertThat(output).contains("Hello debug world"); }
@Test public void setLevelOfUnconfiguredLoggerDoesNotAffectRootConfiguration() throws Exception { this.loggingSystem.beforeInitialize(); this.loggingSystem.initialize(null, null, null); LogManager.getRootLogger().debug("Hello"); this.loggingSystem.setLogLevel("foo.bar.baz", LogLevel.DEBUG); LogManager.getRootLogger().debug("Hello"); assertThat(this.output.toString()).doesNotContain("Hello"); }
@Test public void setLevel() throws Exception { this.loggingSystem.beforeInitialize(); this.loggingSystem.initialize(null, null, null); this.logger.debug("Hello"); this.loggingSystem.setLogLevel("org.springframework.boot", LogLevel.DEBUG); this.logger.debug("Hello"); assertThat(StringUtils.countOccurrencesOf(this.output.toString(), "Hello"), equalTo(1)); }