[log] connecting a logger to a specific sink
Hello, I'm integrating Boost.Log to my project, and have a couple of questions. First of all, I'd like each instance of some class to write its log to a separate rotating file. I understand how to setup such a sink, but can't find a way to connect a particular source (logger) to the sink. Is it possible? Besides, when setting up the file, I configure it with: keywords::format = "[%TimeStamp%]: %Message%" but %TimeStamp% doesn't seem to get recognized, so I get empty [ ] in the file. Do I miss some header file? Thanks.
I'm not yet a boost log expert but, I think what you need to do is to add filters to your sinks so that they only take the log record that they are supposed to write. Basically, boost log is built as a hub: n log-record -> 1 core -> n sinks So if you prefer something like that: log-record A -> core -> sinks A log-record B -> core -> sinks B log-record C -> core -> sinks C Then it's not directly supported at the moment (Andrey correct me if I'm wrong). So you have to do 2 things: - add the information of which kind of log is a log-record - add filters to each sinks so that they take only log-records of a specific kind I'm abstract on purpose because there are several ways to define kinds of logs (basically any comparable data will do).
I think what you need to do is to add filters to your sinks so that they only take the log record that they are supposed to write. Basically, boost log is built as a hub:
n log-record -> 1 core -> n sinks
So if you prefer something like that:
log-record A -> core -> sinks A log-record B -> core -> sinks B log-record C -> core -> sinks C
Then it's not directly supported at the moment (Andrey correct me if I'm wrong). So you have to do 2 things: - add the information of which kind of log is a log-record - add filters to each sinks so that they take only log-records of a specific kind
Thanks, I see. I'm currently trying to figure out how to configure such a filtering without overcomplicating the whole task. Currently, I'm passing different logger objects to different instances of my class, and I wouldn't like those instances to perform any additional log-related actions, besides BOOST_LOG(logger) << "some message"; So if it were possible to associate some attribute value with each logger, that would probably solve my problem: I could create as much sinks as I need, while filtering every sink like this: sink->set_filter(expr::has_attr(my_attr) && my_attr == n); Is it possible to "link" an attribute value to a logger?
I think you need to read the attribute page in details but you mostly get it apparently. http://boost-log.sourceforge.net/libs/log/doc/html/log/tutorial/attributes.h... Basically, you don't associate attribute to loggers but to log record. In your case I think the simplest way would be to have a different logging macro (or function), that would do the log record setup and send manually but will add necessary attributes in the middle. See the example of manual logging (or see the boost log macros implementations): void manual_logging() { src::severity_logger< severity_level > slg; logging::record rec = slg.open_record(keywords::severity = normal); /// HERE ADD NECESSARY ATTRIBUTE THAT WILL BE FILTERED BY SINKS if (rec) { logging::record_ostream strm(rec); strm << "A regular message"; strm.flush(); slg.push_record(boost::move(rec)); } }
I think you need to read the attribute page in details but you mostly get it apparently. http://boost-log.sourceforge.net/libs/log/doc/html/log/tutorial/attributes.h...
Basically, you don't associate attribute to loggers but to log record. In your case I think the simplest way would be to have a different logging macro (or function), that would do the log record setup and send manually but will add necessary attributes in the middle.
That's the approach I'd like to avoid, due to some legacy code limitations (I'm moving to Boost.Log from some other logger). But it turns out that it's possible to attach attributes to loggers in a straightforward way: BOOST_LOG_ATTRIBUTE_KEYWORD(my_attr, "my_attr", int) logger->add_attribute("my_attr", attrs::constant<int>(i)); It works as I expected and solves my problem. Thanks.
participants (2)
-
Igor R
-
Klaim - Joël Lamotte