private void installJulBridge() { // Workaround for strange ClassCircularityErrors in the JUL bridge when very // strange classloader hierarchies are // set up and logging occurs from inside classloaders themselves (eg: some // strange Tomcat deployments) try { Class.forName("java.util.logging.LogRecord"); } catch (ClassNotFoundException e) { throw new AssertionError(e); } LoggerContext loggerContext = (LoggerContext) getContext(); if (!SLF4JBridgeHandler.isInstalled()) { SLF4JBridgeHandler.removeHandlersForRootLogger(); SLF4JBridgeHandler.install(); } LevelChangePropagator julLevelChanger = new LevelChangePropagator(); julLevelChanger.setContext(loggerContext); julLevelChanger.setResetJUL(true); julLevelChanger.start(); loggerContext.addListener(julLevelChanger); }
private Logger configureLevels() { final Logger root = (Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); root.getLoggerContext().reset(); final LevelChangePropagator propagator = new LevelChangePropagator(); propagator.setContext(root.getLoggerContext()); propagator.setResetJUL(true); root.getLoggerContext().addListener(propagator); root.setLevel(config.getLevel()); // Decode the packages and levels for (Map.Entry<String, Level> entry : config.getLoggers().entrySet()) { ((Logger) LoggerFactory.getLogger(entry.getKey())).setLevel(entry.getValue()); } return root; }
protected void configLogbackContext(LoggerContext context, Logger root, Map<String, LoggerFactory> loggers) { context.reset(); if (debugLogback) { StatusListenerConfigHelper.addOnConsoleListenerInstance(context, new OnConsoleStatusListener()); } LevelChangePropagator propagator = new LevelChangePropagator(); propagator.setContext(context); propagator.setResetJUL(true); context.addListener(propagator); root.setLevel(Level.toLevel(level.name(), Level.INFO)); loggers.forEach((name, lf) -> lf.configLogger(name, context)); if (appenders.isEmpty()) { setAppenders(Collections.singletonList(new ConsoleAppenderFactory())); } appenders.forEach(a -> root.addAppender(a.createAppender(context, getLogFormat()))); }
private Logger configureLevels() { final Logger root = (Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); root.getLoggerContext().reset(); final LevelChangePropagator propagator = new LevelChangePropagator(); propagator.setContext(root.getLoggerContext()); propagator.setResetJUL(true); root.getLoggerContext().addListener(propagator); root.setLevel(config.getServerLogConfiguration().getRootLevel()); for (Map.Entry<String, Level> entry : config.getServerLogConfiguration().getLoggers().entrySet()) { ((Logger) LoggerFactory.getLogger(entry.getKey())).setLevel(entry.getValue()); } return root; }
/** * Configure logging based on a list of builders provided to it. Injects the * configured logging to replace the default UIMA loggers, and also sets up * metrics on the logging. * * @param builders * The builders to use to configure the logging */ public void configure(List<BaleenLoggerBuilder> builders) { // Install JUL to SLF4J handling (JUL is default for UIMA) SLF4JBridgeHandler.removeHandlersForRootLogger(); SLF4JBridgeHandler.install(); // Configure Logback LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); Logger rootLogger = context.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME); // Install the level change propagator to reduce the impact of JUL logging context.addListener(new LevelChangePropagator()); // Remove all the existing appenders rootLogger.detachAndStopAllAppenders(); for (BaleenLoggerBuilder builder : builders) { PatternLayoutEncoder ple = new PatternLayoutEncoder(); ple.setCharset(StandardCharsets.UTF_8); ple.setContext(context); ple.setPattern(builder.getPattern()); ple.start(); Appender<ILoggingEvent> appender = builder.build(context, ple); if (!appender.isStarted()) { appender.start(); } rootLogger.addAppender(appender); } LOGGER.debug("Adding instrumented metrics for logging"); // Add an instrumented appender so we get the information about logging // through metrics InstrumentedAppender instrumentedAppender = new InstrumentedAppender(MetricsFactory.getInstance().getRegistry()); instrumentedAppender.setContext(context); instrumentedAppender.start(); rootLogger.addAppender(instrumentedAppender); }
/** * Configure logging. * @param loggingSettings the logging settings */ public void configureLogging(final LoggingSettings loggingSettings) { final Logger root = (Logger)LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); final LoggerContext context = root.getLoggerContext(); // Reset prior configuration context.reset(); // Propagate level changes to java.util.logging final LevelChangePropagator propagator = new LevelChangePropagator(); propagator.setContext(context); propagator.setResetJUL(true); // Set base threshold for all other loggers. root.setLevel(loggingSettings.getLevel()); // Loop through explicitly configured loggers and set the levels for (final Map.Entry<String, Level> entry : loggingSettings.getLoggers().entrySet()) { ((Logger)LoggerFactory.getLogger(entry.getKey())).setLevel(entry.getValue()); } if (loggingSettings.getLogFile().isEnabled()) { root.addAppender(getFileAppender(loggingSettings.getLogFile(), context)); } if (loggingSettings.getConsole().isEnabled()) { root.addAppender(getConsoleAppender(loggingSettings.getConsole(), context)); } }
/** * Assimilate a small set of logging frameworks. */ public static synchronized void assimilate() { if (assimilated) { return; } assimilated = true; // Assimilate java.util.logging final Logger rootLogger = LogManager.getLogManager().getLogger(""); final Handler[] handlers = rootLogger.getHandlers(); if (handlers != null) { for (Handler handler : handlers) { rootLogger.removeHandler(handler); } } SLF4JBridgeHandler.install(); LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); lc.addListener(new LevelChangePropagator()); try { lc.addListener(new JMXConfigurator(lc, ManagementFactory.getPlatformMBeanServer(), new ObjectName("com.opentable.logging:name=LogbackConfig"))); } catch (MalformedObjectNameException e) { throw new RuntimeException(e); } LoggerFactory.getLogger(AssimilateForeignLogging.class).info("java.util.logging was assimilated."); }
private static void configureLogging(File logFile, Level level) { LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); OutputStreamAppender<ILoggingEvent> appender; if (logFile == null) { appender = configureConsoleLogging(loggerContext); } else { appender = configureFileLogging(logFile, loggerContext); } System.out.println("Logging: The log level is set to " + level); PatternLayoutEncoder patternLayoutEncoder = new PatternLayoutEncoder(); patternLayoutEncoder.setPattern(LogDescription.LOG_PATTERN); patternLayoutEncoder.setContext(loggerContext); patternLayoutEncoder.start(); appender.setEncoder(patternLayoutEncoder); appender.start(); ch.qos.logback.classic.Logger rootLogger = loggerContext.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME); loggerContext.stop(); rootLogger.detachAndStopAllAppenders(); rootLogger.addAppender(appender); rootLogger.setLevel(level); SLF4JBridgeHandler.removeHandlersForRootLogger(); SLF4JBridgeHandler.install(); LevelChangePropagator levelChangePropagator = new LevelChangePropagator(); levelChangePropagator.setResetJUL(true); levelChangePropagator.setContext(loggerContext); levelChangePropagator.start(); loggerContext.addListener(levelChangePropagator); loggerContext.start(); }
private void addLevelChangePropagator(LoggerContext loggerContext) { LevelChangePropagator levelChangePropagator = new LevelChangePropagator(); levelChangePropagator.setResetJUL(true); levelChangePropagator.setContext(loggerContext); loggerContext.addListener(levelChangePropagator); }
@Override @SuppressWarnings({ CompilerWarnings.UNCHECKED }) public void environmentPrepared(ConfigurableEnvironment env) { this.buildInitializer(DefaultLoggingInitializer::new).initialize(env); this.loggerContext = ContextSelectorStaticBinder.getSingleton().getContextSelector().getDefaultLoggerContext(); SLF4JBridgeHandler.removeHandlersForRootLogger(); SLF4JBridgeHandler.install(); this.loggerContext.stop(); this.loggerContext.reset(); LevelChangePropagator lvlChangePropagator = new LevelChangePropagator(); lvlChangePropagator.setContext(this.loggerContext); lvlChangePropagator.setResetJUL(true); this.loggerContext.addListener(lvlChangePropagator); this.loggerContext.putObject(SdcctApplication.BEAN_NAME, this.app); this.buildConversionRule(MessageMarkerConverter.WORD, MessageMarkerConverter.class); this.buildConversionRule(PriorityColorCompositeConverter.WORD, PriorityColorCompositeConverter.class); this.buildConversionRule(RootCauseThrowableProxyConverter.WORD, RootCauseThrowableProxyConverter.class); this.buildConversionRule(ThreadSectionConverter.WORD, ThreadSectionConverter.class); this.buildConversionRule(TxMdcConverter.WORD, TxMdcConverter.class); this.buildAppender(new ConsoleAppender<>(), AppenderType.CONSOLE, this.buildPatternLayoutEncoder(CONSOLE_APPENDER_PATTERN), true); this.buildFileAppender(AppenderType.FILE, this.buildPatternLayoutEncoder(FILE_APPENDER_PATTERN), new File( (this.logFileDir = this.app.getLogFileDirectory()), (this.app.getLogFileName() + FilenameUtils.EXTENSION_SEPARATOR + SdcctFileNameExtensions.LOG))); this.buildCachingAppender(AppenderType.LOGSTASH_FILE); this.buildLogger(org.slf4j.Logger.ROOT_LOGGER_NAME, Level.WARN, true, this.appenders.values()); PropertySourceUtils.getSubProperties(env.getPropertySources(), SdcctPropertyNames.LOGGING_LOGGER_PREFIX).forEach((loggerName, loggerPropValue) -> { String[] loggerPropValueParts = StringUtils.split(((String) loggerPropValue), SdcctStringUtils.COLON, 2); Level loggerLvl = Level.toLevel(loggerPropValueParts[0].toUpperCase(), null); if (loggerLvl == null) { throw new ApplicationContextException( String.format("Unknown application (name=%s) logger (name=%s) level: %s", this.app.getName(), loggerName, loggerPropValueParts[0])); } this.buildLogger(loggerName, loggerLvl, false, ((loggerPropValueParts.length == 2) ? Stream.of(org.springframework.util.StringUtils.commaDelimitedListToStringArray(loggerPropValueParts[1])).map(loggerAppenderName -> { AppenderType loggerAppenderType = SdcctEnumUtils.findById(AppenderType.class, loggerAppenderName); if (!this.appenders.containsKey(loggerAppenderType)) { throw new ApplicationContextException(String.format("Unknown application (name=%s) logger (name=%s) appender type (name=%s).", this.app.getName(), loggerName, loggerAppenderName)); } return this.appenders.get(loggerAppenderType); }).collect(Collectors.toList()) : this.appenders.values())); }); StatusManager statusManager = this.loggerContext.getStatusManager(); StatusUtil statusUtil = new StatusUtil(statusManager); long lastResetTime = statusUtil.timeOfLastReset(); if (statusUtil.getHighestLevel(lastResetTime) >= Status.WARN) { StatusPrinter.print(statusManager, lastResetTime); throw new ApplicationContextException(String.format("Unable to initialize application (name=%s) logging.", this.app.getName())); } this.loggerContext.getLogger(LoggingInitializerRunListener.class).info(String.format("Application (name=%s) logging initialized.", this.app.getName())); }