一、plsql使用odbc導入器
在進行日誌記錄時,通常需要將日誌保存在文件中或者將其存儲在資料庫中。而當需要將日誌存儲在Oracle資料庫中時,可以使用plsql開發工具並使用odbc導入器。
begin
insert into Logtable values ('DEBUG', '2022-1-1 12:30:00', 'this is a debug log');
end;
二、spdlog優缺點
spdlog是一個高速且靈活的C++日誌記錄庫,可以進行多線程支持、非同步記錄、日誌回滾等。
優點:
- 速度非常快
- 提供了多種輸出方式
- 支持多線程
- 易於使用
缺點:
- 不支持跨平台
- 可能不適合所有的項目需要
三、spdlog 多線程
spdlog支持多線程記錄日誌,可以讓多個線程同時進行記錄,避免了線程阻塞的問題。
// 創建並設置logger對象
auto console = spdlog::stdout_color_mt("console");
auto file_logger = spdlog::basic_logger_mt("file_logger", "logs/spdlog.txt");
// 多線程記錄日誌
std::thread t1([](){
console->info("This is thread 1.");
});
std::thread t2([](){
console->info("This is thread 2.");
});
t1.join();
t2.join();
四、spdlog庫
使用spdlog,我們可以方便地進行日誌記錄、管理和輸出。
如何使用spdlog?
在使用spdlog之前,需要先安裝spdlog庫,在CMakeLists.txt中添加以下代碼:
# 添加spdlog庫
include(FetchContent)
FetchContent_Declare(
spdlog
GIT_REPOSITORY https://github.com/gabime/spdlog.git
GIT_TAG v1.9.2
)
FetchContent_GetProperties(spdlog)
if(NOT spdlog_POPULATED)
FetchContent_Populate(spdlog)
add_subdirectory(${spdlog_SOURCE_DIR} ${spdlog_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
然後在C++代碼中使用spdlog庫。
// 創建一個logger對象
auto console = spdlog::stdout_color_mt("console");
// 記錄日誌到console
console->info("This is a console log.");
五、spdlog sink
使用spdlog時,可以將日誌記錄到多個輸出位置,稱為sink。spdlog支持多種sink,如文件、控制台、網路等。
如何使用sink?
// 添加控制台sink
auto console_sink = std::make_shared<spdlog::sinks::stdout_sink_mt>();
auto console_logger = std::make_shared<spdlog::logger>("console", console_sink);
// 添加文件sink
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/spdlog.txt", true);
auto file_logger = std::make_shared<spdlog::logger>("file_logger", file_sink);
六、spdlog flush
當使用spdlog進行日誌記錄時,有時候需要手動將緩存中的日誌刷進磁碟,可以使用flush函數來完成此操作。
// 記錄日誌並刷進磁碟
console->info("This is a console log.");
console->flush();
七、spdlog utf8
spdlog支持UTF-8編碼的日誌記錄,並提供了多種輸出編碼方式。
// 記錄UTF-8編碼的日誌並以UTF-16LE編碼輸出
auto utf16_sink = std::make_shared<spdlog::sinks::wincolor_stdout_sink_st<spdlog::details::null_mutex>>();
utf16_sink->set_encoding(spdlog::sinks::wincolor_sink_base::encoding_type::utf16);
auto utf16_logger = std::make_shared<spdlog::logger>("utf16_logger", utf16_sink);
utf16_logger->info(u8"This is a UTF-8 log.");
utf16_logger->flush();
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/236989.html