use of com.aws.greengrass.logging.impl.config.LogConfig in project aws-greengrass-nucleus by aws-greengrass.
the class LogManagerHelperTest method GIVEN_mock_service_WHEN_getComponentLogger_THEN_logs_to_correct_log_file.
@Test
void GIVEN_mock_service_WHEN_getComponentLogger_THEN_logs_to_correct_log_file() throws IOException {
when(mockGreengrassService.getServiceName()).thenReturn("MockService");
LogConfig.getRootLogConfig().setStore(LogStore.FILE);
Logger componentLogger = LogManagerHelper.getComponentLogger(mockGreengrassService);
componentLogger.atInfo().log("Something");
LogConfig logConfig = LogManager.getLogConfigurations().get("MockService");
File logFile = new File(logConfig.getStoreName());
assertThat(logFile, aFileNamed(equalToIgnoringCase("MockService.log")));
List<String> lines = Files.readAllLines(logFile.toPath());
assertThat(lines.get(0), containsString("Something"));
File ggLogFile = new File(LogManager.getRootLogConfiguration().getStoreName());
assertThat(ggLogFile, aFileNamed(equalToIgnoringCase("greengrass.log")));
assertEquals(0, ggLogFile.length());
}
use of com.aws.greengrass.logging.impl.config.LogConfig in project aws-greengrass-nucleus by aws-greengrass.
the class KernelLifecycle method shutdown.
/**
* Shutdown all services and the kernel with given timeout, but not exit the process.
*
* @param timeoutSeconds Timeout in seconds
*/
@SuppressWarnings("PMD.AvoidCatchingThrowable")
public void shutdown(int timeoutSeconds) {
if (!isShutdownInitiated.compareAndSet(false, true)) {
logger.info("Shutdown already initiated, returning...");
return;
}
try {
logger.atInfo().setEventType("system-shutdown").addKeyValue("main", getMain()).log();
softShutdown(timeoutSeconds);
// Do not wait for tasks in the executor to end.
ScheduledExecutorService scheduledExecutorService = kernel.getContext().get(ScheduledExecutorService.class);
ExecutorService executorService = kernel.getContext().get(ExecutorService.class);
kernel.getContext().runOnPublishQueueAndWait(() -> {
executorService.shutdownNow();
scheduledExecutorService.shutdownNow();
logger.atInfo().setEventType("executor-service-shutdown-initiated").log();
});
logger.atInfo().log("Waiting for executors to shutdown");
boolean executorTerminated = executorService.awaitTermination(timeoutSeconds, TimeUnit.SECONDS);
boolean scheduledExecutorTerminated = scheduledExecutorService.awaitTermination(timeoutSeconds, TimeUnit.SECONDS);
logger.atInfo("executor-service-shutdown-complete").kv("executor-terminated", executorTerminated).kv("scheduled-executor-terminated", scheduledExecutorTerminated).log();
// Stop the telemetry logger context after each test so we can delete the telemetry log files that are
// created during the test.
TelemetryConfig.getInstance().closeContext();
logger.atInfo("context-shutdown-initiated").log();
kernel.getContext().close();
logger.atInfo("context-shutdown-complete").log();
} catch (Throwable ex) {
logger.atError("system-shutdown-error", ex).log();
}
// Stop all the contexts for the loggers.
LogConfig.getRootLogConfig().closeContext();
for (LogConfig logConfig : LogManager.getLogConfigurations().values()) {
logConfig.closeContext();
}
}
use of com.aws.greengrass.logging.impl.config.LogConfig 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));
}
use of com.aws.greengrass.logging.impl.config.LogConfig 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);
}
use of com.aws.greengrass.logging.impl.config.LogConfig in project aws-greengrass-nucleus by aws-greengrass.
the class LogManagerHelperTest method cleanup.
@AfterEach
void cleanup() {
LogConfig.getRootLogConfig().reset();
LogConfig.getRootLogConfig().closeContext();
for (LogConfig logConfig : LogManager.getLogConfigurations().values()) {
logConfig.reset();
logConfig.closeContext();
}
LogManager.getTelemetryConfig().reset();
}
Aggregations