在debian9上使用syslog后端时,是否可以配置boost日志将日志发送到特定文件。以下代码初始化我的日志记录接收器:
namespace logging = boost::log;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
using namespace boost::log::sinks::syslog;
boost::shared_ptr< logging::core > core = logging::core::get();
boost::shared_ptr< sinks::syslog_backend > backend =
boost::make_shared< sinks::syslog_backend >(
keywords::use_impl = sinks::syslog::udp_socket_based,
keywords::facility = sinks::syslog::local0
);
// Create and fill in another level translator for "MyLevel" attribute of type string
sinks::syslog::custom_severity_mapping< std::string > mapping("MyLevel");
mapping["debug"] = sinks::syslog::debug;
mapping["info"] = sinks::syslog::info;
mapping["warning"] = sinks::syslog::warning;
mapping["error"] = sinks::syslog::error;
backend->set_severity_mapper(mapping);
backend->set_target_address("localhost");
// Wrap it into the frontend and register in the core.
// The backend requires synchronization in the frontend.
typedef sinks::synchronous_sink< sinks::syslog_backend > sink_t;
boost::shared_ptr< sink_t > sink(new sink_t(backend));
core->add_sink(sink);
答案 0 :(得分:1)
syslog API(和协议)不允许应用程序指定日志服务器处理日志的方式。为此,您必须配置syslog服务器。请参阅服务器的文档(例如rsyslog,syslog-ng或journald,以便通过syslog API进行登录。)
如果您确实想在应用程序中配置目标文件名,则必须从应用程序中编写该文件。 text_file_backend
下沉后端将帮助您实现这一目标。