Search in sources :

Example 41 with Configuration

use of org.osgi.service.cm.Configuration in project sling by apache.

the class SlingLogPanel method configureLogger.

/**
     * Configures the logger with the given pid. If the pid is empty a new logger configuration is created.
     *
     * @param pid      configuration pid of the logger
     * @param logLevel the log level to set
     * @param loggers  list of logger categories to set
     * @param logFile  log file (relative path is ok)
     * @param additive logger additivity
     * @throws IOException            when an existing configuration couldn't be updated or a configuration couldn't be created.
     * @throws ConfigurationException when mandatory parameters where not specified
     */
private void configureLogger(final String pid, final String logLevel, final String[] loggers, final String logFile, boolean additive) throws IOException, ConfigurationException {
    // try to get the configadmin service reference
    ServiceReference sr = this.bundleContext.getServiceReference(ConfigurationAdmin.class.getName());
    if (sr != null) {
        try {
            if (logLevel == null) {
                throw new ConfigurationException(LogConfigManager.LOG_LEVEL, "Log level has to be specified.");
            }
            if (loggers == null) {
                throw new ConfigurationException(LogConfigManager.LOG_LOGGERS, "Logger categories have to be specified.");
            }
            if (logFile == null) {
                throw new ConfigurationException(LogConfigManager.LOG_FILE, "LogFile name has to be specified.");
            }
            // try to get the configadmin
            final ConfigurationAdmin configAdmin = (ConfigurationAdmin) this.bundleContext.getService(sr);
            if (configAdmin != null) {
                Configuration config;
                if (pid == null || pid.length() == 0) {
                    config = configAdmin.createFactoryConfiguration(LogConfigManager.FACTORY_PID_CONFIGS);
                } else {
                    config = configAdmin.getConfiguration(pid);
                }
                if (config != null) {
                    Dictionary<String, Object> dict = new Hashtable<String, Object>();
                    dict.put(LogConfigManager.LOG_LEVEL, logLevel.toLowerCase());
                    dict.put(LogConfigManager.LOG_LOGGERS, loggers);
                    dict.put(LogConfigManager.LOG_FILE, logFile);
                    if (additive) {
                        dict.put(LogConfigManager.LOG_ADDITIV, "true");
                    } else {
                        dict.put(LogConfigManager.LOG_ADDITIV, "false");
                    }
                    config.update(dict);
                }
            }
        } finally {
            // release the configadmin reference
            this.bundleContext.ungetService(sr);
        }
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) ConfigurationException(org.apache.sling.commons.log.logback.internal.config.ConfigurationException) Hashtable(java.util.Hashtable) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) ServiceReference(org.osgi.framework.ServiceReference)

Example 42 with Configuration

use of org.osgi.service.cm.Configuration in project sling by apache.

the class ITConfigPrinter method includeOnlyLastNFiles.

@Test
public void includeOnlyLastNFiles() throws Exception {
    waitForPrinter();
    Configuration config = ca.getConfiguration(PID, null);
    Dictionary<String, Object> p = new Hashtable<String, Object>();
    p.put(LogConfigManager.PRINTER_MAX_INCLUDED_FILES, 3);
    p.put(LogConfigManager.LOG_LEVEL, "INFO");
    config.update(p);
    delay();
    createLogConfig("error.log", "includeOnlyLastNFiles", "includeOnlyLastNFiles.1");
    //txt mode log should at least have mention of all files
    for (int i = 0; i < 10; i++) {
        FileUtils.touch(new File(logDir, "error.log." + i));
    }
    StringWriter sw = new StringWriter();
    invoke("printConfiguration", new PrintWriter(sw), "txt");
    assertThat(sw.toString(), containsString("error.log"));
    for (int i = 0; i < 10; i++) {
        assertThat(sw.toString(), containsString("error.log." + i));
    }
    //Attachment should only be upto 3
    assertTrue(((URL[]) invoke("getAttachments", "zip")).length > 3);
}
Also used : Configuration(org.osgi.service.cm.Configuration) StringWriter(java.io.StringWriter) Hashtable(java.util.Hashtable) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) File(java.io.File) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 43 with Configuration

use of org.osgi.service.cm.Configuration in project sling by apache.

