使用 glog 实现高效的日志记录
glog高性能:基于异步日志记录,减少对主线程的影响。灵活的日志级别:支持多种日志级别(如 INFO、WARNING、ERROR、FATAL)。日志文件管理:自动按日期或大小分割日志文件。线程安全:支持多线程环境下的日志记录。跨平台:支持 Linux、macOS 和 Windows 等操作系统。glog广泛应用于 Google 的开源项目(如 gRPC、TensorFlow)以及其他许多 C++
使用 glog 实现高效的日志记录
在开发和维护应用程序时,日志记录是一个不可或缺的工具。它不仅可以帮助开发者调试问题,还能用于监控应用程序的运行状态。Google 的 glog 库是一个功能强大且高效的 C++ 日志库,广泛应用于各种项目中。
本文将详细介绍 glog 的核心功能、使用方法以及如何利用它实现高效的日志记录。
1. 什么是 glog?
glog 是 Google 提供的一个 C++ 日志库,具有以下特点:
- 高性能:基于异步日志记录,减少对主线程的影响。
- 灵活的日志级别:支持多种日志级别(如 INFO、WARNING、ERROR、FATAL)。
- 日志文件管理:自动按日期或大小分割日志文件。
- 线程安全:支持多线程环境下的日志记录。
- 跨平台:支持 Linux、macOS 和 Windows 等操作系统。
glog 广泛应用于 Google 的开源项目(如 gRPC、TensorFlow)以及其他许多 C++ 项目中。
2. 安装 glog
在开始使用 glog 之前,需要先安装它。
在 Ubuntu 上安装
sudo apt-get install libgoogle-glog-dev
在 macOS 上安装
brew install glog
在 Windows 上安装
可以从 glog GitHub 仓库 下载源码并编译,或者使用 vcpkg:
vcpkg install glog
3. 核心功能
1. 日志级别
glog 支持以下日志级别:
- INFO:用于记录常规信息。
- WARNING:用于记录警告信息。
- ERROR:用于记录错误信息。
- FATAL:用于记录致命错误,并终止程序。
2. 日志文件管理
glog 会自动管理日志文件,支持以下功能:
- 按日期分割日志文件。
- 按大小分割日志文件。
- 自动删除旧的日志文件。
3. 日志格式
glog 的日志格式如下:
<日志级别><日期时间> <进程ID> <文件名:行号>] <日志内容>
例如:
I20231012 12:34:56.789012 12345 main.cpp:10] This is an info message.
4. 基本用法
1. 初始化 glog
在使用 glog 之前,需要先初始化它。
#include <glog/logging.h>
int main(int argc, char* argv[]) {
// 初始化 glog
google::InitGoogleLogging(argv[0]);
// 设置日志输出目录(可选)
google::SetLogDestination(google::INFO, "/path/to/log/");
// 设置日志级别(可选)
FLAGS_minloglevel = google::INFO;
// 记录日志
LOG(INFO) << "This is an info message.";
LOG(WARNING) << "This is a warning message.";
LOG(ERROR) << "This is an error message.";
// 清理 glog 资源
google::ShutdownGoogleLogging();
return 0;
}
2. 记录日志
使用 LOG 宏记录日志,例如:
LOG(INFO) << "This is an info message.";
LOG(WARNING) << "This is a warning message.";
LOG(ERROR) << "This is an error message.";
LOG(FATAL) << "This is a fatal error message.";
3. 条件日志
glog 支持条件日志,例如:
int value = 10;
LOG_IF(INFO, value > 5) << "Value is greater than 5.";
4. 频率限制日志
glog 支持频率限制日志,例如:
for (int i = 0; i < 10; ++i) {
LOG_EVERY_N(INFO, 3) << "This log will be printed every 3 times. Current count: " << i;
}
5. 首次日志
glog 支持首次日志,例如:
LOG_FIRST_N(INFO, 2) << "This log will be printed only the first 2 times.";
5. 高级特性
1. 自定义日志格式
glog 允许自定义日志格式,例如:
google::InstallFailureSignalHandler();
google::InstallFailureWriter([](const char* data, int size) {
std::cerr << "Custom failure writer: " << std::string(data, size);
});
2. 信号处理
glog 可以捕获信号并记录相关信息,例如:
google::InstallFailureSignalHandler();
3. 日志文件管理
glog 支持自动管理日志文件,例如:
FLAGS_log_dir = "/path/to/log/";
FLAGS_max_log_size = 100; // 每个日志文件最大 100MB
FLAGS_logbufsecs = 0; // 立即刷新日志
6. 示例代码
以下是一个完整的示例,展示如何使用 glog 记录日志。
#include <glog/logging.h>
#include <iostream>
int main(int argc, char* argv[]) {
// 初始化 glog
google::InitGoogleLogging(argv[0]);
// 设置日志输出目录
google::SetLogDestination(google::INFO, "./log/");
// 设置日志级别
FLAGS_minloglevel = google::INFO;
// 记录日志
LOG(INFO) << "This is an info message.";
LOG(WARNING) << "This is a warning message.";
LOG(ERROR) << "This is an error message.";
// 条件日志
int value = 10;
LOG_IF(INFO, value > 5) << "Value is greater than 5.";
// 频率限制日志
for (int i = 0; i < 10; ++i) {
LOG_EVERY_N(INFO, 3) << "This log will be printed every 3 times. Current count: " << i;
}
// 首次日志
LOG_FIRST_N(INFO, 2) << "This log will be printed only the first 2 times.";
// 清理 glog 资源
google::ShutdownGoogleLogging();
return 0;
}
7. 编译和运行
编译
确保已安装 glog 开发库,然后使用以下命令编译:
g++ -std=c++11 glog_example.cpp -lglog -o glog_example
运行
运行生成的可执行文件:
./glog_example
8. 运行结果
日志文件 ./log/<程序名>.INFO 中的内容:
I20231012 12:34:56.789012 12345 glog_example.cpp:10] This is an info message.
W20231012 12:34:56.789123 12345 glog_example.cpp:11] This is a warning message.
E20231012 12:34:56.789234 12345 glog_example.cpp:12] This is an error message.
I20231012 12:34:56.789345 12345 glog_example.cpp:15] Value is greater than 5.
I20231012 12:34:56.789456 12345 glog_example.cpp:19] This log will be printed every 3 times. Current count: 2
I20231012 12:34:56.789567 12345 glog_example.cpp:19] This log will be printed every 3 times. Current count: 5
I20231012 12:34:56.789678 12345 glog_example.cpp:19] This log will be printed every 3 times. Current count: 8
I20231012 12:34:56.789789 12345 glog_example.cpp:23] This log will be printed only the first 2 times.
9. 总结
glog 是一个功能强大且高效的日志库,适用于各种 C++ 项目。通过它,开发者可以轻松实现灵活的日志记录,并提高应用程序的可维护性和可调试性。
希望本文能帮助你快速上手 glog,并将其应用到实际项目中。如果你有任何问题或建议,欢迎在评论区留言!
参考文档
Happy coding! 🚀
更多推荐

所有评论(0)