use of org.apache.sling.commons.log.logback.internal.config.ConfigurationException 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.apache.sling.commons.log.logback.internal.config.ConfigurationException in project sling by apache.
the class LogConfigManager method updateLoggerConfiguration.
/**
* Updates or removes the logger configuration indicated by the given
* <code>pid</code>. If the case of modified categories or removal of the
* logger configuration, existing loggers will be modified to reflect the
* correct logger configurations available.
* <p>
* The configuration object is expected to contain the following properties:
* <dl>
* <dt>{@link LogConfigManager#LOG_PATTERN}</dt>
* <dd>The <code>MessageFormat</code> pattern to apply to format the log
* message before writing it to the log writer. If this property is missing
* or the empty string the default pattern
* {@link LogConfigManager#LOG_PATTERN_DEFAULT} is used.</dd>
* <dt>{@link LogConfigManager#LOG_LEVEL}</dt>
* <dd>The log level to use for log message limitation. The supported values
* are <code>off</code>, <code>trace</code>, <code>debug</code>, <code>info</code>,
* <code>warn</code> and <code>error</code>. Case does not matter. If this
* property is missing a <code>ConfigurationException</code> is thrown and
* this logger configuration is not used.</dd>
* <dt>{@link LogConfigManager#LOG_LOGGERS}</dt>
* <dd>The logger names to which this configuration applies. As logger names
* form a hierarchy like Java packages, the listed names also apply to
* "child names" unless more specific configuration applies for such
* children. This property may be a single string, an array of strings or a
* collection of strings. Each string may itself be a comma-separated list
* of logger names. If this property is missing a
* <code>ConfigurationException</code> is thrown.</dd>
* <dt>{@link LogConfigManager#LOG_FILE}</dt>
* <dd>The name of the log writer to use. This may be the name of a log file
* configured for any log writer or it may be the configuration PID of such
* a writer. If this property is missing or empty or does not refer to an
* existing log writer configuration, the default log writer is used.</dd>
*
* @param pid The name of the configuration to update or remove.
* @param configuration The configuration object.
* @throws ConfigurationException If the log level and logger names
* properties are not configured for the given configuration.
*/
public void updateLoggerConfiguration(final String pid, final Dictionary<?, ?> configuration, final boolean performRefresh) throws ConfigurationException {
if (configuration != null) {
String pattern = (String) configuration.get(LogConfigManager.LOG_PATTERN);
final String level = (String) configuration.get(LogConfigManager.LOG_LEVEL);
String fileName = (String) configuration.get(LogConfigManager.LOG_FILE);
final Set<String> categories = toCategoryList(configuration.get(LogConfigManager.LOG_LOGGERS));
final boolean additiv;
if (configuration.get(LogConfigManager.LOG_ADDITIV) != null) {
additiv = Boolean.valueOf(configuration.get(LogConfigManager.LOG_ADDITIV).toString());
} else {
// If an appender is explicitly defined then set additive to false
// to be compatible with earlier Sling Logging behavior
additiv = false;
}
// verify categories
if (categories == null) {
throw new ConfigurationException(LogConfigManager.LOG_LOGGERS, "Missing categories in configuration " + pid);
}
// verify no other configuration has any of the categories
for (final String cat : categories) {
final LogConfig cfg = configByCategory.get(cat);
if (cfg != null && !pid.equals(cfg.getConfigPid())) {
throw new ConfigurationException(LogConfigManager.LOG_LOGGERS, "Category " + cat + " already defined by configuration " + cfg.getConfigPid());
}
}
// verify log level
if (level == null) {
throw new ConfigurationException(LogConfigManager.LOG_LEVEL, "Value required");
}
final Level logLevel;
final boolean resetToDefault;
if (LOG_LEVEL_RESET_TO_DEFAULT.equalsIgnoreCase(level)) {
resetToDefault = true;
logLevel = null;
} else {
logLevel = Level.toLevel(level);
resetToDefault = false;
}
// verify pattern
if (pattern == null || pattern.length() == 0) {
pattern = LogConfigManager.LOG_PATTERN_DEFAULT;
}
// LogLevel
if (fileName != null && !isConsole(fileName)) {
fileName = getAbsoluteFilePath(fileName);
}
// create or modify existing configuration object
final LogConfig newConfig = new LogConfig(this, pattern, categories, logLevel, fileName, additiv, pid, loggerContext, resetToDefault);
LogConfig oldConfig = configByPid.get(pid);
if (oldConfig != null) {
configByCategory.keySet().removeAll(oldConfig.getCategories());
}
// relink categories
for (String cat : categories) {
configByCategory.put(cat, newConfig);
}
configByPid.put(pid, newConfig);
} else {
// configuration deleted if null
// remove configuration from pid list
LogConfig config = configByPid.remove(pid);
if (config != null) {
// remove all configured categories
configByCategory.keySet().removeAll(config.getCategories());
}
}
if (performRefresh) {
logbackManager.configChanged();
}
}
Aggregations