What I"ve just done is define a logger with an attribute in each class:
#define DECLARE_LOGGER(name) static ::youtils::Logger::logger_type& getLogger__() \
{ \
static youtils::SeverityLoggerWithAttribute logger(name); \
return logger.get(); \
}
Where:
class SeverityLoggerWithAttribute
{
public:
SeverityLoggerWithAttribute(const std::string& name,
const std::string& /*subsystem*/ = "no subsystem")
{
logger_.add_attribute(LOGGER_ATTRIBUTE_ID,
boost::log::attributes::constant<LOGGER_ATTRIBUTE_ID_TYPE>(name));
// logger_.add_attribute(LOGGER_ATTRIBUTE_SUBSYSTEM,
// boost::log::attributes::constant<LOGGER_ATTRIBUTE_SUBSYSTEM_TYPE>(subsystem));
}
inline Logger::logger_type&
get()
{
return logger_;
}
private:
Logger::logger_type logger_;
};
Then I wrote some macros:
#define LOG_TYPE(type, message) \
BOOST_LOG_SEV(getLogger__(), type) << __FUNCTION__ << ": " << message
#define LOG_NOTIFY(message) LOG_TYPE(youtils::Severity::notification, message)
So long as getLogger__ is in scope and returns the correct logger you can just LOG_XXX(whatever).
You add DECLARE_LOGGER("xxx") to the class and all the member functions will do the correct
logging. A mixin class that contains a logger would work equally well -- or better -- but i had to stay
close to an existing architecture.
So the idea is
* use different loggers
* make sure the LOG statements find the correct logger
* Add the attribute to the logger once, and each log record will see it automatically.
Regards,
i