Home > Software engineering >  Boost log - include object id into logs
Boost log - include object id into logs

Time:10-24

I'm currently getting started with boost.log and have a question about how to incorporate object id into the logs. So for instance:


class Sample
{
    size_t id() const; // ...

    void someMethod() {
        // log here, see id() in a message
    }
};

So, each object has it's own id and I want to see it in the logs. Here are the approaches I came up with:

  1. create logger object per instance. Each logger will have a constant attribute id. While it should work, I don't think it's a good approach from performance perspective.
  2. one logger per class, write a logger feature that will add and remove id at every call (sth similar to this: https://www.boost.org/doc/libs/1_80_0/libs/log/doc/html/log/extension/sources.html). Here my concern is locking (I am not sure whether the logs will not block when mutliple instances are in different threads)
  3. include id as a part of log message. While being the most straightforward, it does not offer formatting capabilities.

So each of the 3 approaches is not perfect. Perhaps I'm missing something? Thank Your for Your time!

P.S. I don't understand why I cannot provide an attribute when doing logger.open_record call, for me it's not intuitive at all.

CodePudding user response:

If your object id doesn't change throughout the object lifetime, you do not need to add and remove the id on every log record. You can just add it to the logger once in the logger constructor. You don't even need a new logger feature to do this as every logger supports a set of logger-specific attributes.

// Define an attribute keyword for use in filters and formatters
BOOST_LOG_ATTRIBUTE_KEYWORD(a_object_id, "ObjectId", size_t)

class Sample
{
private:
    boost::log::logger m_log;

public:
    Sample()
    {
        m_log.add_attribute(tag::a_object_id::get_name(),
            boost::log::attributes::make_constant(id()));
    }

    size_t id() const;

    void someMethod() {
        // Every log record made through m_log will have ObjectId attached
        BOOST_LOG(m_log) << "someMethod() called";
    }
};

Remember that in order to actually see the attribute value in the output you need to set the formatter that will produce that attribute value in the formatted message.

sink->set_formatter(boost::log::expressions::stream
    << "[" << a_object_id << "] "
    << boost::log::expressions::smessage);
  • Related