feat(logger): add log level filtering

This commit is contained in:
user
2026-07-21 13:44:54 +04:00
parent 4366eeb92e
commit ee5fec311c
4 changed files with 83 additions and 2 deletions
+60
View File
@@ -109,6 +109,64 @@ void testMultipleWrites() {
assert(secondLine.find("second message") != std::string::npos);
}
void testIgnoreLowerLogLevel() {
helpers::ScopedFile file("filter.log");
file.throwIfExists();
Logger logger(file.getFileName(), LogLevel::info());
// INFO соответствует минимальному уровню логирования,
// поэтому сообщение должно быть записано в журнал
auto infoResult = logger.writeMessage(LogLevel::info(), "info message");
assert(infoResult.isSuccess());
// DEBUG имеет более низкий приоритет, чем установленный уровень INFO.
// Сообщение не должно попасть в журнал
auto debugResult = logger.writeMessage(LogLevel::debug(), "debug message");
assert(debugResult.isSuccess());
std::ifstream input(file.getFileName());
assert(input.is_open());
std::string firstLine;
std::string secondLine;
std::getline(input, firstLine);
assert(firstLine.find("info message") != std::string::npos);
// Проверяем, что после INFO-сообщения отсутствует DEBUG-сообщение.
bool hasSecondLine = static_cast<bool>(std::getline(input, secondLine));
assert(!hasSecondLine);
}
void testWriteMessageWithDefaultLevel() {
helpers::ScopedFile file("default_level.log");
file.throwIfExists();
Logger logger(file.getFileName(), LogLevel::error());
auto result = logger.writeMessage("critical message");
assert(result.isSuccess());
std::ifstream input(file.getFileName());
assert(input.is_open());
std::string content;
std::getline(input, content);
assert(content.find("critical message") != std::string::npos);
}
void runTests() {
testLoggerCreation();
testWithLogLevel();
@@ -116,6 +174,8 @@ void runTests() {
testWriteLogSuccess();
testWriteLogFailure();
testMultipleWrites();
testIgnoreLowerLogLevel();
testWriteMessageWithDefaultLevel();
}
} // namespace logger::tests