C++ Easylogging++日志库配置使用超详细讲解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C++ Easylogging++日志库配置使用超详细讲解

HW140701   2022-11-12 我要评论

Easylogging++

Easylogging++是一个只有单个头文件的开源跨平台日志库,拥有简单易集成,速度极快,线程安全,高效并可配置可扩展等等优点,现在也是我的主力日志库。

下载Easylogging++

Github地址:https://github.com/amrayn/easyloggingpp

从Githu下载Easylogging++,下载下来只有两个文件,easylogging++.heasylogging++.cc

在VS中配置Easylogging++

右键项目-属性-C+±常规-附加包含项目,添加easylogging++.h所在目录

easylogging++.cc添加到项目中。

使用Easylogging++

(1) 包含头文件

// easylogging++
#include "easylogging++.h"
#define ELPP_THREAD_SAFE

(2) 初始化Easylogging++

INITIALIZE_EASYLOGGINGPP

(3) 设置日志输出配置

static void InitEasyloggingPP()
{
	el::Configurations conf;
	// 启用日志
	conf.setGlobally(el::ConfigurationType::Enabled, "true");
	//设置日志文件目录以及文件名
	conf.setGlobally(el::ConfigurationType::Filename, "log\\log_%datetime{%Y%M%d %H%m%s}.log");
	//设置日志文件最大文件大小
	conf.setGlobally(el::ConfigurationType::MaxLogFileSize, "20971520");
	//是否写入文件
	conf.setGlobally(el::ConfigurationType::ToFile, "true");
	//是否输出控制台
	conf.setGlobally(el::ConfigurationType::ToStandardOutput, "true");
	//设置日志输出格式
	conf.setGlobally(el::ConfigurationType::Format, "[%datetime] [%loc] [%level] : %msg");
	//设置日志文件写入周期,如下每100条刷新到输出流中
	conf.setGlobally(el::ConfigurationType::LogFlushThreshold, "100");
	//设置配置文件
	el::Loggers::reconfigureAllLoggers(conf);
}

(4) 示例程序

// easylogging++
#include "easylogging++.h"
#define ELPP_THREAD_SAFE
INITIALIZE_EASYLOGGINGPP
static void InitEasyloggingPP()
{
	el::Configurations conf;
	// 启用日志
	conf.setGlobally(el::ConfigurationType::Enabled, "true");
	//设置日志文件目录以及文件名
	conf.setGlobally(el::ConfigurationType::Filename, "log\\log_%datetime{%Y%M%d %H%m%s}.log");
	//设置日志文件最大文件大小
	conf.setGlobally(el::ConfigurationType::MaxLogFileSize, "20971520");
	//是否写入文件
	conf.setGlobally(el::ConfigurationType::ToFile, "true");
	//是否输出控制台
	conf.setGlobally(el::ConfigurationType::ToStandardOutput, "true");
	//设置日志输出格式
	conf.setGlobally(el::ConfigurationType::Format, "[%datetime] [%loc] [%level] : %msg");
	//设置日志文件写入周期,如下每100条刷新到输出流中
	conf.setGlobally(el::ConfigurationType::LogFlushThreshold, "100");
	//设置配置文件
	el::Loggers::reconfigureAllLoggers(conf);
}
int main()
{
	InitEasyloggingPP();
	LOG(INFO) << "Hello World";
}

 

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们