Tuesday 3 December 2013

use boost log step 4

Now, I apply the boost log to my one of existing applications to replace CppCMS log. I use it to do the following jobs, the size of one single log file is limited less than 10 MB, and the free disk space must be 3GB.
To use this, there are some issues need to be known.
1. include correct header files

#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
2. register_simple_formatter_factory is required, otherwise you cannot see severity field in log file
void InitLog() {
  boost::log::register_simple_formatter_factory< boost::log::trivial::severity_level, char >("Severity");
  logging::add_file_log(
keywords::file_name = AppHolder::Instance().config().log_folder + "/sign_%Y-%m-%d_%H-%M-%S.%N.log",
keywords::rotation_size = 10 * 1024 * 1024,
keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
keywords::format = "[%TimeStamp%] (%Severity%) : %Message%",
keywords::min_free_space=3 * 1024 * 1024
);
  logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::debug);
}

3.  In main function, set attributes after calling InitLog like this:
    InitLog();
    logging::add_common_attributes();
    using namespace logging::trivial;
    src::severity_logger< severity_level > lg;
    BOOST_LOG_SEV(lg, info) << "thread id: " << this_thread::get_id() << " Initialization succeeded";

Now, have a look at the log files, each file size is less than 10M, and one new file will be created at midnight.

 -rw-r--r-- 1 root root  10M Dec  3 23:16 sign_2013-12-03_23-00-01.0.log
-rw-r--r-- 1 root root  10M Dec  3 23:36 sign_2013-12-03_23-16-55.1.log
-rw-r--r-- 1 root root  10M Dec  3 23:55 sign_2013-12-03_23-36-21.2.log
-rw-r--r-- 1 root root 2.4M Dec  4 00:00 sign_2013-12-03_23-55-33.3.log
-rw-r--r-- 1 root root  10M Dec  4 00:19 sign_2013-12-04_00-00-00.4.log
-rw-r--r-- 1 root root  10M Dec  4 00:38 sign_2013-12-04_00-19-30.5.log
-rw-r--r-- 1 root root  10M Dec  4 00:58 sign_2013-12-04_00-38-48.6.log
-rw-r--r-- 1 root root  10M Dec  4 01:17 sign_2013-12-04_00-58-06.7.log
-rw-r--r-- 1 root root  10M Dec  4 01:36 sign_2013-12-04_01-17-19.8.log
-rw-r--r-- 1 root root  10M Dec  4 01:56 sign_2013-12-04_01-36-45.9.log
-rw-r--r-- 1 root root  10M Dec  4 02:15 sign_2013-12-04_01-56-07.10.log

Look inside one log file:
[2013-Dec-04 10:17:45.728393] (debug) : object id: 529df217186f983f62ffa718 sent data: 01 30 30 30 43 44 31 4F 4B 31 30 03  sent size:12

4.  In other .cc files, just include one header file
 #include <boost/log/trivial.hpp>
And use BOOST_LOG_TRIVIAL() MACROS
BOOST_LOG_TRIVIAL(debug) << "object id: " << id_ << " sent data: " << PrintBytesAsHexString(*data, data->size()) << " sent size:" << data->size();

That's enough.
Inside boost log, there are many details need to be learned. I have not time for now to analyze it. Will write blogs about this when free in the future.

No comments:

Followers

Contributors