private void writeLogsOnFileAndConsole() { loggers.debug("****Configuring Logger****"); try { if(Platform.isRunning()){ System.setProperty(HYDROGRAPH_INSTALLATION_LOCATION, Platform.getInstallLocation().getURL().getPath()); ClassLoader loader = new URLClassLoader(new URL[] {new File(Platform.getInstallLocation().getURL().getPath() + LOG_DIR).toURI().toURL()}); LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); URL url = Loader.getResource(CLASSIC_FILE, loader); if (url != null) { JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(lc); lc.reset(); configurator.doConfigure(url); lc.start(); } loggers.debug("****Logger Configured Successfully****"); } } catch(MalformedURLException|JoranException exception){ loggers.error("Failed to configure the logger {}", exception); } }
private void multiplicityWarning(String resourceName, ClassLoader classLoader) { Set urlSet = null; StatusManager sm = this.loggerContext.getStatusManager(); try { urlSet = Loader.getResourceOccurrenceCount(resourceName, classLoader); } catch (IOException var7) { sm.add(new ErrorStatus("Failed to get url list for resource [" + resourceName + "]", this.loggerContext, var7)); } if (urlSet != null && urlSet.size() > 1) { sm.add(new WarnStatus("Resource [" + resourceName + "] occurs multiple times on the classpath.", this.loggerContext)); Iterator i$ = urlSet.iterator(); while (i$.hasNext()) { URL url = (URL) i$.next(); sm.add(new WarnStatus("Resource [" + resourceName + "] occurs at [" + url.toString() + "]", this.loggerContext)); } } }
public URL findURLOfDefaultConfigurationFile(boolean updateStatus) { ClassLoader myClassLoader = Loader.getClassLoaderOfObject(this); URL url = findConfigFileURLFromSystemProperties(myClassLoader, updateStatus); if (url != null) { return url; } url = getResource(GROOVY_AUTOCONFIG_FILE, myClassLoader, updateStatus); if (url != null) { return url; } url = getResource(TEST_AUTOCONFIG_FILE, myClassLoader, updateStatus); if (url != null) { return url; } return getResource(AUTOCONFIG_FILE, myClassLoader, updateStatus); }
private void multiplicityWarning(String resourceName, ClassLoader classLoader) { Set<URL> urlSet = null; StatusManager sm = loggerContext.getStatusManager(); try { urlSet = Loader.getResourceOccurrenceCount(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 String findURLOfDefaultConfigurationFile() { ClassLoader myClassLoader = Loader.getClassLoaderOfObject(this); URL url = getResource(TEST_AUTOCONFIG_FILE, myClassLoader); if (url != null) { return TEST_AUTOCONFIG_FILE; } url = getResource(AUTOCONFIG_FILE, myClassLoader); if (url != null) { return AUTOCONFIG_FILE; } return DEFAULT_CONFIG_FILE; }
public String getPropertyValue() { if (path == null) { return "false"; } URL resourceURL = Loader.getResourceBySelfClassLoader(path); return (resourceURL != null) ? "true" : "false"; }
private URL findConfigFileURLFromSystemProperties(ClassLoader classLoader, boolean updateStatus) { String logbackConfigFile = OptionHelper.getSystemProperty("logback.configurationFile"); if (logbackConfigFile != null) { URL result = null; URL f; try { result = new URL(logbackConfigFile); URL e = result; return e; } catch (MalformedURLException var13) { result = Loader.getResource(logbackConfigFile, classLoader); if (result == null) { File f1 = new File(logbackConfigFile); if (!f1.exists() || !f1.isFile()) { return null; } try { result = f1.toURI().toURL(); URL e1 = result; return e1; } catch (MalformedURLException var12) { return null; } } f = result; } finally { if (updateStatus) { this.statusOnResourceSearch(logbackConfigFile, classLoader, result); } } return f; } else { return null; } }
public URL findURLOfDefaultConfigurationFile(boolean updateStatus) { ClassLoader myClassLoader = Loader.getClassLoaderOfObject(this); URL url = this.findConfigFileURLFromSystemProperties(myClassLoader, updateStatus); if (url != null) { return url; } else { url = this.getResource("logback.groovy", myClassLoader, updateStatus); if (url != null) { return url; } else { url = this.getResource("logback-test.xml", myClassLoader, updateStatus); return url != null ? url : this.getResource("logback.xml", myClassLoader, updateStatus); } } }
private URL getResource(String filename, ClassLoader myClassLoader, boolean updateStatus) { URL url = Loader.getResource(filename, myClassLoader); if (updateStatus) { this.statusOnResourceSearch(filename, myClassLoader, url); } return url; }
/** * Returns the string "true" if the {@link #setResource(String) resource} specified by the * user is available on the class path, "false" otherwise. * * @return "true"|"false" depending on the availability of resource on the classpath */ public String getPropertyValue() { if (OptionHelper.isEmpty(resourceStr)) { addError("The \"resource\" property must be set."); return null; } URL resourceURL = Loader.getResourceBySelfClassLoader(resourceStr); return booleanAsStr(resourceURL != null); }
URL resourceAsURL(String resourceAttribute) { URL url = Loader.getResourceBySelfClassLoader(resourceAttribute); if (url == null) { optionalWarning("Could not find resource corresponding to [" + resourceAttribute + "]"); return null; } else return url; }
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); }
private URL findConfigFileURLFromSystemProperties(ClassLoader classLoader, boolean updateStatus) { String logbackConfigFile = OptionHelper.getSystemProperty(CONFIG_FILE_PROPERTY); if (logbackConfigFile != null) { URL result = null; try { result = new URL(logbackConfigFile); return result; } catch (MalformedURLException e) { // so, resource is not a URL: // attempt to get the resource from the class path result = Loader.getResource(logbackConfigFile, classLoader); if (result != null) { return result; } File f = new File(logbackConfigFile); if (f.exists() && f.isFile()) { try { result = f.toURI().toURL(); return result; } catch (MalformedURLException e1) { } } } finally { if (updateStatus) { statusOnResourceSearch(logbackConfigFile, classLoader, result); } } } return null; }
private URL getResource(String filename, ClassLoader myClassLoader, boolean updateStatus) { URL url = Loader.getResource(filename, myClassLoader); if (updateStatus) { statusOnResourceSearch(filename, myClassLoader, url); } return url; }
static public boolean isGroovyAvailable() { ClassLoader classLoader = Loader.getClassLoaderOfClass(EnvUtil.class); try { Class<?> bindingClass = classLoader.loadClass("groovy.lang.Binding"); return (bindingClass != null); } catch (ClassNotFoundException e) { return false; } }
@Test public void shouldConfigureFromXmlFile() throws MalformedURLException, JoranException { LoggerContext loggerContext = new LoggerContext(); ContextInitializer initializer = new ContextInitializer(loggerContext); assertNull(loggerContext.getObject(CoreConstants.SAFE_JORAN_CONFIGURATION)); URL configurationFileUrl = Loader.getResource("BOO_logback-test.xml", Thread.currentThread().getContextClassLoader()); initializer.configureByResource(configurationFileUrl); assertNotNull(loggerContext.getObject(CoreConstants.SAFE_JORAN_CONFIGURATION)); }
@Test public void shouldConfigureFromGroovyScript() throws MalformedURLException, JoranException { LoggerContext loggerContext = new LoggerContext(); ContextInitializer initializer = new ContextInitializer(loggerContext); assertNull(loggerContext.getObject(CoreConstants.CONFIGURATION_WATCH_LIST)); URL configurationFileUrl = Loader.getResource("test.groovy", Thread.currentThread().getContextClassLoader()); initializer.configureByResource(configurationFileUrl); assertNotNull(loggerContext.getObject(CoreConstants.CONFIGURATION_WATCH_LIST)); }
@Test public void shouldThrowExceptionIfUnexpectedConfigurationFileExtension() throws JoranException { LoggerContext loggerContext = new LoggerContext(); ContextInitializer initializer = new ContextInitializer(loggerContext); URL configurationFileUrl = Loader.getResource("README.txt", Thread.currentThread().getContextClassLoader()); try { initializer.configureByResource(configurationFileUrl); fail("Should throw LogbackException"); } catch (LogbackException expectedException) { // pass } }
static void configureViaXML_File() { LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); try { JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(lc); lc.reset(); URL url = Loader.getResourceBySelfClassLoader("chapters/mdc/simpleMDC.xml"); configurator.doConfigure(url); } catch (JoranException je) { StatusPrinter.print(lc); } }
static KonkerDefaultContextSelector dynamicalContextSelector(KonkerLoggerContext defaultLoggerContext, String contextSelectorStr) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { Class contextSelectorClass = Loader.loadClass(contextSelectorStr); Constructor cons = contextSelectorClass.getConstructor(new Class[]{LoggerContext.class}); return (KonkerDefaultContextSelector) cons.newInstance(new Object[]{defaultLoggerContext}); }
public static void autoConfig(Auditor auditor) throws AuditException { ClassLoader tccl = Loader.getTCL(); autoConfig(auditor, tccl); }
public static void autoConfig(Auditor auditor, ClassLoader classLoader) throws AuditException { String autoConfigFileByProperty = System .getProperty(AUTOCONFIG_FILE_PROPERTY); String pathPrefix = clientApplication.getName() + "/"; URL url = null; InputStream inputStream = null; if (StringUtils.hasText(host) && port > 0) { final byte[] bytes = new StringBuilder() .append("<auditor><appender name=\"server\" class=\"ch.qos.logback.audit.client.net.SocketAuditAppender\"><remoteHost>") .append(host) .append("</remoteHost><port>") .append(port) .append("</port></appender></auditor>") .toString() .getBytes(StandardCharsets.UTF_8); inputStream = new ByteArrayInputStream(bytes); } else if (autoConfigFileByProperty != null) { url = Loader.getResource(pathPrefix + autoConfigFileByProperty, classLoader); } else { url = Loader.getResource(pathPrefix + TEST_AUTOCONFIG_FILE, classLoader); if (url == null) { url = Loader.getResource(pathPrefix + AUTOCONFIG_FILE, classLoader); } } if (inputStream != null) { configureByInputStream(auditor, inputStream); try { inputStream.close(); } catch (IOException e) { throw new AuditException("Failed loading configuration file from input stream: " + e.getMessage(), e); } } else if (url != null) { configureByResource(auditor, url); } else { String errMsg; if (autoConfigFileByProperty != null) { errMsg = "Failed to find configuration file [" + pathPrefix + autoConfigFileByProperty + "]."; } else { errMsg = "Failed to find logback-audit configuration files [" + pathPrefix + TEST_AUTOCONFIG_FILE + "] or [" + pathPrefix + AUTOCONFIG_FILE + "]."; } throw new AuditException(errMsg); } }
public void begin(InterpretationContext ec, String localName, Attributes attributes) { // LogLog.debug("in NestComponentIA begin method"); // get the action data object pushed in isApplicable() method call IADataForComplexProperty actionData = (IADataForComplexProperty) actionDataStack .peek(); String className = attributes.getValue(CLASS_ATTRIBUTE); // perform variable name substitution className = ec.subst(className); Class<?> componentClass = null; try { if (!OptionHelper.isEmpty(className)) { componentClass = Loader.loadClass(className, context); } else { // guess class name via implicit rules PropertySetter parentBean = actionData.parentBean; componentClass = parentBean.getClassNameViaImplicitRules(actionData .getComplexPropertyName(), actionData.getAggregationType(), ec .getDefaultNestedComponentRegistry()); } if (componentClass == null) { actionData.inError = true; String errMsg = "Could not find an appropriate class for property [" + localName + "]"; addError(errMsg); return; } if (OptionHelper.isEmpty(className)) { addInfo("Assuming default type [" + componentClass.getName() + "] for [" + localName + "] property"); } actionData.setNestedComplexProperty(componentClass.newInstance()); // pass along the repository if (actionData.getNestedComplexProperty() instanceof ContextAware) { ((ContextAware) actionData.getNestedComplexProperty()) .setContext(this.context); } //addInfo("Pushing component [" + localName // + "] on top of the object stack."); ec.pushObject(actionData.getNestedComplexProperty()); } catch (Exception oops) { actionData.inError = true; String msg = "Could not create component [" + localName + "] of type [" + className + "]"; addError(msg, oops); } }
private static ClassLoader getServiceLoaderClassLoader() { return testServiceLoaderClassLoader == null ? Loader.getClassLoaderOfClass(EnvUtil.class) : testServiceLoaderClassLoader; }
private URL getResource(String filename, ClassLoader myClassLoader) { return Loader.getResource(filename, myClassLoader); }
public LoggerContext getLoggerContext() { //System.out.println("getLoggerContext request"); // First check if ThreadLocal has been set already LoggerContext lc = threadLocal.get(); if (lc != null) { //System.out.printf("Thread local found: %s\n", lc.getName()); return lc; } if (contextName == null) { //System.out.println("Context name was null, returning default"); // We return the default context return defaultContext; } else { // Let's see if we already know such a context LoggerContext loggerContext = contextMap.get(contextName); //System.out.printf("Logger context for %s is %s\n", contextName, loggerContext); if (loggerContext == null) { // We have to create a new LoggerContext loggerContext = new LoggerContext(); loggerContext.setName(contextName); // allow override using logbacks system prop String overrideProperty = System.getProperty("logback.configurationFile"); if (overrideProperty == null) { contextConfigFile = String.format("logback-%s.xml", contextName); } else { contextConfigFile = String.format(overrideProperty, contextName); } System.out.printf("Context logger config file: %s\n", contextConfigFile); ClassLoader classloader = Thread.currentThread().getContextClassLoader(); //System.out.printf("Thread context cl: %s\n", classloader); //ClassLoader classloader2 = Loader.class.getClassLoader(); //System.out.printf("Loader tcl: %s\n", classloader2); //URL url = Loader.getResourceBySelfClassLoader(contextConfigFile); URL url = Loader.getResource(contextConfigFile, classloader); if (url != null) { try { JoranConfigurator configurator = new JoranConfigurator(); loggerContext.reset(); configurator.setContext(loggerContext); configurator.doConfigure(url); } catch (JoranException e) { StatusPrinter.print(loggerContext); } } else { try { ContextInitializer ctxInit = new ContextInitializer(loggerContext); ctxInit.autoConfig(); } catch (JoranException je) { StatusPrinter.print(loggerContext); } } System.out.printf("Adding logger context: %s to map for context: %s\n", loggerContext.getName(), contextName); contextMap.put(contextName, loggerContext); } return loggerContext; } }
/** * Instantiate the context selector class designated by the user. The selector * must have a constructor taking a LoggerContext instance as an argument. * * @param defaultLoggerContext * @param contextSelectorStr * @return an instance of the designated context selector class * @throws ClassNotFoundException * @throws SecurityException * @throws NoSuchMethodException * @throws IllegalArgumentException * @throws InstantiationException * @throws IllegalAccessException * @throws InvocationTargetException */ static ContextSelector dynamicalContextSelector( LoggerContext defaultLoggerContext, String contextSelectorStr) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { Class<?> contextSelectorClass = Loader.loadClass(contextSelectorStr); Constructor cons = contextSelectorClass .getConstructor(new Class[] { LoggerContext.class }); return (ContextSelector) cons.newInstance(defaultLoggerContext); }