public static void unregister(LoggerContext loggerContext, MBeanServer mbs, ObjectName objectName, Object caller) { StatusUtil statusUtil = new StatusUtil(loggerContext); if (mbs.isRegistered(objectName)) { try { statusUtil.addInfo(caller, "Unregistering mbean [" + objectName + "]"); mbs.unregisterMBean(objectName); } catch (InstanceNotFoundException e) { // this is theoretically impossible statusUtil.addError(caller, "Failed to unregister mbean" + objectName, e); } catch (MBeanRegistrationException e) { // this is theoretically impossible statusUtil.addError(caller, "Failed to unregister mbean" + objectName, e); } } else { statusUtil.addInfo(caller, "mbean [" + objectName + "] does not seem to be registered"); } }
public boolean isErrorFree(long threshold) { return Status.ERROR > getHighestLevel(threshold); }
private void performXMLConfiguration(LoggerContext lc, URL mainConfigurationURL) { 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 ObjectName string2ObjectName(Context context, Object caller, String objectNameAsStr) { String msg = "Failed to convert [" + objectNameAsStr + "] to ObjectName"; StatusUtil statusUtil = new StatusUtil(context); try { return new ObjectName(objectNameAsStr); } catch (MalformedObjectNameException e) { statusUtil.addError(caller, msg, e); return null; } catch (NullPointerException e) { statusUtil.addError(caller, msg, e); return null; } }
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(); } }
public static void print(StatusManager sm, long threshold) { StringBuilder sb = new StringBuilder(); List<Status> filteredList = filterStatusListByTimeThreshold(sm.getCopyOfStatusList(), threshold); buildStrFromStatusList(sb, filteredList); ps.println(sb.toString()); }
public void addError(Object caller, String msg, Throwable t) { addStatus(new ErrorStatus(msg, caller, t)); }
/** * Print the contents of the context status, but only if they contain * warnings or errors occurring later then the threshold. * * @param context */ public static void printInCaseOfErrorsOrWarnings(Context context, long threshold) { if (context == null) { throw new IllegalArgumentException("Context argument cannot be null"); } StatusManager sm = context.getStatusManager(); if (sm == null) { ps.println("WARN: Context named \"" + context.getName() + "\" has no status manager"); } else { StatusUtil statusUtil = new StatusUtil(context); if (statusUtil.getHighestLevel(threshold) >= ErrorStatus.WARN) { print(sm, threshold); } } }
public boolean containsMatch(int level, String regex) { return containsMatch(0, level, regex); }
public static void unregister(LoggerContext loggerContext, MBeanServer mbs, ObjectName objectName, Object caller) { if (mbs.isRegistered(objectName)) { try { StatusUtil.addInfo(loggerContext, caller, "Unregistering mbean [" + objectName + "]"); mbs.unregisterMBean(objectName); } catch (InstanceNotFoundException e) { // this is theoretically impossible StatusUtil.addError(loggerContext, caller, "Failed to unregister mbean" + objectName, e); } catch (MBeanRegistrationException e) { // this is theoretically impossible StatusUtil.addError(loggerContext, caller, "Failed to unregister mbean" + objectName, e); } } else { StatusUtil.addInfo(loggerContext, caller, "mbean [" + objectName + "] does not seem to be registered"); } }
public boolean noXMLParsingErrorsOccurred(long threshold) { return !hasXMLParsingErrors(threshold); }
/** * 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 (Exception t) { // see LOGBACK-1159 Util.report("Failed to instantiate [" + LoggerContext.class.getName() + "]", t); } }
public static ObjectName string2ObjectName(Context context, Object caller, String objectNameAsStr) { String msg = "Failed to convert [" + objectNameAsStr + "] to ObjectName"; try { return new ObjectName(objectNameAsStr); } catch (MalformedObjectNameException e) { StatusUtil.addError(context, caller, msg, e); return null; } catch (NullPointerException e) { StatusUtil.addError(context, caller, msg, e); return null; } }
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 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(recorder.saxEventList); } }
public boolean containsMatch(long threshold, int level, String regex) { List<Status> filteredList = filterStatusListByTimeThreshold(sm.getCopyOfStatusList(), threshold); Pattern p = Pattern.compile(regex); for (Status status : filteredList) { if (level != status.getLevel()) { continue; } String msg = status.getMessage(); Matcher matcher = p.matcher(msg); if (matcher.lookingAt()) { return true; } } return false; }
public void addInfo(Object caller, String msg) { addStatus(new InfoStatus(msg, caller)); }
/** * Print the contents of the context statuses, but only if they contain * errors. * * @param context */ public static void printIfErrorsOccured(Context context) { if (context == null) { throw new IllegalArgumentException("Context argument cannot be null"); } StatusManager sm = context.getStatusManager(); if (sm == null) { ps.println("WARN: Context named \"" + context.getName() + "\" has no status manager"); } else { StatusUtil statusUtil = new StatusUtil(context); if (statusUtil.getHighestLevel(0) == ErrorStatus.ERROR) { print(sm); } } }
public boolean hasXMLParsingErrors(long threshold) { return containsMatch(threshold, Status.ERROR, CoreConstants.XML_PARSING); }
public static void unregister(LoggerContext loggerContext, MBeanServer mbs, ObjectName objectName, Object caller) { if (mbs.isRegistered(objectName)) { try { StatusUtil.addInfo(loggerContext, caller, "Unregistering mbean [" + objectName + "]"); mbs.unregisterMBean(objectName); } catch (InstanceNotFoundException e) { // this is theoretically impossible StatusUtil.addError(loggerContext, caller, "Failed to unregister mbean" + objectName, e); } catch (MBeanRegistrationException e) { // this is theoretically impossible StatusUtil.addError(loggerContext, caller, "Failed to unregister mbean" + objectName, e); } } else { StatusUtil.addInfo(loggerContext, caller, "mbean [" + objectName + "] does not seem to be registered"); } }