我正在使用Boost 1.54 Log并以下列方式初始化我的日志记录:
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace sinks = boost::log::sinks;
namespace attrs = boost::log::attributes;
namespace keywords = boost::log::keywords;
BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
BOOST_LOG_ATTRIBUTE_KEYWORD(tag_attr, "Tag", std::string)
BOOST_LOG_ATTRIBUTE_KEYWORD(timestamp, "TimeStamp", boost::posix_time::ptime)
logging::add_common_attributes();
boost::shared_ptr<logging::core> core = logging::core::get();
// setup console log
logging::add_console_log (
std::clog,
keywords::filter = severity >= debug,
keywords::format = (
expr::stream << expr::format_date_time(timestamp, "%Y-%m-%d %H:%M:%S") <<
line_id << " [" << severity << "] " << expr::smessage
)
);
Run Code Online (Sandbox Code Playgroud)
这是生成的输出的示例: …
BOOST_LOG_SEV我正在尝试使用严重性通道记录器,但我遇到了不是 const 方法的问题- 我想是因为 open_record() 和 push_record()
这基本上会迫使我将类中的所有方法设置为非 const,因为它们想要写入记录器。我真的负担不起这样做——所以目前我只能使用全球记录器。
在我当前的实现中,每个类都有一个以类名作为通道的记录器(在其构造函数中初始化),并且它随时发出日志消息BOOST_LOG_SEV(this->logger, level)
我很想听听非 constness 背后的原因,以及我的设计是否不适合 Boost::Log。
我正在尝试通过利用来即时压缩使用Boost Log库创建的日志文件boost::iostreams::gzip_compressor.因此,当我打电话时BOOST_LOG(),输出会即时压缩.这是我到目前为止所尝试的:
#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared_object.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sources/logger.hpp>
void init()
{
// Construct the sink
typedef boost::log::sinks::synchronous_sink< boost::log::sinks::text_ostream_backend > text_sink;
boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
boost::shared_ptr< std::ofstream > file = boost::make_shared< std::ofstream >(
"sample.gz", std::ios_base::out | std::ios_base::binary );
boost::iostreams::filtering_ostream out;
out.push( boost::iostreams::gzip_compressor() );
out.push( *(file.get()) );
for( int i = 0; i < …Run Code Online (Sandbox Code Playgroud) 我在尝试使用升压日志时遇到问题.我收到以下错误消息:
1>------ Build started: Project: vms, Configuration: Release x64 ------
1> main.cpp
1> Linking to lib file: libboost_system-vc140-mt-1_60.lib
1> Linking to lib file: libboost_date_time-vc140-mt-1_60.lib
1> Linking to lib file: libboost_regex-vc140-mt-1_60.lib
1> Linking to lib file: libboost_log-vc140-mt-1_60.lib
1> Linking to lib file: libboost_filesystem-vc140-mt-1_60.lib
1> Linking to lib file: libboost_date_time-vc140-mt-1_60.lib
1> Linking to lib file: libboost_thread-vc140-mt-1_60.lib
1> Linking to lib file: libboost_atomic-vc140-mt-1_60.lib
1> Linking to lib file: libboost_chrono-vc140-mt-1_60.lib
1> Linking to lib file: libboost_log_setup-vc140-mt-1_60.lib
1>main.obj : error LNK2001: unresolved external symbol "public: …Run Code Online (Sandbox Code Playgroud) 我希望能够从config指定日志记录严重性级别:
# my config file
LogSeverity = info
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?目前我的main功能中有这样的东西:
// logging settings
boost::log::trivial::severity_level logSeverity;
boost::program_options::options_description loggingSettings
("Logging settings");
loggingSettings.add_options()
("LogSeverity", value<boost::log::trivial::severity_level>(&logSeverity)
->required(),
"log level to output");
variables_map vm;
store(parse_config_file(configFilestream, loggingSettings), vm);
notify(vm);
boost::log::core::get()->set_filter(
boost::log::trivial::severity >= logSeverity);
BOOST_LOG_TRIVIAL(info) << "severity " << logSeverity;
Run Code Online (Sandbox Code Playgroud)
该程序的输出是:
[2015-05-18 09:58:40.783298] [0x000007f017445078] [info]严重性追踪
但是,我info在我的配置中设置了严重性(如上所述),为什么要设置为trace?
我一直在研究Boost.Log一段时间,我相信现在是我将代码库从log4cxx转换为Boost.Log的时候了.我相信Boost.Log的设计和实现将显着改善我的代码维护和使用.我知道Boost.Log FAQ有一个页面说
对于分层记录器,当前库设计中不需要此功能.它在log4j中提供的主要好处之一是确定日志记录最终的appender(接收器,就此库而言).该库通过过滤实现相同的结果.
我理解概念等价而不是试图将Boost.Log变成log4j/log4cxx.相反,我的问题是:如何使用Boost.Log来获得与我目前在log4cxx中使用的功能相同的功能?特别是,我想为日志源或通道层次结构中的特定节点设置严重性阈值和接收器.例如,我有按日期组织的日志源,libA.moduleB.componentC.logD其中层次结构中的级别由点分隔..使用log4cxx,可以使用libA更具体的记录器设置INFO 的总阈值,libA.moduleB阈值为DEBUG.
libA.threshold=INFO
libA.moduleB.threshold=DEBUG
Run Code Online (Sandbox Code Playgroud)
类似地,可以将接收器附加到层次结构中的任意节点.
我相信使用Boost.Log可以实现类似的功能,但我需要有关如何实际实现此功能的帮助/指导.另外,我确信其他想要从其他框架过渡到Boost.Log的人会有同样的问题.
我真诚地感谢您的评论.
我是Boost日志的新手。我有一些简单的东西可以工作,但是我一直坚持要使用宏来简化指定文件名和行号的工作。看起来好难,我想我已经错过了一些东西。
我编写此代码是为了在最后使用演示日志初始化日志记录。请注意,我正在登录控制台和旋转文件。
logging::add_console_log(std::clog, keywords::format = "%TimeStamp%: %_%",
keywords::auto_flush = true);
typedef sinks::synchronous_sink<sinks::text_file_backend> file_sink;
shared_ptr<file_sink> sink(new file_sink(
keywords::file_name = "%Y%m%d_%H%M%S_%5N.log",
keywords::rotation_size = 16384, keywords::auto_flush = true));
sink->locked_backend()->set_file_collector(sinks::file::make_collector(
keywords::target = "logs",
keywords::max_size = 16 * 1024 * 1024,
keywords::min_free_space = 100 * 1024 * 1024));
sink->locked_backend()->scan_for_files();
sink->set_formatter(expr::stream
<< expr::attr<boost::posix_time::ptime>("TimeStamp")
<< " " << logging::trivial::severity << "["
<< expr::attr<string>("FileName") << ":"
<< expr::attr<unsigned int>("LineNumber") << "] "
<< expr::smessage);
logging::core::get()->add_sink(sink);
logging::add_common_attributes();
logging::core::get()->add_global_attribute("TimeStamp",
attrs::local_clock());
src::severity_logger<logging::trivial::severity_level> slg;
lg.add_attribute("LineNumber", attrs::constant<unsigned int>(__LINE__));
lg.add_attribute("FileName", attrs::constant<string>(__FILE__));
for (unsigned int …Run Code Online (Sandbox Code Playgroud) 我正在使用boost :: log同时登录到文件和控制台。初始化如下:
void Init() {
logging::core::get()->set_filter
(
// set logging level to one of trace, debug, info, warning, error, fatal
logging::trivial::severity >= logging::trivial::trace
);
logging::add_common_attributes(); // enables timestamps and such
logging::add_file_log
(
keywords::file_name = logfile + "_%N.log",
keywords::rotation_size = 1024 * 1024 * 50, // 50MB log file max
// keywords::format = "[%TimeStamp%]: %Message%"
keywords::format = "%Message% #[%TimeStamp%]"
);
logging::add_console_log(std::cout,
keywords::format = "%Message%"
);
}
Run Code Online (Sandbox Code Playgroud)
在程序的某些时候,我想手动更改日志文件。我可以在上面的代码中更改“ logfile”字符串,然后再次调用该Init(),但是它将继续写入旧的日志文件并启动新的日志文件,并开始将控制台日志的输出加倍。
我缺少某种对应的“ remove_file_log”,还是手动告诉它停止记录到原始日志并移至下一个日志?
我有一个日志设置,其中有两种类型的日志消息:
这些属性定义如下:
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", trivial::severity_level)
BOOST_LOG_ATTRIBUTE_KEYWORD(tag_attr, "Tag", std::string)
Run Code Online (Sandbox Code Playgroud)
我想创建一个过滤器功能,允许根据 2 个标准中的任何一个将消息添加到我的日志中(请注意,基于自定义标记属性的日志消息始终与严重性级别信息一起打印,基于平凡记录器的严重性水平)。
所以我想要一个过滤器,它允许基于消息是否具有自定义标签的消息,如果没有,则基于消息的严重性。
我试图有一个相对简单的过滤器,它执行以下操作:
sink_->set_filter(
trivial::severity >= severityLevel
|| (expr::has_attr(tag_attr) && tag_attr == "JSON" && logJson_)
);
Run Code Online (Sandbox Code Playgroud)
但是,severityLevel 可能是 Debug、Info、Warning、Error 或 Fatal,如果级别配置为 Debug 或 Info,则过滤器将忽略自定义标记属性。
我曾尝试使用 c++11 lambda,如下所示:
sink_->set_filter([this, severityLevel](const auto& attr_set) {
if (<condition for custom tag first>) {
return true;
} else if (<condition for severity level second>) {
return true;
} else {
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
但后来我不知道如何实际检查我的条件。我尝试了以下方法:
if (attr_set["Tag"].extract<std::string>() == "JSON" && logJson_) …Run Code Online (Sandbox Code Playgroud) 我是boost-log的新手,并尝试使用过滤器示例使用boost-log 1.1 进行简单的日志记录.代码如下所示:
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/filters.hpp>
void init()
{
logging::core::get()->set_filter
(
filters::attr< logging::trivial::severity_level >("Severity") >= logging::trivial::info
);
}
int main(int, char*[])
{
BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
}
Run Code Online (Sandbox Code Playgroud)
使用clang进行编译会产生以下错误消息:
/Users/admin/Documents/cmake tests/boost-log/main.cpp:7:5: error: use of undeclared identifier 'logging'
logging::core::get()->set_filter
^
/Users/admin/Documents/cmake tests/boost-log/main.cpp:9:3: error: use of undeclared identifier 'filters'; did you mean 'boost::log_mt_posix::filters'?
filters::attr< logging::trivial::severity_level >("Severity") >= logging::trivial::info
^~~~~~~
boost::log_mt_posix::filters
/usr/local/include/boost/log/filters/has_attr.hpp:32:11: note: 'boost::log_mt_posix::filters' declared here
namespace filters {
^
/Users/admin/Documents/cmake tests/boost-log/main.cpp:9:18: error: …Run Code Online (Sandbox Code Playgroud) 我想知道是否有类似的东西FLAGS_stderrthreshold只输出比boost库中的标志更严重的级别.例如,我下面的代码将所有内容打印到控制台.我可以设置级别,以便它只显示警告,错误和致命吗?
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
int main() {
using namespace boost::log::trivial;
boost::log::sources::severity_logger< severity_level > lg;
BOOST_LOG_SEV(lg, trace) << "A trace severity message";
BOOST_LOG_SEV(lg, debug) << "A debug severity message";
BOOST_LOG_SEV(lg, info) << "An informational severity message";
BOOST_LOG_SEV(lg, warning) << "A warning severity message";
BOOST_LOG_SEV(lg, error) << "An error severity message";
BOOST_LOG_SEV(lg, fatal) << "A fatal severity message";
return 0
}
Run Code Online (Sandbox Code Playgroud)
实际产量:
[2014-03-13 22:21:55.734957] [0xc00001d6] [trace] A trace severity message
[2014-03-13 22:21:55.735957] [0xc00001d6] [debug] A debug severity message
[2014-03-13 …Run Code Online (Sandbox Code Playgroud) 我在以下链接中使用该示例.
我的代码如下.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/from_stream.hpp>
#include <boost/log/utility/setup/settings.hpp>
#include <boost/log/utility/setup/from_settings.hpp>
#include <boost/regex.hpp>
namespace logging = boost::log;
namespace keywords = boost::log::keywords;
#define BOOST_LOG_DYN_LINK 1
int main(int, char*[])
{
//init_logging();
std::ifstream file("settings.ini");
logging::init_from_stream(file);
BOOST_LOG_TRIVIAL(trace) << "This is a trace severity message";
BOOST_LOG_TRIVIAL(debug) << "This is a debug severity message";
BOOST_LOG_TRIVIAL(info) << "This is an informational severity message";
BOOST_LOG_TRIVIAL(warning) << "This is a warning severity message";
BOOST_LOG_TRIVIAL(error) << …Run Code Online (Sandbox Code Playgroud)