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; } }
private static void addError(LoggerContext context, Object origin, String msg, Throwable t) { StatusManager sm = context.getStatusManager(); if (sm == null) { return; } sm.add(new ErrorStatus(msg, origin, t)); }
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"); } }
private void multiplicityWarning(String resourceName, ClassLoader classLoader) { Set<URL> urlSet = null; StatusManager sm = loggerContext.getStatusManager(); try { urlSet = Loader.getResources(resourceName, classLoader); } catch (IOException e) { sm.add(new ErrorStatus("Failed to get url list for resource [" + resourceName + "]", loggerContext, e)); } if (urlSet != null && urlSet.size() > 1) { sm.add(new WarnStatus("Resource [" + resourceName + "] occurs multiple times on the classpath.", loggerContext)); for (URL url : urlSet) { sm.add(new WarnStatus("Resource [" + resourceName + "] occurs at [" + url.toString() + "]", loggerContext)); } } } }
private URL findConfigFileURL(Context ctx, LoggerContext loggerContext) { StatusManager sm = loggerContext.getStatusManager(); String jndiEntryForConfigResource = JNDIUtil.lookup(ctx, JNDI_CONFIGURATION_RESOURCE); // Do we have a dedicated configuration file? if (jndiEntryForConfigResource != null) { sm.add(new InfoStatus("Searching for [" + jndiEntryForConfigResource + "]", this)); URL url = urlByResourceName(sm, jndiEntryForConfigResource); if (url == null) { String msg = "The jndi resource [" + jndiEntryForConfigResource + "] for context [" + loggerContext.getName() + "] does not lead to a valid file"; sm.add(new WarnStatus(msg, this)); } return url; } else { String resourceByConvention = conventionalConfigFileName(loggerContext.getName()); return urlByResourceName(sm, resourceByConvention); } }
private void statusOnResourceSearch(String resourceName, ClassLoader classLoader, URL url) { StatusManager sm = loggerContext.getStatusManager(); if (url == null) { sm.add(new InfoStatus("Could NOT find resource [" + resourceName + "]", loggerContext)); } else { sm.add(new InfoStatus("Found resource [" + resourceName + "] at [" + url.toString() + "]", loggerContext)); multiplicityWarning(resourceName, classLoader); } }
final void noAppenderDefinedWarning(final Logger logger) { if (noAppenderWarning++ == 0) { getStatusManager().add(new WarnStatus("No appenders present in context [" + getName() + "] for logger [" + logger.getName() + "].", logger)); } }
public static void addError(String msg, Object o) { addStatus(new ErrorStatus(msg, o)); }
void addStatusListener(StatusListener statusListener) { StatusManager sm = loggerContext.getStatusManager(); sm.add(statusListener); }
private void resetStatusListeners() { StatusManager sm = getStatusManager(); for (StatusListener sl : sm.getCopyOfStatusListenerList()) { sm.remove(sl); } }
public static void addInfo(String msg, Object o) { addStatus(new InfoStatus(msg, o)); }
public List<String> getStatuses() { List<String> list = new ArrayList<String>(); Iterator<Status> it = context.getStatusManager().getCopyOfStatusList().iterator(); while (it.hasNext()) { list.add(it.next().toString()); } return list; }
protected void configureWith(URL resource) { JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext((LoggerContext) loggerFactory); ((LoggerContext) loggerFactory).reset(); // the statusManager keeps a copy of all logback status messages even after reset, so we clear that ((LoggerContext) loggerFactory).getStatusManager().clear(); try { configurator.doConfigure(resource); } catch (JoranException ignore) { } StatusPrinter.printInCaseOfErrorsOrWarnings((Context) loggerFactory); }
public void reloadByURL(URL url) throws JoranException { StatusListenerAsList statusListenerAsList = new StatusListenerAsList(); addStatusListener(statusListenerAsList); addInfo("Resetting context: " + loggerContext.getName()); loggerContext.reset(); // after a reset the statusListenerAsList gets removed as a listener addStatusListener(statusListenerAsList); try { if (url != null) { JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(loggerContext); configurator.doConfigure(url); addInfo("Context: " + loggerContext.getName() + " reloaded."); } } finally { removeStatusListener(statusListenerAsList); if (debug) { StatusPrinter.print(statusListenerAsList.getStatusList()); } } }
void removeStatusListener(StatusListener statusListener) { StatusManager sm = loggerContext.getStatusManager(); sm.remove(statusListener); }
public void configureByResource(URL url) throws JoranException { if (url == null) { throw new IllegalArgumentException("URL argument cannot be null"); } final String urlString = url.toString(); if (urlString.endsWith("groovy")) { if (EnvUtil.isGroovyAvailable()) { // avoid directly referring to GafferConfigurator so as to avoid // loading groovy.lang.GroovyObject . See also http://jira.qos.ch/browse/LBCLASSIC-214 GafferUtil.runGafferConfiguratorOn(loggerContext, this, url); } else { StatusManager sm = loggerContext.getStatusManager(); sm.add(new ErrorStatus("Groovy classes are not available on the class path. ABORTING INITIALIZATION.", loggerContext)); } } else if (urlString.endsWith("xml")) { JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(loggerContext); configurator.doConfigure(url); } else { throw new LogbackException("Unexpected filename extension of file [" + url.toString() + "]. Should be either .groovy or .xml"); } }
private URL urlByResourceName(StatusManager sm, String resourceName) { sm.add(new InfoStatus("Searching for [" + resourceName + "]", this)); URL url = Loader.getResource(resourceName, Loader.getTCL()); if (url != null) { return url; } return Loader.getResourceBySelfClassLoader(resourceName); }
public static void addError(String msg, Object o, Throwable t) { addStatus(new ErrorStatus(msg, o, t)); }
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); } }
@Override protected void reinitialize(LoggingInitializationContext initializationContext) { getLoggerContext().reset(); getLoggerContext().getStatusManager().clear(); loadConfiguration(initializationContext, getSelfInitializationConfig(), null); }