C++项目在 CentOS 的日志管理实践
一 方案总览与选型
二 快速上手示例
#include <fstream>
#include <iostream>
#include <string>
#include <ctime>
#include <mutex>
std::mutex log_mtx;
void logMessage(const std::string& msg) {
std::lock_guard<std::mutex> lk(log_mtx);
std::ofstream of("app.log", std::ios_base::app);
if (!of) { std::cerr << "无法打开日志文件\n"; return; }
time_t now = time(nullptr);
char buf[64];
strftime(buf, sizeof(buf), "%F %T", localtime(&now));
of << "[" << buf << "] " << msg << '\n';
}
int main() { logMessage("程序启动"); return 0; }
sudo yum install -y epel-release cmake gcc-c++
git clone https://github.com/gabime/spdlog.git
cd spdlog && mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make -j$(nproc) && sudo make install
#include "spdlog/spdlog.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
int main() {
auto file = spdlog::basic_logger_mt("file", "logs/app.log");
auto console = spdlog::stdout_color_mt("console");
spdlog::set_default_logger(file);
spdlog::set_level(spdlog::level::debug);
file->set_pattern("[%Y-%m-%d %H:%M:%S] [%l] %v");
console->set_pattern("[%H:%M:%S] [%^%l%$] %v");
SPDLOG_INFO("服务启动,版本={}", "1.2.3");
SPDLOG_WARN("磁盘余量低: {}%", 6);
SPDLOG_ERROR("初始化失败: {}", "配置缺失");
return 0;
}
g++ -std=c++11 -O2 -o myapp main.cpp -lspdlog
#include <syslog.h>
int main() {
openlog("myapp", LOG_PID | LOG_CONS, LOG_USER);
syslog(LOG_INFO, "服务启动,pid=%d", getpid());
closelog();
return 0;
}
#include <systemd/sd-journal.h>
int main() {
sd_journal_send("MESSAGE=服务启动", "PRIORITY=%i", LOG_INFO, NULL);
return 0;
}
[Unit]
Description=My C++ App
[Service]
ExecStart=/usr/local/bin/myapp
StandardOutput=journal
StandardError=journal
SyslogIdentifier=myapp
[Install]
WantedBy=multi-user.target
查看:journalctl -u myapp.service -f。三 日志轮转与保留策略
auto daily = spdlog::daily_logger_mt("daily", "logs/app.log", 2, 30); // 每日 02:30 切分
daily->set_level(spdlog::level::info);
// 示例:按 10MB、保留 7 个文件
// auto rotating = spdlog::rotating_logger_mt("rot", "logs/app.log", 10*1024*1024, 7);
/var/log/myapp/*.log {
daily
rotate 7
compress
missingok
notifempty
create 0640 myapp myapp
copytruncate
}
kill -HUP <pid> 触发重开。logrotate -d /etc/logrotate.d/myapp(干跑),logrotate -f /etc/logrotate.d/myapp(强制执行)。四 作为 systemd 服务时的日志最佳实践
StandardOutput=journal、StandardError=journal,并用 SyslogIdentifier=your_app 统一标识;通过 journalctl -u your_app.service -f 实时查看。五 性能与安全建议