自定义日志格式
重载默认文件接收器的文件头
默认文件头可以在默认文件接收器中自定义:
FileSink::overrideLogHeader(std::string);
重载默认文件接收器的日志格式
默认的日志格式是在LogMessage.hpp
static std::string DefaultLogDetailsToString(const LogMessage& msg);
在日志格式中添加线程ID
An “all details” log formatting function is also defined - this one also adds the “calling thread’s ID”
static std::string FullLogDetailsToString(const LogMessage& msg);
重载默认Sink日志格式
为了方便重载默认Sink有一个函数可以实现
void overrideLogDetails(LogMessage::LogDetailsFunc func);
将默认日志格式替换为“完整详细信息”格式的示例代码(添加线程ID
auto worker = g3::LogWorker::createLogWorker();
auto handle= worker->addDefaultLogger(argv[0], path_to_log_file);
g3::initializeLogging(worker.get());
handle->call(&g3::FileSink::overrideLogDetails, &LogMessage::FullLogDetailsToString);
查阅test_message.cpp获取更多细节和测试
重载自定义接收器的格式设置的示例代码,日志格式化函数将被传递到LogMessage::toString(...)
,覆盖掉默认的日志格式
示例
namespace {
std::string MyCustomFormatting(const LogMessage& msg) {
... how you want it ...
}
}
void MyCustomSink::ReceiveLogEntry(LogMessageMover message) {
std::string formatted = message.get().toString(&MyCustomFormatting) << std::flush;
}
...
...
auto worker = g3::LogWorker::createLogWorker();
auto sinkHandle = worker->addSink(std::make_unique<MyCustomSink>(),
&MyCustomSink::ReceiveLogMessage);
// ReceiveLogMessage(...) will used the custom formatting function "MyCustomFormatting(...)