从Java转到c++后,一直对log4j念念不忘,尝试过log4cplus,可能是版本选择的问题,程序中时常出现因为log而崩溃的问题,后来发现boost.log库出了,而项目中boost库基本是必用的,所以直接拿来用了。
boost.log库的介绍就不在这里啰嗦了,详见官网:http://www.boost.org/doc/libs/1_55_0/libs/log/doc/html/index.html
这里简单写一下初步的使用代码:
我将对log库的调用封装到一个文件里:
logger.h
#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/common.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/expressions/keyword.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/attributes/timer.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/sources/severity_feature.hpp> //
#include <boost/log/sinks.hpp>//
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/formatter_parser.hpp>//
#include <boost/log/attributes/named_scope.hpp>
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 SeverityLevel
{
trace = 0,
debug,
info,
warn,
error,
fatal
};
template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (
std::basic_ostream< CharT, TraitsT >& strm, SeverityLevel lvl)
{
static const char* const str[] =
{
"TRACE",
"DEBUG",
"INFO",
"WARN",
"ERROR",
"FATAL"
};
if (static_cast<std::size_t>(lvl) < (sizeof(str) / sizeof(*str)))
strm << str[lvl];
else
strm << static_cast<int>(lvl);
return strm;
}
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(vdsu_logger, src::severity_logger_mt<SeverityLevel>);
BOOST_LOG_ATTRIBUTE_KEYWORD(log_severity, "Severity", SeverityLevel)
BOOST_LOG_ATTRIBUTE_KEYWORD(log_timestamp, "TimeStamp", boost::posix_time::ptime)
BOOST_LOG_ATTRIBUTE_KEYWORD(log_uptime, "Uptime", attrs::timer::value_type)
BOOST_LOG_ATTRIBUTE_KEYWORD(log_scope, "Scope", attrs::named_scope::value_type)
BOOST_LOG_ATTRIBUTE_KEYWORD(log_threadId, "ThreadID", attrs::current_thread_id::value_type)
typedef src::severity_logger_mt<SeverityLevel> vdsu_logger_mt;
void g_InitLog(SeverityLevel file_level, SeverityLevel console_level);
然后是cpp文件:
#include "logger.h"
void g_InitLog(SeverityLevel file_level, SeverityLevel console_level)
{
logging::formatter formatter_file =
expr::stream
<< "[" << expr::format_date_time(log_timestamp, "%Y-%m-%d %H:%M:%S")
<< "]" << expr::if_(expr::has_attr(log_uptime))
[
expr::stream << " [" << format_date_time(log_uptime, "%O:%M:%S") << "]"
]
<< expr::if_(expr::has_attr(log_scope))
[
expr::stream << "[" << expr::format_named_scope(log_scope, keywords::format = "%n (%f : %l)") << "]"
]
<< "[" << log_threadId << "][" << log_severity << "]" << expr::message;
logging::formatter formatter_console =
expr::stream
<< "[" << expr::format_date_time(log_timestamp, "%H:%M:%S")
<< "]" << expr::if_(expr::has_attr(log_uptime))
[
expr::stream << " [" << format_date_time(log_uptime, "%O:%M:%S") << "]"
]
<< "[" << log_severity << "]:" << expr::message;
logging::add_common_attributes();
auto console_sink = logging::add_console_log();
auto file_sink = logging::add_file_log
(
keywords::file_name = "%Y-%m-%d_%N.log", //文件名
keywords::rotation_size = 10 * 1024 * 1024, //单个文件限制大小
keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0) //每天重建
);
file_sink->locked_backend()->set_file_collector(sinks::file::make_collector(
keywords::target = "logs", //文件夹名
keywords::max_size = 50 * 1024 * 1024, //文件夹所占最大空间
keywords::min_free_space = 100 * 1024 * 1024 //磁盘最小预留空间
));
file_sink->set_filter(log_severity >= file_level); //日志级别过滤
file_sink->locked_backend()->scan_for_files();
console_sink->set_formatter(formatter_console);
console_sink->set_filter(log_severity >= console_level);
file_sink->set_formatter(formatter_file);
//file_sink->locked_backend()->auto_flush(true);//立刻写日志
//logging::core::get()->add_global_attribute("Scope", attrs::named_scope());
logging::core::get()->add_global_attribute("ThreadID", attrs::current_thread_id());
logging::core::get()->add_sink(console_sink);
logging::core::get()->add_sink(file_sink);
}
下面是用法:
首先在项目的main函数里对日志库进行初始化
//初始化日志库
g_InitLog(debug, debug);
两个参数的含义是指文件日志的级别,和控制台日志的级别。
然后在需要写日志的地方首先调用:
vdsu_logger_mt& vlg = vdsu_logger::get();
然后调用:
BOOST_LOG_SEV(vlg, info) << "Hello World!";
来写日志。
基本的用法就是这些,如果想进行更复杂的用法,请研究官网的Tutorial.