use of alma.maci.loggingconfig.NamedLogger in project ACS by ACS-Community.
the class LogConfig method initialize.
/**
* Initializes the values based on CDB settings, logging properties, etc. All subscribing classes are notified of
* the new configuration, see {@link LogConfigSubscriber#configureLogging(LogConfig)}.
* <p>
* This method can be called more than once: if some settings have changed, should be read in, and the logging
* classes should be notified of these changes. For example, the container could call this method when it gets
* notified that the logging configuration in the CDB has been changed at runtime.
*
* @param cdbBeatsProperties
* if true then the default logger level values from the CDB override the properties (env vars).
* <code>True</code> is foreseen for dynamic updates from the CDB, whereas for the initial
* configuration it should be a <code>false</code>.
*
* @throws LogConfigException
* if reading the configuration data failed and thus default values were used, or if there were problems
* during configuration even though some of the configuring went ok (best-effort approach).
*/
public void initialize(boolean cdbBeatsProperties) throws LogConfigException {
StringBuffer errMsg = new StringBuffer();
// schema binding class generated from LogggingConfig.xsd
LoggingConfig newLoggingConfig = null;
if (cdb != null) {
try {
if (cdbLoggingConfigPath != null) {
String loggingConfigXml = getLogConfigXml(cdbLoggingConfigPath, "//" + CDBNAME_LoggingConfig);
if (loggingConfigXml == null || loggingConfigXml.trim().isEmpty()) {
// the LoggingConfig child is mandatory for containers and manager
throw new LogConfigException("Node " + cdbLoggingConfigPath + " does not contain one LoggingConfig element.");
}
try {
newLoggingConfig = LoggingConfig.unmarshalLoggingConfig(new StringReader(loggingConfigXml));
} catch (Throwable thr) {
log(Level.FINE, "Failed to unmarshal logging config xml '" + loggingConfigXml + "'.", thr);
throw thr;
}
} else {
errMsg.append("CDB reference was set, but not the path to the logging configuration. ");
}
if (newLoggingConfig != null) {
loggingConfig = newLoggingConfig;
synchronized (namedLoggerConfigs) {
// but only null their configurations.
for (String loggerName : namedLoggerConfigs.keySet()) {
storeNamedLoggerConfig(loggerName, null);
}
// named logger levels from children of the <LoggingConfig/>
NamedLogger[] namedLoggers = loggingConfig.get();
for (int i = 0; i < namedLoggers.length; i++) {
storeNamedLoggerConfig(namedLoggers[i].getName(), new LockableUnnamedLogger(namedLoggers[i]));
}
// check CDB config for all component loggers who got a CDB path configured
for (String loggerName : cdbComponentPaths.keySet()) {
// skip named logger if it's been already configured from the main XML, since those values have precedence
if (!namedLoggerConfigs.containsKey(loggerName)) {
String cdbPath = cdbComponentPaths.get(loggerName);
String xpath = "//_[@Name='" + loggerName + "']/" + CDBNAME_ComponentLogger;
String componentConfigXML = getLogConfigXml(cdbPath, xpath);
// the ComponentLogger xml child element is optional, we get a null if it's missing.
if (componentConfigXML != null) {
UnnamedLogger compLoggerConfig;
try {
compLoggerConfig = UnnamedLogger.unmarshalUnnamedLogger(new StringReader(componentConfigXML));
} catch (Throwable thr) {
log(Level.FINE, "Failed to unmarshal component config xml '" + componentConfigXML + "'.", thr);
throw thr;
}
storeNamedLoggerConfig(loggerName, new LockableUnnamedLogger(compLoggerConfig));
}
}
}
}
} else {
throw new LogConfigException("LoggingConfig binding class obtained from CDB node '" + cdbLoggingConfigPath + "' was null.");
}
} catch (CDBXMLErrorEx ex) {
errMsg.append("Failed to read node " + cdbLoggingConfigPath + " from the CDB (msg='" + ex.errorTrace.shortDescription + "'). ");
} catch (CDBRecordDoesNotExistEx ex) {
errMsg.append("Node " + cdbLoggingConfigPath + " does not exist in the CDB (msg='" + ex.errorTrace.shortDescription + "'). ");
} catch (CastorException ex) {
errMsg.append("Failed to parse XML for CDB node " + cdbLoggingConfigPath + " into binding classes (ex=" + ex.getClass().getName() + ", msg='" + ex.getMessage() + "'). ");
} catch (Throwable thr) {
errMsg.append("Failed to read node " + cdbLoggingConfigPath + " from the CDB (ex=" + thr.getClass().getName() + ", msg='" + thr.getMessage() + "'). ");
}
}
// consider the env var based properties only if the CDB was not considered or if the CDB settings should not override the env vars
if (cdb == null || !cdbBeatsProperties) {
configureDefaultLevelsFromProperties();
configureNamedLoggerLevelsFromProperties();
}
notifySubscribers();
// present), we can publish a trace log
if (newLoggingConfig != null) {
StringWriter writer = new StringWriter();
String newXML = null;
try {
newLoggingConfig.marshal(writer);
newXML = writer.toString().trim();
} catch (Throwable thr) {
// nothing
;
}
String msg = "Updated logging configuration based on CDB entry " + newXML;
msg += " with " + (cdbBeatsProperties ? "CDB" : "env vars") + " having precedence over " + (cdbBeatsProperties ? "env vars" : "CDB");
log(Level.FINER, msg, null);
// @TODO: also log something for named component loggers if any were considered
} else {
log(Level.FINER, "Logging configuration has been initialized, but not from CDB settings.", null);
}
if (errMsg.length() > 0) {
throw new LogConfigException("Log config initialization at least partially failed. " + errMsg.toString());
}
}
Aggregations