void init() { try { try { (new KonkerContextInitializer(this.defaultLoggerContext)).autoConfig(); } catch (JoranException var2) { Util.report("Failed to auto configure default logger context", var2); } if(!StatusUtil.contextHasStatusListener(this.defaultLoggerContext)) { StatusPrinter.printInCaseOfErrorsOrWarnings(this.defaultLoggerContext); } this.contextSelectorBinder.init(this.defaultLoggerContext, KEY); this.initialized = true; } catch (Throwable var3) { Util.report("Failed to instantiate [" + LoggerContext.class.getName() + "]", var3); } }
public final void doConfigure(final InputSource inputSource) throws JoranException { long threshold = System.currentTimeMillis(); if (!ConfigurationWatchListUtil.wasConfigurationWatchListReset(context)) { informContextOfURLUsedForConfiguration(getContext(), null); } SaxEventRecorder recorder = new SaxEventRecorder(context); recorder.recordEvents(inputSource); doConfigure(recorder.saxEventList); // no exceptions a this level StatusUtil statusUtil = new StatusUtil(context); if (statusUtil.noXMLParsingErrorsOccurred(threshold)) { addInfo("Registering current configuration as safe fallback point"); registerSafeConfiguration(); } }
/** * Package access for testing purposes. */ void init() { try { try { new ContextInitializer(defaultLoggerContext).autoConfig(); } catch (JoranException je) { Util.report("Failed to auto configure default logger context", je); } // logback-292 if(!StatusUtil.contextHasStatusListener(defaultLoggerContext)) { StatusPrinter.printInCaseOfErrorsOrWarnings(defaultLoggerContext); } contextSelectorBinder.init(defaultLoggerContext, KEY); initialized = true; } catch (Throwable t) { // we should never get here Util.report("Failed to instantiate [" + LoggerContext.class.getName() + "]", t); } }
private void performXMLConfiguration(LoggerContext lc) { JoranConfigurator jc = new JoranConfigurator(); jc.setContext(context); StatusUtil statusUtil = new StatusUtil(context); List<SaxEvent> eventList = jc.recallSafeConfiguration(); URL mainURL = ConfigurationWatchListUtil.getMainWatchURL(context); lc.reset(); long threshold = System.currentTimeMillis(); try { jc.doConfigure(mainConfigurationURL); if (statusUtil.hasXMLParsingErrors(threshold)) { fallbackConfiguration(lc, eventList, mainURL); } } catch (JoranException e) { fallbackConfiguration(lc, eventList, mainURL); } }
public static int getHighestLevel(long threshold, StatusManager sm ) { List filteredList = StatusUtil.filterStatusListByTimeThreshold(sm.getCopyOfStatusList(), threshold); int maxLevel = 0; Iterator i$ = filteredList.iterator(); while(i$.hasNext()) { Status s = (Status)i$.next(); if(s.getLevel() > maxLevel) { maxLevel = s.getLevel(); } } return maxLevel; }
public static void createAndRegisterJMXConfigurator( MBeanServer mbs, LoggerContext loggerContext, JMXConfigurator jmxConfigurator, ObjectName objectName, Object caller) { try { mbs.registerMBean(jmxConfigurator, objectName); } catch (Exception e) { StatusUtil statusUtil = new StatusUtil(loggerContext); statusUtil.addError(caller, "Failed to create mbean", e); } }
public LoggerContext getLoggerContext() { String contextName = null; Context ctx = null; // First check if ThreadLocal has been set already LoggerContext lc = threadLocal.get(); if (lc != null) { return lc; } try { // We first try to find the name of our // environment's LoggerContext ctx = JNDIUtil.getInitialContext(); contextName = (String) JNDIUtil.lookup(ctx, JNDI_CONTEXT_NAME); } catch (NamingException ne) { // We can't log here } if (contextName == null) { // We return the default context return defaultContext; } else { // Let's see if we already know such a context LoggerContext loggerContext = synchronizedContextMap.get(contextName); if (loggerContext == null) { // We have to create a new LoggerContext loggerContext = new LoggerContext(); loggerContext.setName(contextName); synchronizedContextMap.put(contextName, loggerContext); URL url = findConfigFileURL(ctx, loggerContext); if (url != null) { configureLoggerContextByURL(loggerContext, url); } else { try { new ContextInitializer(loggerContext).autoConfig(); } catch (JoranException je) { } } // logback-292 if (!StatusUtil.contextHasStatusListener(loggerContext)) StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext); } return loggerContext; } }
@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())); }
@Override public void started() { SLF4JBridgeHandler.removeHandlersForRootLogger(); SLF4JBridgeHandler.install(); this.app.addListeners(this); LoggerContext loggerContext = ContextSelectorStaticBinder.getSingleton().getContextSelector().getLoggerContext(); loggerContext.stop(); loggerContext.reset(); String appName = this.app.getName(), consoleTty = System.getProperty(CrigttProperties.LOGGING_CONSOLE_TTY_NAME); CrigttLoggingInitializer loggingInit = buildComponent(CrigttLoggingInitializer.class, DefaultLoggingInitializer::new, this.app); loggerContext.setName(appName); loggerContext .putProperty(CrigttProperties.LOGGING_CONSOLE_TTY_NAME, ((consoleTty != null) ? consoleTty : Boolean.toString((System.console() != null)))); loggerContext.putProperty(CrigttProperties.LOGGING_FILE_DIR_NAME, loggingInit.buildLogDirectory().getPath()); loggerContext.putProperty(CrigttProperties.LOGGING_FILE_NAME_NAME, loggingInit.buildLogFileName()); String configFileUrlPath = LOGBACK_CONFIG_FILE_URL_PATH_PREFIX + appName + FilenameUtils.EXTENSION_SEPARATOR + CrigttFileExtensions.GROOVY; URL configFileUrl; try { GafferConfigurator configurator = new GafferConfigurator(loggerContext); loggerContext.putObject(ClassicConstants.GAFFER_CONFIGURATOR_FQCN, configurator); configurator.run(IOUtils.toString((configFileUrl = ResourceUtils.getURL(configFileUrlPath)))); } catch (IOException e) { throw new ApplicationContextException(String.format("Unable to process Logback configuration file (path=%s).", configFileUrlPath), e); } StatusManager statusManager = 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 Logback using configuration file (path=%s).", configFileUrlPath)); } loggingInit.postProcessContext(loggerContext); loggerContext.getLogger(LoggingApplicationRunListener.class).info( String.format("Logging initialized (initializerClass=%s, configFileUrl=%s).", loggingInit.getClass().getName(), configFileUrl.toString())); }