C++ - configuration and use of single header cross-platform efficient open source log library Easylogging++

1 Easylogging++

Easylogging++ is an open source cross-platform logging library with only a single header file. It has the advantages of being simple and easy to integrate, extremely fast, thread-safe, efficient, configurable and scalable. It is now my main logging library.

1.1 Download Easylogging++

Github address: https://github.com/amrayn/easyloggingpp

Download Easylogging++ from Githu, there are only two files downloaded, easylogging++.hand easylogging++.cc.

1.1 Configure Easylogging++ in VS

Right-click the project-properties-C+±general-additional include items, add easylogging++.hthe directory

will easylogging++.ccbe added to the project.

1.2 Using Easylogging++

(1) Include header files

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

(2) Initialize Easylogging++

INITIALIZE_EASYLOGGINGPP

(3) Set log output configuration

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) Sample program

// 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";
}

If you are interested, you can visit my personal website: https://www.stubbornhuang.com/

Guess you like

Origin blog.csdn.net/HW140701/article/details/127532430