Now that we have defined where and how the log is to be stored, it's time to go on and try logging. In order to do this one has to create a logging source. This would be a logger object in our case and it is as simple as that:
src::logger lg;
Note that unlike sinks, sources need not be registered anywhere, since they
interact directly with logging core. Also note that there are two versions
of loggers provided by the library: the thread-safe ones and the non-thread-safe
ones. For the non-thread-safe loggers it is safe for different threads to
write logs through different instances of loggers and thus there should be
a separate logger for each thread that writes logs. The thread-safe counterparts
may be accessed from different threads concurrently, but this would involve
locking and may slow things down in case of intense logging. The thread-safe
logger types have the _mt
suffix in their name.
Regardless of the thread safety, all loggers provided by the library are default and copy-constructible and support swapping, so there should be no problem in making a logger a member of your class. As you will see later, such approach can give you additional benefits.
The library provides a number of loggers with different features, such as severity and channel support. These features may be combined with each other in order to construct more complex loggers. See here for more datails.
In case you cannot put a logger into your class (suppose you don't have one), the library provides a way of declaring global loggers like this:
BOOST_LOG_DECLARE_GLOBAL_LOGGER(my_logger, src::logger_mt)
Here my_logger
is a user-defined
tag name that will be used later to retrieve the logger instance and logger_mt
is the logger type. Any logger
type provided by the library or defined by the user can be used in such declaration.
However, since the logger will have a single instance, you will normally
want to use thread-safe loggers in a multithreaded application as global
ones.
In case you want to pass some arguments to the logger on construction, you can use the following macro:
BOOST_LOG_DECLARE_GLOBAL_LOGGER_CTOR_ARGS(my_logger, src::logger_mt, (arg1)(arg2)(arg3))
or even write the initialization code yourself:
BOOST_LOG_DECLARE_GLOBAL_LOGGER_INIT(my_logger, src::logger_mt) { // do something on logger initialization and return logger instance return src::logger_mt(); }
You can have such declaration in either a header or a compiled cpp unit.
You can even have it in different modules of your application, however, the
declaration should be exactly the same in all places, including the definition
of the logger type (logger_mt
in this case) and all symbol bindings within the logger initialization body.
Later on you can acquire the logger like this:
src::logger_mt& lg = get_my_logger();
or like this:
src::logger_mt& lg = my_logger::get();
In any case, the lg
will
reference to the one and only instance of the logger throughout the application,
even if the application consists of multiple modules.
No matter what kind of logger you use (class member or global, thread-safe or not), to write a log record into a logger you could write something like this:
logging::record rec = lg.open_record(); if (rec) { rec.message() = "Hello world!"; lg.push_record(rec); }
Here the open_record
function
call determines if the record to be constructed is going to be consumed by
at least one sink. Filtering is applied at this stage. If the record is to
be consumed, the function returns a valid record object, and one may fill
in the record message string. After that the record processing can be completed
with the call to push_record
.
Of course, the above syntax can easily be wrapped in a macro and, in fact, users are encouraged to write their own macros instead of using the C++ logger interface directly. The log record above can be written like this:
#include <boost/log/sources/record_ostream.hpp> BOOST_LOG(lg) << "Hello, World!";
Looks a bit shorter, doesn't it? The BOOST_LOG
macro, along with other similar ones, is defined by the library. It automatically
provides an STL-like stream in order to format the message with ordinary
insertion expressions. Having all that code written, compiled and executed
you should be able to see the "Hello, World!" record in the "sample.log"
file.