diff --git a/src/ulib/log/level.h b/src/ulib/log/level.h index 6c9ae0e..b35f7f6 100644 --- a/src/ulib/log/level.h +++ b/src/ulib/log/level.h @@ -42,6 +42,28 @@ public: FATAL = ULOG_LEVEL_FATAL }; + static const char *GetConsoleColorPrefix(int level) + { + switch (level) { + case kTrace: + return "\033[0;37m"; + case kDebug: + return "\033[0;36m"; + case kInfo: + return "\033[0;32m"; + case kWarn: + return "\033[0;33m"; + case kError: + return "\033[0;31m"; + case kFatal: + return "\033[0;31m"; + default: + return "\033[0m"; + } + } + + static const char *GetConsoleColorSuffix(int level) { return "\033[0m"; } + static const char *ToShortString(int level) { switch (level) { diff --git a/src/ulib/log/logger.cpp b/src/ulib/log/logger.cpp index d87aa2f..85b923d 100644 --- a/src/ulib/log/logger.cpp +++ b/src/ulib/log/logger.cpp @@ -62,7 +62,8 @@ Logger::Log(int32_t level, /** * @brief time level_name tag file:line@func msg */ - std::string pattern = "{} {} {} {}:{}@{}: {}"; + std::string pattern = std::string() + Level::GetConsoleColorPrefix(level) + + "{} {} {} {}:{}@{}: {}" + Level::GetConsoleColorSuffix(level); std::string log_time; { std::time_t now = time(NULL);