glog下载和编译
- glog开源地址
https://github.com/google/glog
- glog静态库编译
cd /home/wangz/3rdParty/hldglog/glogmkdir out
mkdir build && cd buildcmake .. -DCMAKE_INSTALL_PREFIX=../out -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF
本文选择的glog版本为glog-0.7.0 |
Hldglog类封装
#ifndef HLD_GLOG_H
#define HLD_GLOG_H#define GLOG_USE_GLOG_EXPORT
#include <glog/logging.h>#define HLD_LOG_INFO LOG(INFO)
#define HLD_LOG_WARNING LOG(WARNING)
#define HLD_LOG_ERROR LOG(ERROR)
// #define LOG_FATAL LOG(FATAL) FATAL消息会终止程序(在记录消息之后),禁用class Hldglog
{
public:static Hldglog *InitGlog(const char *argv, std::string logPath);private:Hldglog() = default;virtual ~Hldglog();Hldglog(const Hldglog &) = delete;Hldglog &operator=(const Hldglog &) = delete;Hldglog(Hldglog &&) = delete;Hldglog &operator=(Hldglog &&) = delete;private:static void CustomePrefixFormatter(std::ostream &s, const google::LogMessage &m, void *data);private:static std::string m_FilePath;static Hldglog *m_instance;
};#endif
#include "hldglog.h"
#include <iostream>
#include <chrono>
#include <iomanip>
#include <ctime>
#include <functional>using namespace std::chrono_literals;std::string Hldglog::m_FilePath = "./log";
Hldglog *Hldglog::m_instance = nullptr;Hldglog::~Hldglog()
{google::ShutdownGoogleLogging();
}Hldglog *Hldglog::InitGlog(const char *argv, std::string logPath)
{if (m_instance == nullptr){if (logPath.empty()){Hldglog::m_FilePath = "./log";}else{Hldglog::m_FilePath = logPath;}google::InitGoogleLogging(argv); // 初始化谷歌的日志库// 自定义日志格式google::InstallPrefixFormatter(Hldglog::CustomePrefixFormatter, nullptr);FLAGS_log_dir = Hldglog::m_FilePath; // 设置日志文件存放的目录FLAGS_minloglevel = 0; // 设置日志抑制级别FLAGS_stderrthreshold = google::GLOG_INFO; // 设置日志记录到文件的级别FLAGS_alsologtostderr = true; // 错误信息同时输出到终端和文件FLAGS_colorlogtostderr = true; // 设置输出到屏幕的日志显示相应颜色FLAGS_max_log_size = 2; // 最大日志大小(单位为MB)FLAGS_logbufsecs = 0; // 缓冲日志输出,默认为30秒,此处改为立即输出(日志实时输出)FLAGS_stop_logging_if_full_disk = true; // 当磁盘被写满时,停止日志输出FLAGS_timestamp_in_logfile_name = false; // 日志文件名取消时间戳// 获取当前日期time_t now = time(nullptr);struct tm *local_time = localtime(&now);// 从tm结构体中获取年、月、日int year = local_time->tm_year + 1900; // tm_year是以1900年为基的int month = local_time->tm_mon + 1; // tm_mon是以0为1月的int day = local_time->tm_mday; // tm_mday是月份中的哪一天std::stringstream ss;ss << year << "-" << month << "-" << day;std::string current_date = ss.str();std::string info_log_path = Hldglog::m_FilePath + "/INFO_" + current_date;std::string warn_log_path = Hldglog::m_FilePath + "/WARNING_" + current_date;std::string error_log_path = Hldglog::m_FilePath + "/ERROR_" + current_date;google::SetLogDestination(google::GLOG_INFO, info_log_path.c_str()); // 设置google::GLOG_INFO级别的日志存储路径和文件名前缀google::SetLogDestination(google::GLOG_WARNING, warn_log_path.c_str()); // 设置google::GLOG_WARNING级别的日志存储路径和文件名前缀google::SetLogDestination(google::GLOG_ERROR, error_log_path.c_str()); // 设置google::GLOG_ERROR级别的日志存储路径和文件名前缀google::SetLogFilenameExtension(".log"); // 设置日志文件的扩展名google::EnableLogCleaner(24h * 7); // 自动删除旧的日志,设置期限为7天m_instance = new Hldglog();}return m_instance;
}void Hldglog::CustomePrefixFormatter(std::ostream &s, const google::LogMessage &m, void *data)
{// [L thread_id] yyyymmdd hh:mm:ss.uuuuuu [file:line]// msg...// google::GetLogSeverityName(m.severity())[0] 获取日志级别s << "[" << google::GetLogSeverityName(m.severity())[0] << ' ' << m.thread_id() << "]"<< ' '<< std::setw(4) << 1900 + m.time().year() << "-"<< std::setw(2) << 1 + m.time().month() << "-"<< std::setw(2) << m.time().day()<< ' '<< std::setw(2) << m.time().hour() << ':'<< std::setw(2) << m.time().min() << ':'<< std::setw(2) << m.time().sec() << "."<< std::setw(6) << m.time().usec()<< ' '<< "[" << m.basename() << ':' << m.line() << "]"<< "\n";
}
- 使用方法
#include <iostream>
#include "hldglog.h"
#include <unistd.h>using namespace std;int main(int argc, char *argv[])
{Hldglog::InitGlog(argv[0], "./log");HLD_LOG_INFO << "HLD_LOG_INFO";HLD_LOG_WARNING << "HLD_LOG_WARNING";HLD_LOG_ERROR << "HLD_LOG_ERROR";while (true){sleep(10);}return 0;
}
-
该类设置日志保留的时间为7天
-
该类的日志输出格式:
[L thread_id] yyyymmdd hh:mm:ss.uuuuuu [file:line]
msg…
-
效果展示
- 日志文件的格式
- INFO_日期.log
- WARNING_日期.log
- ERROR_日期.log
std::string info_log_path = Hldglog::m_FilePath + "/INFO_" + current_date;
std::string warn_log_path = Hldglog::m_FilePath + "/WARNING_" + current_date;
std::string error_log_path = Hldglog::m_FilePath + "/ERROR_" + current_date;google::SetLogDestination(google::GLOG_INFO, info_log_path.c_str()); // 设置google::GLOG_INFO级别的日志存储路径和文件名前缀
google::SetLogDestination(google::GLOG_WARNING, warn_log_path.c_str()); // 设置google::GLOG_WARNING级别的日志存储路径和文件名前缀
google::SetLogDestination(google::GLOG_ERROR, error_log_path.c_str()); // 设置google::GLOG_ERROR级别的日志存储路径和文件名前缀
这样设计的好处:确保了当应用程序在同一日内多次启动时,不会生成多个日志文件,从而有效避免了日志分散的问题,保持日志的连续性和管理的便捷性