use of alma.maci.loggingconfig.UnnamedLogger in project ACS by ACS-Community.
the class LogConfigTest method testDefaultValues.
/**
* Tests the config values returned from {@link LogConfig}
* with at most env vars for default levels being set (can be enforced by TAT!),
* but without CDB or other information being considered.
* <p>
* Also asserts that none of these calls return the original object,
* but instead a copy of it. This indirectly exercises the equals method.
*/
public void testDefaultValues() throws Exception {
logger.info("============ Running testDefaultValues ============");
assertEquals("Log", logConfig.getCentralizedLogger());
assertEquals(100, logConfig.getDispatchPacketSize());
LoggingConfig schemaDefaults = new LoggingConfig();
// We verify that the castor-generated class actually has the current schema defaults.
// These values must be adjusted when the schema is changed.
// In that case also the values in the simulated CDB's xml might have to be changed
// in order to still be different from the default values.
// 0 named loggers
assertEquals(0, schemaDefaults.getCount());
assertEquals(100, schemaDefaults.getDispatchPacketSize());
assertEquals(10, schemaDefaults.getFlushPeriodSeconds());
assertEquals(LogLevel.VALUE_10, schemaDefaults.getImmediateDispatchLevel());
assertEquals(1000, schemaDefaults.getMaxLogQueueSize());
assertEquals(LogLevel.VALUE_2, schemaDefaults.getMinLogLevelLocal());
assertEquals(LogLevel.VALUE_2, schemaDefaults.getMinLogLevel());
assertEquals("Log", schemaDefaults.getCentralizedLogger());
AcsLogLevelDefinition defaultMinLogLevelLocal = AcsLogLevelDefinition.fromXsdLogLevel(schemaDefaults.getMinLogLevelLocal());
// but if env vars are set, we may have different default levels
Integer PROP_ACS_LOG_STDOUT = Integer.getInteger(LogConfig.PROPERTYNAME_MIN_LOG_LEVEL_LOCAL);
if (PROP_ACS_LOG_STDOUT != null) {
defaultMinLogLevelLocal = AcsLogLevelDefinition.fromInteger(PROP_ACS_LOG_STDOUT.intValue());
logger.info("Using default stdout level from env var: " + defaultMinLogLevelLocal);
} else {
logger.info("No env var setting found for " + LogConfig.PROPERTYNAME_MIN_LOG_LEVEL_LOCAL);
}
AcsLogLevelDefinition defaultMinLogLevel = AcsLogLevelDefinition.fromXsdLogLevel(schemaDefaults.getMinLogLevel());
Integer PROP_ACS_LOG_REMOTE = Integer.getInteger(LogConfig.PROPERTYNAME_MIN_LOG_LEVEL);
if (PROP_ACS_LOG_REMOTE != null) {
defaultMinLogLevel = AcsLogLevelDefinition.fromInteger(PROP_ACS_LOG_REMOTE.intValue());
logger.info("Using default remote level from env var: " + defaultMinLogLevelLocal);
} else {
logger.info("No env var setting found for " + LogConfig.PROPERTYNAME_MIN_LOG_LEVEL);
}
// our logConfig should give the correct default values, coming from schema or env var
assertEquals(defaultMinLogLevelLocal, logConfig.getDefaultMinLogLevelLocal());
assertEquals(defaultMinLogLevel, logConfig.getDefaultMinLogLevel());
// Check default data other than log levels
assertEquals(schemaDefaults.getCentralizedLogger(), logConfig.getCentralizedLogger());
assertEquals(schemaDefaults.getImmediateDispatchLevel(), logConfig.getImmediateDispatchLevel().toXsdLevel());
assertEquals(schemaDefaults.getDispatchPacketSize(), logConfig.getDispatchPacketSize());
assertEquals(schemaDefaults.getFlushPeriodSeconds(), logConfig.getFlushPeriodSeconds());
// Get log levels for not existing named loggers, which should result in the default log levels being used
UnnamedLogger namedLogConfig1 = logConfig.getNamedLoggerConfig(null);
assertEquals(defaultMinLogLevel, AcsLogLevelDefinition.fromXsdLogLevel(namedLogConfig1.getMinLogLevel()));
assertEquals(defaultMinLogLevelLocal, AcsLogLevelDefinition.fromXsdLogLevel(namedLogConfig1.getMinLogLevelLocal()));
UnnamedLogger namedLogConfig2 = logConfig.getNamedLoggerConfig("nonExistingLogger");
assertNotSame(namedLogConfig1, namedLogConfig2);
assertEquals(defaultMinLogLevel, AcsLogLevelDefinition.fromXsdLogLevel(namedLogConfig2.getMinLogLevel()));
assertEquals(defaultMinLogLevelLocal, AcsLogLevelDefinition.fromXsdLogLevel(namedLogConfig2.getMinLogLevelLocal()));
}
use of alma.maci.loggingconfig.UnnamedLogger in project ACS by ACS-Community.
the class LogConfigTest method testCDBValues.
/**
* Tests logging config from the CDB, for both cases
* (a) that env vars beat CDB settings e.g. for normal CDB reading,
* (b) that CDB beats env vars e.g. during a refresh from CDB triggered via LoggingConfigurable API.
*/
public void testCDBValues() throws Exception {
logger.info("============ Running testCDBValues ============");
// we simulate an ACS_LOG_STDOUT env var setting
String ACS_LOG_STDOUT_ORIGINAL = System.getProperty(LogConfig.PROPERTYNAME_MIN_LOG_LEVEL_LOCAL);
String ACS_LOG_REMOTE_ORIGINAL = System.getProperty(LogConfig.PROPERTYNAME_MIN_LOG_LEVEL);
String ACS_LOG_STDOUT = "" + AcsLogLevelDefinition.EMERGENCY.value;
assertFalse("Fix this test to chose a different env var than the default", ACS_LOG_STDOUT.equals(ACS_LOG_STDOUT_ORIGINAL));
System.setProperty(LogConfig.PROPERTYNAME_MIN_LOG_LEVEL_LOCAL, ACS_LOG_STDOUT);
logger.info("Set property (env var) for local default level to " + ACS_LOG_STDOUT);
// and remove any possibly present property from env var ACS_LOG_CENTRAL
System.clearProperty(LogConfig.PROPERTYNAME_MIN_LOG_LEVEL);
logger.info("Removed property (env var) for remote default level");
// the schema defaults as reference
LoggingConfig schemaDefaults = new LoggingConfig();
AcsLogLevelDefinition defaultMinLogLevel = AcsLogLevelDefinition.fromXsdLogLevel(schemaDefaults.getMinLogLevel());
AcsLogLevelDefinition defaultMinLogLevelLocal = AcsLogLevelDefinition.fromXsdLogLevel(schemaDefaults.getMinLogLevelLocal());
// before we read the CDB, let's verify that the env var and default log levels are correct
logConfig.initialize(false);
assertEquals(defaultMinLogLevel, logConfig.getDefaultMinLogLevel());
assertEquals(AcsLogLevelDefinition.EMERGENCY, logConfig.getDefaultMinLogLevelLocal());
// the simulated test CDB to configure our loggers from
String cdbContainerPath = "MACI/Containers/frodoContainer";
String frodoContainerXml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> " + "<Container xmlns=\"urn:schemas-cosylab-com:Container:1.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:log=\"urn:schemas-cosylab-com:LoggingConfig:1.0\">" + "<LoggingConfig " + " minLogLevel=\"4\" " + " minLogLevelLocal=\"3\" " + " centralizedLogger=\"LogForFrodo\" " + " maxLogQueueSize=\"200\" " + " immediateDispatchLevel=\"8\" " + " dispatchPacketSize=\"33\" " + " >" + "<log:_ Name=\"MyMuteComponent\" minLogLevel=\"5\" minLogLevelLocal=\"6\" />" + "</LoggingConfig>" + "</Container>";
TestCDB testCDB = new TestCDB();
testCDB.addCurlToXmlMapping(cdbContainerPath, frodoContainerXml);
logConfig.setCDBLoggingConfigPath(cdbContainerPath);
logConfig.setCDB(testCDB);
// first the normal case where the env var default level beats the CDB default level
logConfig.initialize(false);
assertEquals("CDB must beat schema default", AcsLogLevelDefinition.INFO, logConfig.getDefaultMinLogLevel());
assertEquals("Env var must beat CDB", AcsLogLevelDefinition.EMERGENCY, logConfig.getDefaultMinLogLevelLocal());
assertEquals("LogForFrodo", logConfig.getCentralizedLogger());
assertSame(AcsLogLevelDefinition.ERROR, logConfig.getImmediateDispatchLevel());
assertEquals(33, logConfig.getDispatchPacketSize());
assertEquals(200, logConfig.getMaxLogQueueSize());
Set<String> loggerNames = logConfig.getLoggerNames();
assertEquals(1, loggerNames.size());
assertTrue(loggerNames.contains("MyMuteComponent"));
// was not in CDB, thus default should be used
assertEquals(schemaDefaults.getFlushPeriodSeconds(), logConfig.getFlushPeriodSeconds());
// next the special case of CDB refresh via dynamic API, where the CDB beats the env var default levels
logConfig.initialize(true);
assertSame("CDB must beat schema default", AcsLogLevelDefinition.INFO, logConfig.getDefaultMinLogLevel());
assertSame("CDB must beat env var", AcsLogLevelDefinition.DEBUG, logConfig.getDefaultMinLogLevelLocal());
assertEquals("LogForFrodo", logConfig.getCentralizedLogger());
assertSame(AcsLogLevelDefinition.ERROR, logConfig.getImmediateDispatchLevel());
assertEquals(33, logConfig.getDispatchPacketSize());
assertEquals(200, logConfig.getMaxLogQueueSize());
loggerNames = logConfig.getLoggerNames();
assertEquals(1, loggerNames.size());
assertTrue(loggerNames.contains("MyMuteComponent"));
// was not in CDB, thus default should be used
assertEquals(schemaDefaults.getFlushPeriodSeconds(), logConfig.getFlushPeriodSeconds());
UnnamedLogger myMuteloggerConfig = logConfig.getNamedLoggerConfig("MyMuteComponent");
assertEquals(LogLevel.VALUE_5, myMuteloggerConfig.getMinLogLevel());
assertEquals(LogLevel.VALUE_6, myMuteloggerConfig.getMinLogLevelLocal());
// Test logger configuration given in the CDB separately for a component in the Components.xml file, not with the rest of LoggingConfig in the Container xml.
String separateConfigComponent1 = "testComp1";
String separateConfigComponent2 = "testComp2";
String componentsXml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> " + "<Components xmlns=\"urn:schemas-cosylab-com:Components:1.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"> " + createComponentsCdbXml(separateConfigComponent1, "IDL_TYPE_1", "some.class1", "zampaione", true, 2, 8) + createComponentsCdbXml(separateConfigComponent2, "IDL_TYPE_2", "some.class2", "zampaione", true, 5, 6) + "</Components>";
logger.info("componentsXml = " + componentsXml);
String cdbComponentsPath = "MACI/Components";
testCDB.addCurlToXmlMapping(cdbComponentsPath, componentsXml);
logConfig.setCDBComponentPath(separateConfigComponent1, cdbComponentsPath);
logConfig.setCDBComponentPath(separateConfigComponent2, cdbComponentsPath);
logConfig.initialize(false);
loggerNames = logConfig.getLoggerNames();
// assertEquals(2, loggerNames.size());
assertTrue(loggerNames.contains("MyMuteComponent"));
// assertTrue(loggerNames.contains(separateConfigComponent1));
// assertTrue(loggerNames.contains(separateConfigComponent2));
UnnamedLogger separateConfig1 = logConfig.getNamedLoggerConfig(separateConfigComponent1);
assertEquals(LogLevel.VALUE_2, separateConfig1.getMinLogLevel());
assertEquals(LogLevel.VALUE_8, separateConfig1.getMinLogLevelLocal());
UnnamedLogger separateConfig2 = logConfig.getNamedLoggerConfig(separateConfigComponent2);
assertEquals(LogLevel.VALUE_5, separateConfig2.getMinLogLevel());
assertEquals(LogLevel.VALUE_6, separateConfig2.getMinLogLevelLocal());
// restore env vars (probably not necessary)
if (ACS_LOG_STDOUT_ORIGINAL != null) {
System.setProperty(LogConfig.PROPERTYNAME_MIN_LOG_LEVEL_LOCAL, ACS_LOG_STDOUT_ORIGINAL);
}
if (ACS_LOG_REMOTE_ORIGINAL != null) {
System.setProperty(LogConfig.PROPERTYNAME_MIN_LOG_LEVEL, ACS_LOG_REMOTE_ORIGINAL);
}
}
use of alma.maci.loggingconfig.UnnamedLogger in project ACS by ACS-Community.
the class HibernateWDALImpl method get_logLevels.
/**
* Gets log levels for a particular named logger. If the returned field
* LogLevels.useDefault is true, then the logger uses the default levels,
* see get_default_logLevels(); otherwise the returned local and remote
* levels apply.
* <p>
* For possible convenience, the default levels are returned in addition to
* setting {@link LogLevels#useDefault} to <code>true</code>.
*/
public LogLevels get_logLevels(String logger_name) throws LoggerDoesNotExistEx {
UnnamedLogger xsdLevels = logConfig.getNamedLoggerConfig(logger_name);
boolean useDefault = !logConfig.hasCustomConfig(logger_name);
LogLevels ret = AcsLogLevelDefinition.createIdlLogLevelsFromXsd(useDefault, xsdLevels);
return ret;
}
use of alma.maci.loggingconfig.UnnamedLogger in project ACS by ACS-Community.
the class AcsContainer method get_logLevels.
/**
* Gets log levels for a particular named logger. If the returned field
* LogLevels.useDefault is true, then the logger uses the default levels,
* see get_default_logLevels(); otherwise the returned local and remote
* levels apply.
* <p>
* For possible convenience, the default levels are returned in addition to
* setting {@link LogLevels#useDefault} to <code>true</code>.
*/
public LogLevels get_logLevels(String logger_name) throws LoggerDoesNotExistEx {
tryToWaitForContainerStart();
UnnamedLogger xsdLevels = logConfig.getNamedLoggerConfig(logger_name);
boolean useDefault = !logConfig.hasCustomConfig(logger_name);
LogLevels ret = AcsLogLevelDefinition.createIdlLogLevelsFromXsd(useDefault, xsdLevels);
return ret;
}
use of alma.maci.loggingconfig.UnnamedLogger 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