Search in sources :

Example 1 with LoggingConfigurable

use of alma.Logging.LoggingConfigurable in project ACS by ACS-Community.

the class TestLogLevelsCompTest method testGetLevels.

/**
	 * @throws Exception
	 */
public void testGetLevels() throws Exception {
    int[] levels;
    for (TestLogLevelsComp testlogComp : components) {
        try {
            levels = testlogComp.getLevels();
        } catch (CouldntPerformActionEx ex) {
            throw AcsJCouldntPerformActionEx.fromCouldntPerformActionEx(ex);
        }
        String componentName = testlogComp.name();
        // Prepare the log levels via the container's LoggingConfigurable interface
        String containerName = containerTestUtil.resolveContainerName(componentName);
        LoggingConfigurable containerLogConfig = containerTestUtil.getContainerLoggingIF(containerName);
        // @TODO get logger name from the container via a new IDL method, because the logger name 
        //       may not be the same as the container (e.g. for C++ containers).
        //String loggerName = containerName;
        String loggerName = componentName;
        // Get the log level that our test component is subject to 
        alma.Logging.LoggingConfigurablePackage.LogLevels componentLogLevels = containerLogConfig.get_logLevels(loggerName);
        if (componentLogLevels.useDefault) {
            m_logger.info("levels from component's getLevels method (hardcoded remote, local, effective): " + levels[0] + ", " + levels[1] + ", " + levels[2] + ", " + levels[3] + ", " + levels[4]);
            Assert.assertEquals(levels[3], componentLogLevels.minLogLevel);
            Assert.assertEquals(levels[4], componentLogLevels.minLogLevelLocal);
        } else {
            m_logger.info("levels from component's getLevels method and LoggingConfigurable: " + levels[0] + ", " + levels[1] + ", " + levels[2] + ", " + levels[3] + ", " + levels[4]);
        }
        // levels[0-4]: hardcoded remote, hardcoded local, AcsLogger, AcsLogHandler, StdoutConsoleHandler
        Assert.assertTrue(levels[3] != -1);
        Assert.assertTrue(levels[4] != -1);
        // The AcsLogger setting should be the minimum of the one for AcsLogHandler and StdoutConsoleHandler
        int minLevel = levels[3];
        if (levels[3] > levels[4] || levels[3] == -1)
            minLevel = levels[4];
        Assert.assertEquals(levels[2], minLevel);
    }
//		// Sleep is to work-around a race condition, whereby the tearDown() can get called "too early"
//		// (logging not properly initialised when it is already  stopped after running this short method).
//		Thread.sleep(3000);
}
Also used : AcsJCouldntPerformActionEx(alma.ACSErrTypeCommon.wrappers.AcsJCouldntPerformActionEx) CouldntPerformActionEx(alma.ACSErrTypeCommon.CouldntPerformActionEx) TestLogLevelsComp(alma.contLogTest.TestLogLevelsComp) LoggingConfigurable(alma.Logging.LoggingConfigurable)

Example 2 with LoggingConfigurable

use of alma.Logging.LoggingConfigurable in project ACS by ACS-Community.

the class TestLogLevelsCompTest method testLoggingConfigurableCentralLogLevels.

/**
	 * This test uses the LoggingConfigurable interface implemented by all containers to set log levels
	 * These log levels are then expected to be independent of the initial CDB settings, according to precedence rules.
	 * The test verifies that the levels are applied correctly by commanding the test components to log messages
	 * at various log levels. The test listens on the Log service to compare the messages actually sent
	 * with what is expected for the current log level settings.
	 */
