import unittest import os import shutil from lib.core.logger.factory import new from lib.core.logger.model import LogItemArgs, LogType, Logger # Import Logger class from lib.data.ourtime.ourtime import new as ourtime_new, now as ourtime_now from lib.core.pathlib.pathlib import get_file, ls, rmdir_all class TestLogger(unittest.TestCase): def setUp(self): # Corresponds to testsuite_begin() if os.path.exists('/tmp/testlogs'): rmdir_all('/tmp/testlogs') def tearDown(self): # Corresponds to testsuite_end() # if os.path.exists('/tmp/testlogs'): # rmdir_all('/tmp/testlogs') pass def test_logger_functionality(self): logger = new('/tmp/testlogs') # Test stdout logging logger.log(LogItemArgs( cat='test-app', log='This is a test message\nWith a second line\nAnd a third line', logtype=LogType.STDOUT, timestamp=ourtime_new('2022-12-05 20:14:35') )) # Test error logging logger.log(LogItemArgs( cat='error-test', log='This is an error\nWith details', logtype=LogType.ERROR, timestamp=ourtime_new('2022-12-05 20:14:35') )) logger.log(LogItemArgs( cat='test-app', log='This is a test message\nWith a second line\nAnd a third line', logtype=LogType.STDOUT, timestamp=ourtime_new('2022-12-05 20:14:36') )) logger.log(LogItemArgs( cat='error-test', log=''' This is an error With details ''', logtype=LogType.ERROR, timestamp=ourtime_new('2022-12-05 20:14:36') )) logger.log(LogItemArgs( cat='error-test', log=''' aaa bbb ''', logtype=LogType.ERROR, timestamp=ourtime_new('2022-12-05 22:14:36') )) logger.log(LogItemArgs( cat='error-test', log=''' aaa2 bbb2 ''', logtype=LogType.ERROR, timestamp=ourtime_new('2022-12-05 22:14:36') )) # Verify log directory exists self.assertTrue(os.path.exists('/tmp/testlogs'), 'Log directory should exist') # Get log file files = ls('/tmp/testlogs') self.assertEqual(len(files), 2) # Expecting two files: 2022-12-05-20.log and 2022-12-05-22.log # Test search functionality items_stdout = logger.search( timestamp_from=ourtime_new('2022-11-01 20:14:35'), timestamp_to=ourtime_new('2025-11-01 20:14:35'), logtype=LogType.STDOUT ) self.assertEqual(len(items_stdout), 2) items_error = logger.search( timestamp_from=ourtime_new('2022-11-01 20:14:35'), timestamp_to=ourtime_new('2025-11-01 20:14:35'), logtype=LogType.ERROR ) self.assertEqual(len(items_error), 4) # Test specific log content found_error_log = False for item in items_error: if "This is an error\nWith details" in item.log: found_error_log = True break self.assertTrue(found_error_log, "Expected error log content not found") found_stdout_log = False for item in items_stdout: if "This is a test message\nWith a second line\nAnd a third line" in item.log: found_stdout_log = True break self.assertTrue(found_stdout_log, "Expected stdout log content not found") # Test search by category items_test_app = logger.search( timestamp_from=ourtime_new('2022-11-01 20:14:35'), timestamp_to=ourtime_new('2025-11-01 20:14:35'), cat='test-app' ) self.assertEqual(len(items_test_app), 2) items_error_test = logger.search( timestamp_from=ourtime_new('2022-11-01 20:14:35'), timestamp_to=ourtime_new('2025-11-01 20:14:35'), cat='error-test' ) self.assertEqual(len(items_error_test), 4) # Test search by log content items_with_aaa = logger.search( timestamp_from=ourtime_new('2022-11-01 20:14:35'), timestamp_to=ourtime_new('2025-11-01 20:14:35'), log='aaa' ) self.assertEqual(len(items_with_aaa), 2) # Test search with timestamp range items_specific_time = logger.search( timestamp_from=ourtime_new('2022-12-05 22:00:00'), timestamp_to=ourtime_new('2022-12-05 23:00:00'), logtype=LogType.ERROR ) self.assertEqual(len(items_specific_time), 2) if __name__ == '__main__': unittest.main()