时间戳和的ThreadID如何输出升压定制属性::日志格式? [英] How to output TimeStamp and ThreadID attributes with custom boost::log formatter?

查看:375
本文介绍了时间戳和的ThreadID如何输出升压定制属性::日志格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用自定义的提振::登录格式化颜色编码输出日志消息,但我没能找到合适的方法来添加的时间戳的ThreadID 的属性到日志中。当我使用的文件记录我只是写关键词:格式=[%时间戳%] [%的ThreadID%] [%严重性%]:%讯息%作为记录:: add_file_log 参数。我想在下面的自定义格式类似的效果:

 无效coloring_formatter(常量记录:: record_view&放大器;记录,
                        记录:: formatting_ostream&安培;流)
{
  自动严重性=记录[记录::琐碎::严重性]
  断言(严重);  流<< \\ E [1分;  开关(severity.get())
  {
  案件记录::琐碎:: severity_level ::跟踪:
    流<< \\ E [97米
    打破;
  案件记录::琐碎:: severity_level ::调试:
    流<< \\ E [34米
    打破;
  案件记录::琐碎:: severity_level ::信息:
    流<< \\ E [32米
    打破;
  案件记录::琐碎:: severity_level ::警告:
    流<< \\ E [93米
    打破;
  案件记录::琐碎:: severity_level ::错误:
    流<< \\ E [91米
    打破;
  案件记录::琐碎:: severity_level ::致命的:
    流<< \\ E [41米
    打破;
  }  流//<<输出时间戳
         //<<输出的ThreadID
         << [<<严重程度和LT;< ]
         <<记录[记录::前pressions :: smessage]
         << \\ E [0米
}


解决方案

既然你已经添加了这些属性日志记录,有多种方法来提取它们。最简单的方法是使用<一href=\"http://www.boost.org/doc/libs/1_61_0/libs/log/doc/html/log/detailed/ex$p$pssions.html#log.detailed.ex$p$pssions.attr_keywords\"相对=nofollow>关键字。首先,你的属性声明的关键字:

  BOOST_LOG_ATTRIBUTE_KEYWORD(a_timestamp,时间戳,ATTRS :: local_clock :: VALUE_TYPE)
BOOST_LOG_ATTRIBUTE_KEYWORD(a_thread_id的ThreadID,ATTRS :: current_thread_id :: VALUE_TYPE)

请注意,该第三个参数是对应的属性的值的类型。在这个例子中,我假设您使用的属性是<一个href=\"http://www.boost.org/doc/libs/1_61_0/libs/log/doc/html/log/detailed/attributes.html#log.detailed.attributes.clock\"相对=nofollow> local_clock 和<一个href=\"http://www.boost.org/doc/libs/1_61_0/libs/log/doc/html/log/detailed/attributes.html#log.detailed.attributes.thread_id\"相对=nofollow> current_thread_id

现在,您可以使用这些关键字从日志记录提取值。

 流&LT;&LT;记录[a_timestamp&LT;&LT; 
       &LT;&LT;记录[a_thread_id]
       &LT;&LT; [&LT;&LT;严重程度和LT;&LT; ]
       &LT;&LT;记录[记录::前pressions :: smessage]
       &LT;&LT; \\ E [0米

I'm using custom boost::log formatter for color coding the output log message, but I'm failing to find the proper way to add TimeStamp and ThreadID attributes to the log. When I'm using file logging I just write keywords::format = "[%TimeStamp%] [%ThreadID%] [%Severity%]: %Message%" as logging::add_file_log parameter. I want to have similar effect in the following custom formatter:

void coloring_formatter(const logging::record_view& record,
                        logging::formatting_ostream& stream)
{
  auto severity = record[logging::trivial::severity];
  assert(severity);

  stream << "\e[1m";

  switch (severity.get())
  {
  case logging::trivial::severity_level::trace:
    stream << "\e[97m";
    break;
  case logging::trivial::severity_level::debug:
    stream << "\e[34m";
    break;
  case logging::trivial::severity_level::info:
    stream << "\e[32m";
    break;
  case logging::trivial::severity_level::warning:
    stream << "\e[93m";
    break;
  case logging::trivial::severity_level::error:
    stream << "\e[91m";
    break;
  case logging::trivial::severity_level::fatal:
    stream << "\e[41m";
    break;
  }

  stream // << output TimeStamp
         // << output ThreadID
         << "[" << severity << "] "
         << record[logging::expressions::smessage]
         << "\e[0m";
}

解决方案

Given that you have added these attributes to log records, there are multiple ways to extract them. The simplest way is to use keywords. First you declare keywords for the attributes:

BOOST_LOG_ATTRIBUTE_KEYWORD(a_timestamp, "TimeStamp", attrs::local_clock::value_type)
BOOST_LOG_ATTRIBUTE_KEYWORD(a_thread_id, "ThreadID", attrs::current_thread_id::value_type)

Note that the third parameter is the value type of the corresponding attribute. In this example I assumed that the attributes you used were local_clock and current_thread_id.

Now you can use these keywords to extract the value from log records.

stream << record[a_timestamp] << " "
       << record[a_thread_id]
       << " [" << severity << "] "
       << record[logging::expressions::smessage]
       << "\e[0m";

这篇关于时间戳和的ThreadID如何输出升压定制属性::日志格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