在boost::log
进入官方推送库之前(从1.54开始),我使用sourceforge version并使用以下代码:
boost::log::formatters::fmt_format<char> simpleFormat(
boost::log::formatters::format("%1% %2%") %
boost::log::formatters::date_time<boost::posix_time::ptime>(
"TimeStamp", boost::log::keywords::format = "%H:%M:%S") %
boost::log::formatters::message());
以及后来的:
log_sink->locked_backend()->set_formatter(simpleFormat)
但是,我根本无法弄清楚如何使用新的boost::log
API 使用流语法来表达上述日志记录格式(我想坚持上面的格式化样式语法) 。有什么想法吗?
更新:这是我得到的编译器错误(使用gcc 4.6.3):
> boost/boost/log/expressions/formatters/date_time.hpp: In constructor
> ‘boost::log::v2s_mt_posix::expressions::format_date_time_terminal<T,
> FallbackPolicyT, CharT>::format_date_time_terminal(const
> boost::log::v2s_mt_posix::attribute_name&, const fallback_policy&,
> const string_type&) [with T = boost::posix_time::ptime,
> FallbackPolicyT = boost::log::v2s_mt_posix::fallback_to_none, CharT =
> char,
> boost::log::v2s_mt_posix::expressions::format_date_time_terminal<T,
> FallbackPolicyT, CharT>::fallback_policy =
> boost::log::v2s_mt_posix::fallback_to_none,
> boost::log::v2s_mt_posix::expressions::format_date_time_terminal<T,
> FallbackPolicyT, CharT>::string_type = std::basic_string<char>]’:
> /boost/boost/log/expressions/formatters/date_time.hpp:229:94:
> instantiated from
> ‘boost::log::v2s_mt_posix::expressions::format_date_time_actor<AttributeValueT,
> boost::log::v2s_mt_posix::fallback_to_none, CharT>
> boost::log::v2s_mt_posix::expressions::format_date_time(const
> boost::log::v2s_mt_posix::attribute_name&, const CharT*) [with
> AttributeValueT = boost::posix_time::ptime, CharT = char]’
> /src/log.cc:169:102: instantiated from here
> /boost/boost/log/expressions/formatters/date_time.hpp:94:98: error:
> incomplete type
> ‘boost::log::v2s_mt_posix::expressions::format_date_time_terminal<boost::posix_time::ptime,
> boost::log::v2s_mt_posix::fallback_to_none, char>::formatter_generator
> {aka
> boost::log::v2s_mt_posix::expressions::aux::date_time_formatter_generator_traits<boost::posix_time::ptime,
> char, void>}’ used in nested name specifier compilation terminated due
> to -Wfatal-errors.
更新2 :我必须包含
#include <boost/log/support/date_time.hpp>
为了使其发挥作用。
答案 0 :(得分:2)
这就是你想要的吗?
您的案例示例
logging::formatter simpleFormat(expr::format("%1% %2%") %
expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%H:%M:%S") %
expr::smessage);
答案 1 :(得分:0)
您也可以使用以下代码格式化日志:
namespace keywords = boost::log::keywords;
namespace expr = boost::log::expressions;
logging::add_file_log
(
keywords::file_name = "Sample_%N.log", /*< file name pattern >*/
keywords::rotation_size = 1 * 1024, /*< rotate files every 1 KiB >*/
/*< log record format >*/
keywords::format =
(
expr::stream
//<< std::hex //To print the LineID in Hexadecimal format
<< std::setw(8) << std::setfill('0')
<< expr::attr< unsigned int >("LineID")
<< "\t"
<< expr::format_date_time<boost::posix_time::ptime>("TimeStamp","%H:%M:%S.%f")
<< "\t: <" << logging::trivial::severity
<< "> \t" << expr::smessage
)
);
输出就像:
00000741 15:50:11.636997 : <> Logger Starts
00000742 15:50:11.660012 : <> Thread74
00000743 15:50:11.682028 : <trace> Trace message new
00000744 15:50:11.703041 : <debug> Debug Message new
00000745 15:50:11.725055 : <info> Info message
00000746 15:50:11.748088 : <warning> warning Warning message new
00000747 15:50:11.770083 : <error> error An error message new
00000748 15:50:11.792100 : <fatal> fatal A fatal message new
00000749 15:50:11.818117 : <> Thread74
00000750 15:50:11.841148 : <> Logger Stops