日志库EasyLogging++学习系列(2)—— 日志级别

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/Fish_55_66/article/details/49407891

在很多的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.
下面对几个容易产生误解的级别加以补充说明:

  • Global 级别,一个概念性的级别,不能应用于实际的日志记录,也就是说不能用宏 LOG(GLOBLE) 进行日志记录。在划分级别的日志记录中,设置门阀值为 el::Level::Global 表示所有级别的日志都生效。
  • Trace 级别,也许很多人会联想到VC中的宏TRACE,自然而然地以为Trace级别的日志只会在debug版本有效。不过实际验证发现,不论是debug还是release版本,Trace级别的日记都会生效。
  • Debug 级别,只在debug模式生效,在Release模式会自动屏蔽该级别所有的日志记录。除了可以用该级别来记录debug模式日志之外,还有一些专门用于debug模式的宏定义,更多详细信息可以查看《日志库EasyLogging++学习系列(7)—— 记录方式详解》。
  • Fatal 级别,默认情况下会使程序中断,可设置标记 LoggingFlag::DisableApplicationAbortOnFatalLog 来阻止中断
  • Verbose 级别,可以更加详细地记录日志信息,但不适用于划分级别的日志记录,意思就是说即使门阀值设置大于该级别,该级别的日志记录同样生效。同时,该级别只能用宏VLOG而不能用宏 LOG(VERBOSE) 进行日志记录,并且在默认情况下,只有VLOG(0)日志记录生效,更多详细信息可以查看《日志库EasyLogging++学习系列(8)—— Verbose日志详解
  • Unknown 级别,同样也是一个概念性的级别,不能用宏 LOG(UNKNOWN) 进行日志记录。该级别只适用于在划分级别的日志记录中,如果设置门阀值为 el::Level::Unknown ,那么就表示所有级别的日志记录都会被完全屏蔽,需要注意的是,Verbose 级别不受此影响。但是如果程序没有设置划分级别标记:LoggingFlag::HierarchicalLogging,那么即使设置了门阀值为 el::Level::Unknown,而其他级别的日志记录也会正常输出。
最后附上 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;
}

猜你喜欢

转载自blog.csdn.net/Fish_55_66/article/details/49407891