You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by tj...@apache.org on 2021/05/18 14:52:45 UTC
[felix-dev] branch master updated: [FELIX-6407] SCR Logging Fixes
and Improvements
This is an automated email from the ASF dual-hosted git repository.
tjwatson pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/felix-dev.git
The following commit(s) were added to refs/heads/master by this push:
new 589ccc2 [FELIX-6407] SCR Logging Fixes and Improvements
new c1197d9 Merge pull request #75 from amitjoy/scr-logging-enhancement
589ccc2 is described below
commit 589ccc23875ca6afe411aa37b860b667de86c702
Author: Amit Kumar Mondal <ad...@amitinside.com>
AuthorDate: Wed May 5 04:02:26 2021 +0200
[FELIX-6407] SCR Logging Fixes and Improvements
1. Fixed Problem with Extended Logging
2. Introduced functionality to disable logging
Fixes #FELIX-6407
Signed-off-by: Amit Kumar Mondal <ad...@amitinside.com>
---
.../java/org/apache/felix/scr/impl/Activator.java | 6 +-
.../scr/impl/config/ScrConfigurationImpl.java | 54 +++++++++---
.../felix/scr/impl/logger/ExtLogManager.java | 15 ++--
.../felix/scr/impl/logger/LogConfiguration.java | 97 +++++++++++++++++++++
.../apache/felix/scr/impl/logger/LogManager.java | 32 ++++++-
.../apache/felix/scr/impl/logger/NoOpLogger.java | 71 ++++++++++++++++
.../felix/scr/impl/logger/ScrLogManager.java | 30 +------
.../felix/scr/impl/logger/ScrLoggerFactory.java | 71 ++++++++++++++++
.../felix/scr/impl/manager/ScrConfiguration.java | 24 +-----
.../apache/felix/scr/impl/logger/LoggerTest.java | 99 +++++++++++++++-------
10 files changed, 397 insertions(+), 102 deletions(-)
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/Activator.java b/scr/src/main/java/org/apache/felix/scr/impl/Activator.java
index a419f43..3183581 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/Activator.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/Activator.java
@@ -43,7 +43,7 @@ import java.util.concurrent.locks.ReentrantLock;
import org.apache.felix.scr.impl.config.ScrConfigurationImpl;
import org.apache.felix.scr.impl.inject.internal.ClassUtils;
import org.apache.felix.scr.impl.logger.InternalLogger.Level;
-import org.apache.felix.scr.impl.logger.ScrLogManager;
+import org.apache.felix.scr.impl.logger.ScrLoggerFactory;
import org.apache.felix.scr.impl.logger.ScrLogger;
import org.apache.felix.scr.impl.manager.ComponentHolder;
import org.apache.felix.scr.impl.metadata.ComponentMetadata;
@@ -657,10 +657,10 @@ public class Activator extends AbstractExtender
// TODO we only set the logger once
// If the need arises to be able to dynamically set the logger type
// then more work is needed to do that switch
- // for now we only can configure ds.log.extension with context properties
+ // for now we only can configure 'ds.log.extension' and 'ds.log.enabled' with context properties
if (logger == null)
{
- logger = ScrLogManager.scr(m_context, m_configuration);
+ logger = ScrLoggerFactory.create(m_context, m_configuration);
}
}
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/config/ScrConfigurationImpl.java b/scr/src/main/java/org/apache/felix/scr/impl/config/ScrConfigurationImpl.java
index 6d6fd20..9a0417a 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/config/ScrConfigurationImpl.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/config/ScrConfigurationImpl.java
@@ -82,7 +82,10 @@ public class ScrConfigurationImpl implements ScrConfiguration
private boolean infoAsService;
private boolean cacheMetadata;
- private boolean logExtension;
+
+ private boolean isLogEnabled;
+
+ private boolean isLogExtensionEnabled;
private long lockTimeout = DEFAULT_LOCK_TIMEOUT_MILLISECONDS;
@@ -179,7 +182,8 @@ public class ScrConfigurationImpl implements ScrConfiguration
serviceChangecountTimeout = DEFAULT_SERVICE_CHANGECOUNT_TIMEOUT_MILLISECONDS;
newGlobalExtender = false;
cacheMetadata = false;
- logExtension = false;
+ isLogEnabled = true;
+ isLogExtensionEnabled = false;
}
else
{
@@ -192,7 +196,8 @@ public class ScrConfigurationImpl implements ScrConfiguration
serviceChangecountTimeout = getServiceChangecountTimeout();
newGlobalExtender = getDefaultGlobalExtender();
cacheMetadata = getDefaultCacheMetadata();
- logExtension = getDefaultLogExtension();
+ isLogEnabled = getDefaultLogEnabled();
+ isLogExtensionEnabled = getDefaultLogExtension();
}
}
else
@@ -213,7 +218,8 @@ public class ScrConfigurationImpl implements ScrConfiguration
newGlobalExtender = VALUE_TRUE.equalsIgnoreCase( String.valueOf( config.get( PROP_GLOBAL_EXTENDER) ) );
cacheMetadata = VALUE_TRUE.equalsIgnoreCase(
String.valueOf(config.get(PROP_CACHE_METADATA)));
- logExtension = VALUE_TRUE.equalsIgnoreCase(String.valueOf(config.get(PROP_LOG_EXTENSION)));
+ isLogEnabled = checkIfLogEnabled(config);
+ isLogExtensionEnabled = VALUE_TRUE.equalsIgnoreCase(String.valueOf(config.get(PROP_LOG_EXTENSION)));
}
if ( scrCommand != null )
{
@@ -254,7 +260,6 @@ public class ScrConfigurationImpl implements ScrConfiguration
return keepInstances;
}
- @SuppressWarnings("deprecation")
@Override
public boolean infoAsService()
{
@@ -409,13 +414,38 @@ public class ScrConfigurationImpl implements ScrConfiguration
return Level.ERROR;
}
- @Override
- public boolean isLogExtension() {
- return logExtension;
- }
+ @Override
+ public boolean isLogEnabled()
+ {
+ return isLogEnabled;
+ }
- private boolean getDefaultLogExtension() {
- return VALUE_TRUE.equalsIgnoreCase( bundleContext.getProperty( PROP_LOG_EXTENSION) );
- }
+ @Override
+ public boolean isLogExtensionEnabled()
+ {
+ return isLogExtensionEnabled;
+ }
+
+ private boolean getDefaultLogExtension()
+ {
+ return VALUE_TRUE.equalsIgnoreCase(bundleContext.getProperty(PROP_LOG_EXTENSION));
+ }
+
+ private boolean getDefaultLogEnabled()
+ {
+ String isLogEnabled = bundleContext.getProperty(PROP_LOG_ENABLED);
+ return isLogEnabled == null ? true : VALUE_TRUE.equalsIgnoreCase(isLogEnabled);
+ }
+
+ private boolean checkIfLogEnabled(Dictionary<String, ?> properties)
+ {
+ Object isLogEnabled = properties.get(PROP_LOG_ENABLED);
+ if (isLogEnabled == null)
+ {
+ return true;
+ }
+ return isLogEnabled == null ? true : Boolean.parseBoolean(isLogEnabled.toString());
+ }
+
}
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/logger/ExtLogManager.java b/scr/src/main/java/org/apache/felix/scr/impl/logger/ExtLogManager.java
index 759d63d..cf42637 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/logger/ExtLogManager.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/logger/ExtLogManager.java
@@ -18,7 +18,6 @@
*/
package org.apache.felix.scr.impl.logger;
-import org.apache.felix.scr.impl.manager.ScrConfiguration;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
@@ -41,7 +40,7 @@ class ExtLogManager extends ScrLogManager
public static String SCR_LOGGER_PREFIX = "org.apache.felix.scr.";
private final Bundle bundle;
- ExtLogManager(BundleContext context, ScrConfiguration config)
+ ExtLogManager(BundleContext context, LogConfiguration config)
{
super(context, config);
this.bundle = context.getBundle();
@@ -50,13 +49,16 @@ class ExtLogManager extends ScrLogManager
@Override
public ScrLogger scr()
{
- return getLogger(bundle, SCR_LOGGER_NAME, ScrLoggerFacade.class);
+ // use the log level from the scr bundle itself
+ return getLogger(this.bundle, SCR_LOGGER_NAME, ScrLoggerFacade.class);
}
@Override
public BundleLogger bundle(Bundle bundle)
{
- return getLogger(bundle, SCR_LOGGER_PREFIX.concat(bundle.getSymbolicName()),
+ // use the log level of the scr bundle itself
+ // we will not use the log level of the bundle containing the component (extended bundle)
+ return getLogger(this.bundle, SCR_LOGGER_PREFIX.concat(bundle.getSymbolicName()),
ScrLoggerFacade.class);
}
@@ -72,7 +74,10 @@ class ExtLogManager extends ScrLogManager
String loggerName = SCR_LOGGER_PREFIX.concat(bundle.getSymbolicName()).concat(
".").concat(componentName);
- ScrLoggerFacade logger = getLogger(bundle, loggerName, ScrLoggerFacade.class);
+
+ // use the log level of the scr bundle itself
+ // we will not use the log level of the bundle containing the component (extended bundle)
+ ScrLoggerFacade logger = getLogger(this.bundle, loggerName, ScrLoggerFacade.class);
logger.setPrefix("[" + componentName + "]");
return logger;
}
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/logger/LogConfiguration.java b/scr/src/main/java/org/apache/felix/scr/impl/logger/LogConfiguration.java
new file mode 100644
index 0000000..d9b0de7
--- /dev/null
+++ b/scr/src/main/java/org/apache/felix/scr/impl/logger/LogConfiguration.java
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.scr.impl.logger;
+
+import org.apache.felix.scr.impl.logger.InternalLogger.Level;
+
+/**
+ * This is used to deal with the log configuration.
+ *
+ * <p>
+ * The log configuration comprises the following:
+ *
+ * <p>
+ * <ul>
+ * <li>The associated log level</li>
+ * <li>flag if the log is enabled</li>
+ * <li>flag if the log extension is enabled</li>
+ * </ul>
+ *
+ * <p>
+ * Note that, any consumer can decide if the logging in SCR is at all required.
+ * By default, the SCR logging will be enabled. Consumer can decide to set the
+ * following property to {@code false} to disable the SCR logging completely.
+ *
+ * <p>
+ * {@code ds.log.enabled}
+ *
+ * <p>
+ * Also note that, consumer can also decide to enable log extension by setting
+ * the following property to {@code true}. This also implies that the logging is
+ * enabled.
+ *
+ * <p>
+ * {@code ds.log.extension}
+ *
+ * <p>
+ * Note that, by default SCR uses the log level of the bundle that contains the
+ * SCR components to log the messages, but the log extension uses the log level
+ * of the SCR bundle itself to log the messages.
+ *
+ */
+public interface LogConfiguration
+{
+ /**
+ * The property to retrieve the log level
+ */
+ String PROP_LOGLEVEL = "ds.loglevel";
+
+ /**
+ * The property to enable or disable the logging
+ */
+ String PROP_LOG_ENABLED = "ds.log.enabled";
+
+ /**
+ * The property to enable log extension
+ */
+ String PROP_LOG_EXTENSION = "ds.log.extension";
+
+ /**
+ * Returns the current log level
+ *
+ * @return the log level (cannot be {@code null})
+ */
+ Level getLogLevel();
+
+ /**
+ * Checks if the logging is enabled. Disabling logging is incompatible
+ * with the OSGi specification.
+ *
+ * @return {@code true} if enabled otherwise {@code false}
+ */
+ boolean isLogEnabled();
+
+ /**
+ * Checks if the log extension is enabled. The extension is incompatible
+ * with the OSGi specification.
+ *
+ * @return {@code true} if enabled otherwise {@code false}
+ */
+ boolean isLogExtensionEnabled();
+}
\ No newline at end of file
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/logger/LogManager.java b/scr/src/main/java/org/apache/felix/scr/impl/logger/LogManager.java
index 027f4a6..dd3cae0 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/logger/LogManager.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/logger/LogManager.java
@@ -143,12 +143,40 @@ class LogManager extends ServiceTracker<Object, Object> implements BundleListene
}
final Lock lock = new Lock();
+
+ private final LogConfiguration config;
- LogManager(BundleContext context)
+ LogManager(BundleContext context, LogConfiguration config)
{
super(context, LOGGER_FACTORY_CLASS_NAME, null);
this.scrContext = context;
- scrContext.addBundleListener(this);
+ this.config = config;
+ }
+
+ /**
+ * Initializes the log manager. This internally executes the following:
+ *
+ * <ul>
+ * <li>
+ * Track all bundles for retrieving the log levels of each bundles if the log extension
+ * is ({@code ds.log.extension}) not set or set to {@code false} (log extension disabled)
+ * </li>
+ * <li>
+ * Don't track any bundles if log extension is enabled since we don't need the log levels
+ * of the respective bundles. For log extension, we use the log level of the SCR bundle itself.
+ * <li>
+ * <li>
+ * Start the service tracker to track the OSGi LoggerFactory service
+ * </li>
+ * </ul>
+ */
+ public void init()
+ {
+ if (!config.isLogExtensionEnabled())
+ {
+ scrContext.addBundleListener(this);
+ }
+ this.open();
}
@Override
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/logger/NoOpLogger.java b/scr/src/main/java/org/apache/felix/scr/impl/logger/NoOpLogger.java
new file mode 100644
index 0000000..959a93f
--- /dev/null
+++ b/scr/src/main/java/org/apache/felix/scr/impl/logger/NoOpLogger.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.scr.impl.logger;
+
+import org.osgi.framework.Bundle;
+
+/**
+ * This is a dummy logger which is only used when the logging is not enabled at all.
+ */
+public class NoOpLogger implements ScrLogger, BundleLogger, ComponentLogger
+{
+
+ @Override
+ public void log(Level level, String message, Throwable ex)
+ {
+
+ }
+
+ @Override
+ public void log(Level level, String message, Throwable ex, Object... args)
+ {
+
+ }
+
+ @Override
+ public boolean isLogEnabled(Level level)
+ {
+ return false;
+ }
+
+ @Override
+ public void setComponentId(long componentId)
+ {
+
+ }
+
+ @Override
+ public ComponentLogger component(Bundle bundle, String implementationClassName, String name)
+ {
+ return this;
+ }
+
+ @Override
+ public BundleLogger bundle(Bundle bundle)
+ {
+ return this;
+ }
+
+ @Override
+ public void close()
+ {
+
+ }
+
+}
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/logger/ScrLogManager.java b/scr/src/main/java/org/apache/felix/scr/impl/logger/ScrLogManager.java
index 56e88b5..ed63476 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/logger/ScrLogManager.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/logger/ScrLogManager.java
@@ -24,7 +24,6 @@ import java.io.StringWriter;
import java.text.MessageFormat;
import org.apache.felix.scr.impl.logger.InternalLogger.Level;
-import org.apache.felix.scr.impl.manager.ScrConfiguration;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.service.log.Logger;
@@ -40,32 +39,11 @@ public class ScrLogManager extends LogManager
{
private final Bundle bundle;
- private final ScrConfiguration config;
+ private final LogConfiguration config;
- /**
- * Get a new log manager based on the configuration.
- *
- * @param context
- * the bundle context of the SCR bundle
- * @param config
- * the SCR configuration
- * @return a proper ScrLogManager
- */
-
- public static ScrLogger scr(BundleContext context, ScrConfiguration config)
- {
- ScrLogManager manager;
- if (config.isLogExtension())
- manager = new ExtLogManager(context, config);
- else
- manager = new ScrLogManager(context, config);
- manager.open();
- return manager.scr();
- }
-
- ScrLogManager(BundleContext context, ScrConfiguration config)
+ ScrLogManager(BundleContext context, LogConfiguration config)
{
- super(context);
+ super(context, config);
this.config = config;
this.bundle = context.getBundle();
}
@@ -74,7 +52,7 @@ public class ScrLogManager extends LogManager
* This logger is used for the main code of SCR. This will use the SCR
* bundle & the {@link Logger#ROOT_LOGGER_NAME}
*
- * @return an Scr Logger.
+ * @return scr logger.
*/
public ScrLogger scr()
{
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/logger/ScrLoggerFactory.java b/scr/src/main/java/org/apache/felix/scr/impl/logger/ScrLoggerFactory.java
new file mode 100644
index 0000000..80dbc05
--- /dev/null
+++ b/scr/src/main/java/org/apache/felix/scr/impl/logger/ScrLoggerFactory.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.scr.impl.logger;
+
+import org.osgi.framework.BundleContext;
+
+/**
+ * This is used to retrieve the appropriate logger instance based on
+ * a specific log configuration
+ */
+public final class ScrLoggerFactory
+{
+ /** Non-instantiable */
+ private ScrLoggerFactory()
+ {
+ throw new IllegalAccessError("Cannot be instantiated");
+ }
+
+ /**
+ * Retrieves the logger based on the provided log configuration
+ *
+ * <ul>
+ * <li>If the logging is disabled, the {@link NoOpLogger} is used</li>
+ * <li>If the logging is enabled but the log extension is disabled, use {@link ScrLogManager}</li>
+ * <li>If the logging is enabled and the log extension is also enabled, use {@link ExtLogManager}</li>
+ * </ul>
+ *
+ * @param context the bundle context of the SCR bundle
+ * @param config the log configuration
+ *
+ * @return the logger
+ */
+ public static ScrLogger create(BundleContext context, LogConfiguration config)
+ {
+ if (!config.isLogEnabled())
+ {
+ return new NoOpLogger();
+ }
+ else
+ {
+ ScrLogManager manager = null;
+ if (config.isLogExtensionEnabled())
+ {
+ manager = new ExtLogManager(context, config);
+ }
+ else
+ {
+ manager = new ScrLogManager(context, config);
+ }
+ manager.init();
+ return manager.scr();
+ }
+ }
+
+}
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/manager/ScrConfiguration.java b/scr/src/main/java/org/apache/felix/scr/impl/manager/ScrConfiguration.java
index 65a5254..f4b89f9 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/manager/ScrConfiguration.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/manager/ScrConfiguration.java
@@ -18,7 +18,7 @@
*/
package org.apache.felix.scr.impl.manager;
-import org.apache.felix.scr.impl.logger.InternalLogger.Level;
+import org.apache.felix.scr.impl.logger.LogConfiguration;
/**
* The <code>ScrConfiguration</code> class conveys configuration for the
@@ -42,7 +42,7 @@ import org.apache.felix.scr.impl.logger.InternalLogger.Level;
* <a href="http://felix.apache.org/site/apache-felix-service-component-runtime.html">Apache Felix Service Component Runtime</a>
* documentation page for detailed information.
*/
-public interface ScrConfiguration
+public interface ScrConfiguration extends LogConfiguration
{
String PID = "org.apache.felix.scr.ScrService";
@@ -63,12 +63,6 @@ public interface ScrConfiguration
long DEFAULT_STOP_TIMEOUT_MILLISECONDS = 60000;
- String PROP_LOGLEVEL = "ds.loglevel";
- /**
- * See {@link #isLogExtension()}
- */
- String PROP_LOG_EXTENSION = "ds.log.extension";
-
String PROP_GLOBAL_EXTENDER="ds.global.extender";
String PROP_SERVICE_CHANGECOUNT_TIMEOUT = "ds.service.changecount.timeout";
@@ -76,13 +70,6 @@ public interface ScrConfiguration
String PROP_CACHE_METADATA = "ds.cache.metadata";
- /**
- * Returns the current log level.
- * @return
- */
- Level getLogLevel();
-
-
boolean isFactoryEnabled();
@@ -107,11 +94,4 @@ public interface ScrConfiguration
boolean cacheMetadata();
-
- /**
- * If true, use a logging extension. The extension can be incompatible with the OSGi specification.
- * @return true if extension allowed
- */
- boolean isLogExtension();
-
}
diff --git a/scr/src/test/java/org/apache/felix/scr/impl/logger/LoggerTest.java b/scr/src/test/java/org/apache/felix/scr/impl/logger/LoggerTest.java
index e0714da..faa534e 100644
--- a/scr/src/test/java/org/apache/felix/scr/impl/logger/LoggerTest.java
+++ b/scr/src/test/java/org/apache/felix/scr/impl/logger/LoggerTest.java
@@ -38,7 +38,6 @@ import java.util.ServiceLoader;
import org.apache.felix.scr.impl.logger.InternalLogger.Level;
import org.apache.felix.scr.impl.logger.LogManager.LoggerFacade;
import org.apache.felix.scr.impl.logger.LogService.LogEntry;
-import org.apache.felix.scr.impl.manager.ScrConfiguration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -115,7 +114,7 @@ public class LoggerTest {
public void formatTest() {
LogService l = new LogService(log.getBundleContext());
l.register();
- ScrConfiguration config = mock(ScrConfiguration.class);
+ LogConfiguration config = mock(LogConfiguration.class);
when(config.getLogLevel()).thenReturn(Level.DEBUG);
ExtLogManager elm = new ExtLogManager(scr.getBundleContext(), config);
elm.open();
@@ -129,10 +128,12 @@ public class LoggerTest {
@Test
public void testStandardOutput() throws IOException {
- ScrConfiguration config = mock(ScrConfiguration.class);
+ LogConfiguration config = mock(LogConfiguration.class);
when(config.getLogLevel()).thenReturn(Level.DEBUG);
+
ExtLogManager elm = new ExtLogManager(scr.getBundleContext(), config);
elm.open();
+
try (Buf out = new Buf(System.out);
Buf err = new Buf(System.err);) {
try {
@@ -185,15 +186,17 @@ public class LoggerTest {
LogService l = new LogService(log.getBundleContext());
l.register();
- ScrConfiguration config = mock(ScrConfiguration.class);
+ LogConfiguration config = mock(LogConfiguration.class);
when(config.getLogLevel()).thenReturn(Level.DEBUG);
- when(config.isLogExtension()).thenReturn(true);
- ScrLogger logger = ScrLogManager.scr(scr.getBundleContext(), config);
+ when(config.isLogEnabled()).thenReturn(true);
+ when(config.isLogExtensionEnabled()).thenReturn(true);
+
+ ScrLogger logger = ScrLoggerFactory.create(scr.getBundleContext(), config);
BundleLogger bundle = logger.bundle(component);
bundle.log(Level.ERROR, "Ext", null);
assertThat(l.entries).hasSize(1);
LogEntry le = l.entries.get(0);
- assertThat(le.bundle).isEqualTo(component);
+ assertThat(le.bundle).isEqualTo(scr);
}
@Test
@@ -201,11 +204,13 @@ public class LoggerTest {
LogService l = new LogService(log.getBundleContext());
l.register();
- ScrConfiguration config = mock(ScrConfiguration.class);
- when(config.isLogExtension()).thenReturn(false);
+ LogConfiguration config = mock(LogConfiguration.class);
+
+ when(config.isLogEnabled()).thenReturn(true);
+ when(config.isLogExtensionEnabled()).thenReturn(false);
when(config.getLogLevel()).thenReturn(Level.DEBUG);
- ScrLogger logger = ScrLogManager.scr(scr.getBundleContext(), config);
+ ScrLogger logger = ScrLoggerFactory.create(scr.getBundleContext(), config);
BundleLogger bundle = logger.bundle(component);
bundle.log(Level.ERROR, "Ext", null);
assertThat(l.entries).hasSize(1);
@@ -215,8 +220,10 @@ public class LoggerTest {
@Test
public void testExtensionLogLevelNotLoggingWhenRootSetToInfoAndLevelIsDebug() {
- ScrConfiguration config = mock(ScrConfiguration.class);
- when(config.isLogExtension()).thenReturn(true);
+ LogConfiguration config = mock(LogConfiguration.class);
+
+ when(config.isLogEnabled()).thenReturn(true);
+ when(config.isLogExtensionEnabled()).thenReturn(true);
when(config.getLogLevel()).thenReturn(Level.DEBUG);
LogService l = new LogService(log.getBundleContext());
@@ -224,7 +231,7 @@ public class LoggerTest {
l.levels.put(Logger.ROOT_LOGGER_NAME, LogLevel.INFO);
l.defaultLogLevel = LogLevel.TRACE;
- ScrLogger lscr = ScrLogManager.scr(scr.getBundleContext(), config);
+ ScrLogger lscr = ScrLoggerFactory.create(scr.getBundleContext(), config);
assertThat(lscr.isLogEnabled(Level.DEBUG)).isFalse();
assertThat(lscr.isLogEnabled(Level.INFO)).isTrue();
@@ -236,8 +243,10 @@ public class LoggerTest {
@Test
public void testExtensionLogLevelNotLoggingWhenPartialNameSetToInfoAndLevelIsDebug() {
- ScrConfiguration config = mock(ScrConfiguration.class);
- when(config.isLogExtension()).thenReturn(true);
+ LogConfiguration config = mock(LogConfiguration.class);
+
+ when(config.isLogEnabled()).thenReturn(true);
+ when(config.isLogExtensionEnabled()).thenReturn(true);
when(config.getLogLevel()).thenReturn(Level.DEBUG);
LogService l = new LogService(log.getBundleContext());
@@ -245,7 +254,7 @@ public class LoggerTest {
l.register();
l.levels.put("org.apache.felix.scr", LogLevel.INFO);
- ScrLogger lscr = ScrLogManager.scr(scr.getBundleContext(), config);
+ ScrLogger lscr = ScrLoggerFactory.create(scr.getBundleContext(), config);
assertThat(lscr.isLogEnabled(Level.DEBUG)).isFalse();
assertThat(lscr.isLogEnabled(Level.INFO)).isTrue();
@@ -257,8 +266,9 @@ public class LoggerTest {
@Test
public void testExtensionLogManager() {
- ScrConfiguration config = mock(ScrConfiguration.class);
- when(config.isLogExtension()).thenReturn(true);
+ LogConfiguration config = mock(LogConfiguration.class);
+
+ when(config.isLogExtensionEnabled()).thenReturn(true);
when(config.getLogLevel()).thenReturn(Level.DEBUG);
LogService l = new LogService(log.getBundleContext());
@@ -285,7 +295,7 @@ public class LoggerTest {
assertThat(l.entries).hasSize(1);
LogEntry le = l.entries.get(0);
assertThat(le.format).isEqualTo("Bundle");
- assertThat(le.bundle).isEqualTo(component);
+ assertThat(le.bundle).isEqualTo(scr);
assertThat(le.loggername).isEqualTo(ExtLogManager.SCR_LOGGER_PREFIX + "component");
}
@@ -296,7 +306,7 @@ public class LoggerTest {
assertThat(l.entries).hasSize(1);
LogEntry le = l.entries.get(0);
assertThat(le.format).isEqualTo("[name] Component");
- assertThat(le.bundle).isEqualTo(component);
+ assertThat(le.bundle).isEqualTo(scr);
assertThat(le.loggername).isEqualTo(ExtLogManager.SCR_LOGGER_PREFIX + "component.name");
l.entries.clear();
@@ -310,11 +320,33 @@ public class LoggerTest {
lm.scr().close();
}
}
+
+ @Test
+ public void testDisabledLogging() {
+ LogConfiguration config = mock(LogConfiguration.class);
+
+ when(config.isLogEnabled()).thenReturn(false);
+
+ LogService l = new LogService(log.getBundleContext());
+ l.register();
+ l.levels.put(Logger.ROOT_LOGGER_NAME, LogLevel.ERROR);
+ l.defaultLogLevel = LogLevel.ERROR;
+
+ ScrLogger lscr = ScrLoggerFactory.create(scr.getBundleContext(), config);
+
+ assertThat(lscr.isLogEnabled(Level.DEBUG)).isFalse();
+ assertThat(lscr.isLogEnabled(Level.INFO)).isFalse();
+
+ lscr.log(Level.DEBUG, "I should not be reported", null);
+
+ assertThat(l.entries).isEmpty();
+ }
@Test
public void testBackwardCompatibilityOutput() {
- ScrConfiguration config = mock(ScrConfiguration.class);
- when(config.isLogExtension()).thenReturn(false);
+ LogConfiguration config = mock(LogConfiguration.class);
+
+ when(config.isLogExtensionEnabled()).thenReturn(false);
when(config.getLogLevel()).thenReturn(Level.DEBUG);
LogService l = new LogService(log.getBundleContext());
@@ -365,9 +397,11 @@ public class LoggerTest {
@Test
public void testLifeCycle() {
-
- LogManager lm = new LogManager(scr.getBundleContext());
+ LogConfiguration config = mock(LogConfiguration.class);
+
+ LogManager lm = new LogManager(scr.getBundleContext(), config);
lm.open();
+
LoggerFacade facade = lm.getLogger(scr, "lifecycle", LoggerFacade.class);
assertThat(facade.logger).isNull();
@@ -401,9 +435,11 @@ public class LoggerTest {
@Test
public void testPrioritiesLogService() {
-
- LogManager lm = new LogManager(scr.getBundleContext());
+ LogConfiguration config = mock(LogConfiguration.class);
+
+ LogManager lm = new LogManager(scr.getBundleContext(), config);
lm.open();
+
LoggerFacade facade = lm.getLogger(scr, "lifecycle", LoggerFacade.class);
assertThat(facade.logger).isNull();
@@ -429,8 +465,7 @@ public class LoggerTest {
@Test
public void testLifeCycleOfComponentBundle() throws BundleException, InterruptedException {
-
- ScrConfiguration config = mock(ScrConfiguration.class);
+ LogConfiguration config = mock(LogConfiguration.class);
LogService l = new LogService(log.getBundleContext());
l.register();
@@ -442,22 +477,21 @@ public class LoggerTest {
assertThat(lm.lock.domains).hasSize(1);
component.stop();
+ lm.close();
for (int i = 0; i < 100; i++) {
if (lm.lock.domains.isEmpty())
return;
Thread.sleep(10);
}
- lm.close();
fail("domains not cleared within 1 sec");
}
@Test
public void testLogLevels() {
-
- ScrConfiguration config = mock(ScrConfiguration.class);
+ LogConfiguration config = mock(LogConfiguration.class);
when(config.getLogLevel()).thenReturn(Level.DEBUG);
- when(config.isLogExtension()).thenReturn(false);
+ when(config.isLogExtensionEnabled()).thenReturn(false);
LogService l = new LogService(log.getBundleContext());
@@ -465,6 +499,7 @@ public class LoggerTest {
ScrLogManager lm = new ScrLogManager(scr.getBundleContext(), config);
lm.open();
+
ScrLogger facade = lm.scr();
assert LogLevel.values().length == Level.values().length;