public void testLoggingConfigurableCentralLogLevels() throws Exception {
    LogSeriesExpectant expectant = new LogSeriesExpectant(getLogReceiver());
    // All log levels, including OFF
    int[] centralLogLevels;
    // Excluding OFF
    int[] sendLogLevels;
    // Only with OFF
    int[] offLogLevels;
    centralLogLevels = new int[AcsLogLevelDefinition.values().length];
    sendLogLevels = new int[AcsLogLevelDefinition.values().length - 1];
    offLogLevels = new int[1];
    int i = 0;
    int maxLogLevel = -1;
    // log levels ordered, with OFF being the last one.
    for (AcsLogLevelDefinition lld : AcsLogLevelDefinition.values()) {
        if (lld.name.equalsIgnoreCase("OFF")) {
            offLogLevels[0] = lld.value;
        } else {
            maxLogLevel = Math.max(maxLogLevel, lld.value);
            sendLogLevels[i] = lld.value;
        }
        centralLogLevels[i++] = lld.value;
    }
    int waitTimeSeconds = 4;
    for (TestLogLevelsComp testComp : components) {
        String componentName = testComp.name();
        // Prepare the log levels via the container's LoggingConfigurable interface
        String containerName = containerTestUtil.resolveContainerName(componentName);
        LoggingConfigurable containerLogConfig = containerTestUtil.getContainerLoggingIF(containerName);
        // @TODO get logger name from the component via a new IDL method, because the logger name 
        //       may not be the same as the component name (e.g. for C++ components).
        String loggerName = componentName;
        // Set the log level that our test component is subject to (either default level or individual level depending on CDB config)
        alma.Logging.LoggingConfigurablePackage.LogLevels componentLogLevels = containerLogConfig.get_logLevels(loggerName);
        for (int level : centralLogLevels) {
            // The min log level we set for the component and test its log output against.
            short minLogLevelCentral = (short) level;
            componentLogLevels.minLogLevel = minLogLevelCentral;
            if (componentLogLevels.useDefault) {
                // @TODO: add a test as well to change the specific level, and then verify
                // if useDefault becomes false.
                containerLogConfig.set_default_logLevels(componentLogLevels);
                m_logger.info("Set default log level for container " + containerName + " to " + minLogLevelCentral + " to be used by component " + componentName);
            } else {
                // @TODO: add a test to change the default level, and see if it impacts specific logger
                containerLogConfig.set_logLevels(loggerName, componentLogLevels);
                m_logger.info("Set individual log level for component " + componentName + " (running in container " + containerName + ") to " + minLogLevelCentral);
            }
            expectant.clearLogs();
            m_logger.info("Will call 'logDummyMessages' on component " + componentName);
            testComp.logDummyMessages(sendLogLevels);
            //testComp.logDummyMessages(centralLogLevels);
            m_logger.info("Will wait " + waitTimeSeconds + " seconds to receive (some of) these log messages.");
            LogSeriesExpectant.LogList logRecordsReceived = expectant.awaitLogRecords(loggerName, waitTimeSeconds);
            System.out.println("Got " + logRecordsReceived.size() + " records from logger " + loggerName);
            for (LogReceiver.ReceivedLogRecord logRecord : logRecordsReceived) {
                System.out.println("(level=" + logRecord.getLevel().acsCoreLevel.value + ") " + logRecord.getMessage());
            }
            if (minLogLevelCentral > maxLogLevel) {
                // minLogLevelCentral must be set to OFF - no logs should get through
                assertEquals(0, logRecordsReceived.size());
            } else {
                // The next assertion will fail here if there were no logs received,
                // as getMinLogLevel will return then the value 2147483647 (-1).
                assertEquals(minLogLevelCentral, logRecordsReceived.getMinLogLevel());
                assertEquals(maxLogLevel, logRecordsReceived.getMaxLogLevel());
            }
            // logging a msg at level OFF should lead to an exception, independent from central level 
            try {
                testComp.logDummyMessages(offLogLevels);
                // C++ containers don't seem to be able to throw exception in this case 
                m_logger.info("Sending a log with level OFF did not throw exception.");
                // Consume the log that says it is the last log message, unless the current level is OFF
                if (minLogLevelCentral <= maxLogLevel) {
                    logRecordsReceived = expectant.awaitLogRecords(loggerName, waitTimeSeconds);
                    assertEquals(1, logRecordsReceived.size());
                }
            } catch (org.omg.CORBA.UNKNOWN ex) {
                if (ex.getLocalizedMessage().startsWith("Server-side Exception: java.lang.IllegalArgumentException")) {
                    // Obviously the case for Java
                    System.out.println("Got illegal argument exception for attempting to send log with level OFF, as expected.");
                } else if (ex.getLocalizedMessage().startsWith("Server-side Exception: null")) {
                    // This is what Python does
                    System.out.println("Got exception for attempting to send log with level OFF, as expected.");
                } else {
                    // The exception we got is not the one expected
                    throw ex;
                }
            }
        //System.out.println("Finished testing level " + level);
        }
    //System.out.println("Finished testing component " + componentName);
    }
//System.out.println("Finished testing all components - done with testLoggingConfigurableCentralLogLevels()");
}
Also used : TestLogLevelsComp(alma.contLogTest.TestLogLevelsComp) LoggingConfigurable(alma.Logging.LoggingConfigurable) AcsLogLevelDefinition(alma.acs.logging.level.AcsLogLevelDefinition) LogReceiver(alma.acs.logging.engine.LogReceiver)

Aggregations

LoggingConfigurable (alma.Logging.LoggingConfigurable)2 TestLogLevelsComp (alma.contLogTest.TestLogLevelsComp)2 CouldntPerformActionEx (alma.ACSErrTypeCommon.CouldntPerformActionEx)1 AcsJCouldntPerformActionEx (alma.ACSErrTypeCommon.wrappers.AcsJCouldntPerformActionEx)1 LogReceiver (alma.acs.logging.engine.LogReceiver)1 AcsLogLevelDefinition (alma.acs.logging.level.AcsLogLevelDefinition)1