在CentOS系統中配置C++日志系統,通常需要以下幾個步驟:
選擇日志庫:
安裝日志庫:
例如,使用yum安裝spdlog:
sudo yum install epel-release
sudo yum install spdlog-devel
編寫C++代碼:
示例代碼:
#include "spdlog/spdlog.h"
#include "spdlog/sinks/stdout_color_sinks.h"
int main() {
// 創建一個控制臺日志記錄器
auto console = spdlog::stdout_color_mt("console");
// 設置日志級別
console->set_level(spdlog::level::debug);
// 記錄日志
console->info("Welcome to spdlog!");
console->error("Some error message with arg: {}", 1);
return 0;
}
編譯項目:
示例編譯命令:
g++ -std=c++11 -o myapp myapp.cpp -lspdlog
運行程序:
./myapp
配置日志系統:
示例配置:
#include "spdlog/spdlog.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/rotating_file_sink.h"
int main() {
// 創建一個控制臺日志記錄器
auto console = spdlog::stdout_color_mt("console");
console->set_level(spdlog::level::debug);
// 創建一個文件日志記錄器
auto file_logger = spdlog::rotating_logger_mt("file_logger", "logs/myapp.log", 1024 * 1024, 3);
file_logger->set_level(spdlog::level::info);
// 設置日志格式
spdlog::set_pattern("[%Y-%m-%d %H:%M:%S] [%l] %v");
// 記錄日志
console->info("Welcome to spdlog!");
console->error("Some error message with arg: {}", 1);
file_logger->info("This is an info message in the file.");
return 0;
}
通過以上步驟,你可以在CentOS系統中配置和使用C++日志系統。根據具體需求,你可以進一步自定義日志系統的行為和輸出格式。