the class ITConfigPrinter method createLogConfig.

private void createLogConfig(String fileName, String... logConfigs) throws IOException {
    Configuration config = ca.createFactoryConfiguration(FACTORY_PID_CONFIGS, null);
    Dictionary<String, Object> p = new Hashtable<String, Object>();
    p.put(LogConfigManager.LOG_LOGGERS, logConfigs);
    p.put(LogConfigManager.LOG_LEVEL, "DEBUG");
    p.put(LogConfigManager.LOG_FILE, fileName);
    config.update(p);
    delay();
}
Also used : Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 44 with Configuration

use of org.osgi.service.cm.Configuration in project sling by apache.

the class ITAppenderServices method testOsgiAppenderRef.

@Test
public void testOsgiAppenderRef() throws Exception {
    Configuration config = ca.getConfiguration(ITConfigAdminSupport.PID, null);
    Dictionary<String, Object> p = new Hashtable<String, Object>();
    p.put(ITConfigAdminSupport.LOG_LEVEL, "INFO");
    p.put(ITConfigAdminSupport.LOGBACK_FILE, absolutePath("test-osg-appender-ref-config.xml"));
    config.update(p);
    delay();
    Logger ref = (Logger) LoggerFactory.getLogger("foo.ref.osgi");
    assertTrue(ref.isDebugEnabled());
    TestAppender ta = registerAppender("foo.bar", "foo.baz");
    delay();
    Logger bar = (Logger) LoggerFactory.getLogger("foo.bar");
    bar.setLevel(Level.DEBUG);
    Logger baz = (Logger) LoggerFactory.getLogger("foo.baz");
    baz.setLevel(Level.INFO);
    bar.debug("Test message");
    // Would not be logged
    baz.debug("Test message");
    ref.debug("Test message ref");
    // One event should be logged.
    assertEquals(2, ta.events.size());
}
Also used : Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) Logger(ch.qos.logback.classic.Logger) Test(org.junit.Test)

Example 45 with Configuration

use of org.osgi.service.cm.Configuration in project sling by apache.

the class ITConfigAdminSupport method testChangeLogLevelWithConfig.

@Test
public void testChangeLogLevelWithConfig() throws Exception {
    // Set log level to debug for foo1.bar
    Configuration config = ca.createFactoryConfiguration(FACTORY_PID_CONFIGS, null);
    Dictionary<String, Object> p = new Hashtable<String, Object>();
    p.put(LOG_LOGGERS, new String[] { "foo1.bar" });
    p.put(LOG_LEVEL, "DEBUG");
    config.update(p);
    delay();
    Logger slf4jLogger = LoggerFactory.getLogger("foo1.bar");
    assertTrue(slf4jLogger.isDebugEnabled());
    assertTrue(LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME).isInfoEnabled());
    assertFalse(LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME).isDebugEnabled());
    // foo1.bar should not have explicit appender attached with it
    Iterator<Appender<ILoggingEvent>> itr = ((ch.qos.logback.classic.Logger) slf4jLogger).iteratorForAppenders();
    assertFalse(itr.hasNext());
}
Also used : Appender(ch.qos.logback.core.Appender) Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) Logger(org.slf4j.Logger) Test(org.junit.Test)

Aggregations

Configuration (org.osgi.service.cm.Configuration)226 Test (org.junit.Test)85 Hashtable (java.util.Hashtable)75 IOException (java.io.IOException)55 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)49 Dictionary (java.util.Dictionary)36 ArrayList (java.util.ArrayList)19 HashMap (java.util.HashMap)19 ServiceReference (org.osgi.framework.ServiceReference)19 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)18 Matchers.anyString (org.mockito.Matchers.anyString)16 BundleContext (org.osgi.framework.BundleContext)15 RegistrySourceConfiguration (org.codice.ddf.registry.federationadmin.service.internal.RegistrySourceConfiguration)11 Map (java.util.Map)10 Bundle (org.osgi.framework.Bundle)10 File (java.io.File)9 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)9 Mockito.anyString (org.mockito.Mockito.anyString)9 AbstractIntegrationTest (org.codice.ddf.itests.common.AbstractIntegrationTest)8 SkipUnstableTest (org.codice.ddf.itests.common.annotations.SkipUnstableTest)7