在很多的C++日志库中,日志信息会根据严重性来划分级别,使用者可以设置严重性级别门阀值来控制日志的输出,即严重性级别在该门阀值以上的日志信息才进行记录。以此不同,在Easylogging++日志库中,故意默认采用了不划分级别的日志记录,以便使用者 可以完全自主地启用或者禁止某个级别的日志记录。不过实际上,Easylogging++同样也支持划分级别的日志记录,只是需要额外设置一个标记:LoggingFlag::HierarchicalLogging。 下面的表格中列举了GitHub上给出的Easylogging++支持的日志级别(按日志级别由低到高排序):
Level | Description |
---|---|
Global | Generic level that represents all levels. Useful when setting global configuration for all levels. |
Trace | Information that can be useful to back-trace certain events - mostly useful than debug logs. |
Debug | Informational events most useful for developers to debug application. Only applicable if NDEBUG is not defined (for non-VC++) or _DEBUG is defined (for VC++). |
Fatal | Very severe error event that will presumably lead the application to abort. |
Error | Error information but will continue application to keep running. |
Warning | Information representing errors in application but application will keep running. |
Info | Mainly useful to represent current progress of application. |
Verbose | Information that can be highly useful and vary with verbose logging level. Verbose logging is not applicable to hierarchical logging. |
Unknown | Only applicable to hierarchical logging and is used to turn off logging completely. |
下面对几个容易产生误解的级别加以补充说明:
最后附上 Easylogging++ 日志级别测试代码,自己亲自动手试一试吧:
#include "easylogging++.h"
INITIALIZE_EASYLOGGINGPP
int main(int argc, char** argv)
{
/// 防止Fatal级别日志中断程序
el::Loggers::addFlag(el::LoggingFlag::DisableApplicationAbortOnFatalLog);
/// 选择划分级别的日志
el::Loggers::addFlag(el::LoggingFlag::HierarchicalLogging);
/// 设置级别门阀值,修改参数可以控制日志输出
el::Loggers::setLoggingLevel(el::Level::Global);
LOG(TRACE);
LOG(DEBUG);
LOG(FATAL);
LOG(ERROR);
LOG(WARNING);
LOG(INFO);
VLOG(0);
/// Debug模式日志记录
DLOG(TRACE);
DLOG(DEBUG);
DLOG(FATAL);
DLOG(ERROR);
DLOG(WARNING);
DLOG(INFO);
DVLOG(0);
system("pause");
return 0;
}