C++ – single header跨平台高效开源日志库Easylogging++的配置和使用
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ – single header跨平台高效开源日志库Easylogging++的配置和使用
原文链接:https://www.stubbornhuang.com/2395/
发布于:2022年10月26日 11:15:35
修改于:2022年10月26日 14:51:28

1 Easylogging++
Easylogging++是一个只有单个头文件的开源跨平台日志库,拥有简单易集成,速度极快,线程安全,高效并可配置可扩展等等优点,现在也是我的主力日志库。
1.1 下载Easylogging++
Github地址:https://github.com/amrayn/easyloggingpp
从Githu下载Easylogging++,下载下来只有两个文件,easylogging++.h
和easylogging++.cc
。
1.1 在VS中配置Easylogging++
右键项目-属性-C++-常规-附加包含项目,添加easylogging++.h
所在目录


将easylogging++.cc
添加到项目中。
1.2 使用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";
}
当前分类随机文章推荐
- C++ - std::string替换字符串中所有指定的子字符串 阅读2086次,点赞1次
- C++读取Shp文件并将Shp转化为DXF 阅读3036次,点赞1次
- C++ - 字节数组byte[]或者unsigned char[]与int的相互转换 阅读6554次,点赞1次
- C++ - 对字符串和图片进行base64编解码 阅读115次,点赞0次
- C++ - 使用标准库std::use_facet和std::codecvt进行跨平台gbk与utf8字符集转换 阅读74次,点赞0次
- Centos7 编译C++项目错误解决 : terminate called after throwing an instance of 'std::regex_error' 阅读2428次,点赞1次
- C++ - vector存储动态指针时正确释放内存 阅读5568次,点赞0次
- CMake - 设置Debug或者Release编译模式 阅读2084次,点赞0次
- C++ - 使用Websocket++编写客户端连接WebSocket服务器并进行通信 阅读4073次,点赞3次
- C++ - std::unordered_map中使用结构体或者vector等复杂的数据结构作为Key值 阅读322次,点赞0次
全站随机文章推荐
- Onnx - onnx模型简化与优化 阅读16次,点赞0次
- Duilib - 超链接文本 阅读3445次,点赞0次
- 资源分享 - Cloth Simulation for Computer Graphics 英文高清PDF下载 阅读2136次,点赞0次
- 资源分享 - 深度学习与图像识别:原理与实践 (魏溪含 涂铭 张修鹏著) 高清PDF下载 阅读2316次,点赞1次
- C++11 - 使用std::thread,std::shared_future,std::promise并行化/多线程化for循环,提升处理速度 阅读1373次,点赞0次
- 资源分享 - Non-Photorealistic Computer Graphics - Modeling, Rendering, and Animation 英文高清PDF下载 阅读1711次,点赞0次
- 深度学习 - 通俗理解Beam Search Algorithm算法 阅读627次,点赞0次
- C++11 - std::shared_ptr初始化的几种方式 阅读6871次,点赞2次
- C++ – 字节数组byte[]或者unsigned char[]与bool的相互转换 阅读859次,点赞1次
- C++ - C++使用cuda api获取当前GPU显卡的总共的显存容量、已使用显存容量、剩余显存容量 阅读3834次,点赞2次
评论
167