【C++】single header跨平台高效开源日志库Easylogging++的配置和使用

【C++】single header跨平台高效开源日志库Easylogging++的配置和使用

一、Easylogging++简介

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

二、下载Easylogging++

Github地址:https://github.com/cuttingdust/easyloggingpp.git

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

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

在这里插入图片描述
在这里插入图片描述

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

三、使用Easylogging++

(一) 包含头文件

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

(二) 初始化Easylogging++

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);
}

(四) 示例程序

// 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";
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值