Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FELIX-6407] SCR Logging Fixes and Improvements #75

Merged
merged 1 commit into from May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions scr/src/main/java/org/apache/felix/scr/impl/Activator.java
Expand Up @@ -43,7 +43,7 @@
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;
Expand Down Expand Up @@ -657,10 +657,10 @@ public void setLogger()
// 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);
}

}
Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -179,7 +182,8 @@ void configure( Dictionary<String, ?> config, boolean fromConfig )
serviceChangecountTimeout = DEFAULT_SERVICE_CHANGECOUNT_TIMEOUT_MILLISECONDS;
newGlobalExtender = false;
cacheMetadata = false;
logExtension = false;
isLogEnabled = true;
isLogExtensionEnabled = false;
}
else
{
Expand All @@ -192,7 +196,8 @@ void configure( Dictionary<String, ?> config, boolean fromConfig )
serviceChangecountTimeout = getServiceChangecountTimeout();
newGlobalExtender = getDefaultGlobalExtender();
cacheMetadata = getDefaultCacheMetadata();
logExtension = getDefaultLogExtension();
isLogEnabled = getDefaultLogEnabled();
isLogExtensionEnabled = getDefaultLogExtension();
}
}
else
Expand All @@ -213,7 +218,8 @@ void configure( Dictionary<String, ?> config, boolean fromConfig )
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 )
{
Expand Down Expand Up @@ -254,7 +260,6 @@ public boolean keepInstances()
return keepInstances;
}

@SuppressWarnings("deprecation")
@Override
public boolean infoAsService()
{
Expand Down Expand Up @@ -409,13 +414,38 @@ else if ( LOG_LEVEL_ERROR.equalsIgnoreCase( levelString ) )
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());
}


}
Expand Up @@ -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;

Expand All @@ -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();
Expand All @@ -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);
}

Expand All @@ -72,7 +74,10 @@ public ComponentLogger component(Bundle bundle, String implementationClass,

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;
}
Expand Down
@@ -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();
}
32 changes: 30 additions & 2 deletions scr/src/main/java/org/apache/felix/scr/impl/logger/LogManager.java
Expand Up @@ -143,12 +143,40 @@ synchronized void close()
}

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
Expand Down
71 changes: 71 additions & 0 deletions 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()
{

}

}