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);
}
}
}
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);
}
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();
}
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());
}
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());
}
Aggregations