如何将严重性与升压日志中的相同宽度保持一致
How to align severity to same width in Boost log
例如,'info'和'警告'具有不同的宽度,在日志中看起来并不是预备。我想在日志中对齐它们相同的宽度。
看来我可以使用本文所述使用自定义的格式化工厂:带有logging :: init_from_stream
的Boost日志格式单属性这是使用自定义严重性级别的另一个解决方案:如何使用格式字符串
格式化自定义afterity_level除此之外,是否有一种更简单的方法来通过自定义格式字符串(例如printf)来实现此目标?
您可以尝试将格式化设置为
之类的东西expr::stream << std::left << std::setw(7) << std::setfill(' ') <<
logging::trivial::severity << " " << expr::smessage;
7的宽度来自最大可能的长度("警告")。如果您使用自定义严重性级别,则应更改它。
在这里,一个更完整的示例(为简单起见而省略了标题文件):
namespace logging = boost::log;
namespace expr = boost::log::expressions;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
using namespace logging::trivial;
logging::sources::severity_logger<severity_level> s_log;
void init_logging(const std::string& log_dir)
{
boost::shared_ptr<logging::core> core = logging::core::get();
boost::shared_ptr<sinks::text_file_backend> backend =
boost::make_shared<sinks::text_file_backend>(
keywords::file_name = log_dir + "/l_%Y-%m-%d_%5N.log", // one file per date and execution
keywords::rotation_size = 512 * 1024); // rotate after 512KB
// Collect clashing logs
backend->set_file_collector(sinks::file::make_collector(keywords::target = log_dir));
backend->scan_for_files();
typedef sinks::synchronous_sink<sinks::text_file_backend> sink_t;
boost::shared_ptr<sink_t> sink(new sink_t(backend));
sink->locked_backend()->auto_flush(true);
sink->set_formatter(expr::stream <<
"[" << expr::attr<boost::posix_time::ptime>("TimeStamp") << "] " <<
std::left << std::setw(7) << std::setfill(' ') << logging::trivial::severity << " " <<
expr::smessage);
core->add_sink(sink);
s_log.add_attribute("TimeStamp", logging::attributes::local_clock());
}
相关文章:
- 大于65535的C++数组[size]引发不一致的溢出
- 我可以信任表示整数的浮点或双精度来保持精度吗
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- Constexpr替代了新的放置方式,可以让内存中的对象保持未初始化状态
- 在多次运行中获得一致的callgrind输出
- 缓存std::数组的选定元素,并在c++中自动保持其一致性
- 在 C++(和 C)中进行类型转换时明显不一致
- 如何在保持其值的同时将 c++ 无符号字符变量转换为 char 变量
- 是否可以配置提升日志刷新?
- 向量保持复数的运算符重载
- 我似乎无法获得在两个.cpp文件中保持一致的值
- C 将5个字节结构与Cacheline保持一致
- Visual Studio:热键/自动对.h中的函数进行排序以与.cpp保持一致的方式
- 如何将严重性与升压日志中的相同宽度保持一致
- 只要应用程序运行,是否可以接受保持错误日志打开状态
- 互斥锁如何确保变量的值在内核之间保持一致?
- 安装前后与CMake保持一致的配置文件路径
- 返回后无法保持动态分配的内存一致
- 如何在保持使用一致的同时划分责任
- c++测试验证相等操作符随时间保持与结构体一致