Search in sources :

Example 1 with LogConfigUpdate

use of com.aws.greengrass.logging.impl.config.model.LogConfigUpdate in project aws-greengrass-nucleus by aws-greengrass.

the class LogManagerHelperTest method GIVEN_mock_service_logger_WHEN_reconfigure_multiple_configs_THEN_change_applied_correctly.

@Test
void GIVEN_mock_service_logger_WHEN_reconfigure_multiple_configs_THEN_change_applied_correctly() throws IOException, InterruptedException {
    Path tempRootDir2 = tempRootDir.resolve("test_logs_" + Utils.generateRandomString(8));
    Path tempRootDir3 = tempRootDir.resolve("test_logs_" + Utils.generateRandomString(8));
    String mockServiceName = "MockService002";
    when(mockGreengrassService.getServiceName()).thenReturn(mockServiceName);
    LogConfig.getRootLogConfig().setStore(LogStore.FILE);
    Logger componentLogger = LogManagerHelper.getComponentLogger(mockGreengrassService);
    Logger greengrassLogger = LogManager.getLogger("test");
    LogConfig testLogConfig = LogManager.getLogConfigurations().get(mockServiceName);
    // change format and log directory
    LogConfigUpdate newConfig = LogConfigUpdate.builder().format(LogFormat.JSON).outputDirectory(tempRootDir2.toAbsolutePath().toString()).build();
    LogManager.reconfigureAllLoggers(newConfig);
    logRandomMessages(componentLogger, 1025, LogFormat.JSON);
    logRandomMessages(greengrassLogger, 1025, LogFormat.JSON);
    // should output to new directory and still preserve log file size config
    assertEquals(tempRootDir2.toAbsolutePath(), testLogConfig.getStoreDirectory().toAbsolutePath());
    assertEquals(tempRootDir2.toAbsolutePath(), LogManager.getRootLogConfiguration().getStoreDirectory().toAbsolutePath());
    assertEquals(LogFormat.JSON, testLogConfig.getFormat());
    assertEquals(LogFormat.JSON, LogManager.getRootLogConfiguration().getFormat());
    assertEquals(LogFormat.JSON, LogManager.getTelemetryConfig().getFormat());
    // check log format is actually JSON
    File logFile = new File(testLogConfig.getStoreName());
    assertThat(logFile, aFileNamed(equalToIgnoringCase(mockServiceName + ".log")));
    List<String> lines = Files.readAllLines(logFile.toPath());
    ObjectMapper objectMapper = new ObjectMapper();
    assertDoesNotThrow(() -> {
        objectMapper.readValue(lines.get(0), Map.class);
    });
    // change file size, total size, also change to another directory so it's clean
    newConfig = LogConfigUpdate.builder().fileSizeKB(1L).totalLogsSizeKB(1L).format(LogFormat.TEXT).outputDirectory(tempRootDir3.toAbsolutePath().toString()).build();
    LogManager.reconfigureAllLoggers(newConfig);
    logRandomMessages(componentLogger, 4000, LogFormat.TEXT);
    componentLogger.atInfo().log();
    // older rotated file should be deleted. Log file count should not change
    Thread.sleep(850);
    assertEquals(1, getLogFileCount(testLogConfig, mockServiceName));
}
Also used : Path(java.nio.file.Path) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Logger(com.aws.greengrass.logging.api.Logger) LogConfigUpdate(com.aws.greengrass.logging.impl.config.model.LogConfigUpdate) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) LogConfig(com.aws.greengrass.logging.impl.config.LogConfig) Test(org.junit.jupiter.api.Test)

Example 2 with LogConfigUpdate

use of com.aws.greengrass.logging.impl.config.model.LogConfigUpdate in project aws-greengrass-nucleus by aws-greengrass.

the class LogManagerHelperTest method GIVEN_mock_service_logger_WHEN_file_size_limit_reached_THEN_rollover.

@Test
void GIVEN_mock_service_logger_WHEN_file_size_limit_reached_THEN_rollover() throws InterruptedException {
    String mockServiceName = "MockService001";
    when(mockGreengrassService.getServiceName()).thenReturn(mockServiceName);
    LogConfig.getRootLogConfig().setStore(LogStore.FILE);
    Logger componentLogger = LogManagerHelper.getComponentLogger(mockGreengrassService);
    Logger greengrassLogger = LogManager.getLogger("test");
    // change log file size
    LogConfigUpdate newConfig = LogConfigUpdate.builder().fileSizeKB(1L).build();
    LogManager.reconfigureAllLoggers(newConfig);
    // should apply change to all loggers
    LogConfig testLogConfig = LogManager.getLogConfigurations().get(mockServiceName);
    assertEquals(1, testLogConfig.getFileSizeKB());
    assertEquals(1, LogManager.getRootLogConfiguration().getFileSizeKB());
    assertEquals(1, LogManager.getTelemetryConfig().getFileSizeKB());
    // log less than 1KB, should not rotate
    logRandomMessages(componentLogger, 500, LogFormat.TEXT);
    logRandomMessages(greengrassLogger, 500, LogFormat.TEXT);
    assertEquals(1, getLogFileCount(testLogConfig, mockServiceName));
    assertEquals(1, getLogFileCount(testLogConfig, PersistenceConfig.DEFAULT_STORE_NAME));
    // Should rotate this time
    logRandomMessages(componentLogger, 525, LogFormat.TEXT);
    logRandomMessages(greengrassLogger, 525, LogFormat.TEXT);
    // Rollover is guarded by ch.qos.logback.core.util.DefaultInvocationGate so that it's not invoked too soon/often
    // This is the minimum delay since startup for it to allow log rollover.
    Thread.sleep(850);
    // log once more to trigger roll over
    componentLogger.atInfo().log();
    // log once more to trigger roll over
    greengrassLogger.atInfo().log();
    assertTrue(getLogFileCount(testLogConfig, mockServiceName) > 1);
    assertTrue(getLogFileCount(testLogConfig, PersistenceConfig.DEFAULT_STORE_NAME) > 1);
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Logger(com.aws.greengrass.logging.api.Logger) LogConfigUpdate(com.aws.greengrass.logging.impl.config.model.LogConfigUpdate) LogConfig(com.aws.greengrass.logging.impl.config.LogConfig) Test(org.junit.jupiter.api.Test)

Aggregations

Logger (com.aws.greengrass.logging.api.Logger)2 LogConfig (com.aws.greengrass.logging.impl.config.LogConfig)2 LogConfigUpdate (com.aws.greengrass.logging.impl.config.model.LogConfigUpdate)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Test (org.junit.jupiter.api.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 File (java.io.File)1 Path (java.nio.file.Path)1