Friday 27 December 2013

use boost log step 11

Today, I will apply rotation log here. To use rotation, the backend needs to be replaced by text_file_backend instead of text_ostream_backend.
Use keywords to set the following attributes when the backend is constructed.
file_name, rotation_size, time_based_rotation and min_free_space

Code in the logger.cc file
#include "logger.h"
void InitLog() {
  typedef sinks::synchronous_sink<sinks::text_file_backend> TextSink;
 
  // init sink1
  boost::shared_ptr<sinks::text_file_backend> backend1 = boost::make_shared<sinks::text_file_backend>(
 keywords::file_name = "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::min_free_space = 30 * 1024 * 1024);
  boost::shared_ptr<TextSink> sink1(new TextSink(backend1));
  sink1->set_formatter (
expr::format("[%1%]<%2%>(%3%): %4%")
% expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S")
% expr::attr<sign_severity_level>("Severity")
% expr::attr<attrs::current_thread_id::value_type >("ThreadID")
% expr::smessage
);
  sink1->set_filter(expr::attr<sign_severity_level>("Severity") >= warning);
  logging::core::get()->add_sink(sink1);

  // init sink2
  boost::shared_ptr<sinks::text_file_backend> backend2 = boost::make_shared<sinks::text_file_backend>(
 keywords::file_name = "sign_%Y-%m-%d.csv",
 keywords::rotation_size = 100 * 1024 * 1024,
 keywords::time_based_rotation = sinks::file::rotation_at_time_point(boost::gregorian::greg_day(1), 0, 0, 0),
 keywords::min_free_space = 30 * 1024 * 1024);
  backend2->auto_flush(true);
  boost::shared_ptr<TextSink> sink2(new TextSink(backend2));
  sink2->set_formatter (
expr::format("%1%,%2%")
% expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S")
% expr::smessage
);
  sink2->set_filter(expr::attr<sign_severity_level>("Severity") == report);
  logging::core::get()->add_sink(sink2);
  logging::add_common_attributes();
  logging::core::get()->add_global_attribute("ThreadID", attrs::current_thread_id());
}
logger.h file, it is not changed.
#include <boost/log/sinks.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/formatter_parser.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/severity_feature.hpp>
#include <fstream>
namespace logging = boost::log;
namespace attrs = boost::log::attributes;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace expr = boost::log::expressions;
namespace keywords = boost::log::keywords;
enum sign_severity_level {
  trace,
  debug,
  info,
  warning,
  error,
  fatal,
  report
};
void InitLog();
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::severity_logger_mt<sign_severity_level>)
Note, sink1 and sink2 use different rotation log policy.
sink2 generate a new log file on the 1-st of every month

No comments:

Followers

Contributors