简体   繁体   English

如何转义 spdlog 消息?

[英]How to escape spdlog message?

I'm using spdlog to log all messages.我正在使用 spdlog 记录所有消息。 My log pattern is a JSON format.我的日志模式是 JSON 格式。 I'm escaping messages manually in all log calls.我在所有日志调用中手动发送 escaping 消息。 Is there any way exists to escape messages automatically in spdlog layer?有没有办法在 spdlog 层自动转义消息?

Sample of manually escaping:手动 escaping 示例:

spdlog::info(escape_message(data));

It should be automatically handled inside spdlog layer:它应该在 spdlog 层内自动处理:

spdlog::info(data);

Tell spdlog to format a single string, rather than interpreting the string itself as the format string:告诉 spdlog 格式化单个字符串,而不是将字符串本身解释为格式字符串:

spdlog::info("{}", data);

I've found a way to solve it.我找到了解决它的方法。

I should implement a custom formatter class and then in the format method, I escape the message by for example escape_message(msg.payload.data()) and then call spdlog::log(msg.source, msg.level, escape_message(msg.payload.data())) , spdlog::info or... inside this method.我应该实现一个自定义formatter class 然后在format方法中,我通过例如escape_message(msg.payload.data())转义消息,然后调用spdlog::log(msg.source, msg.level, escape_message(msg.payload.data())) , spdlog::info或... 在这个方法中。

Note : inside the format method I set a log pattern, because outside of the class, if I call set_formatter , then calling set_pattern , doesn't work well.注意:在format方法中我设置了一个日志模式,因为在 class 之外,如果我调用set_formatter ,然后调用set_pattern ,则效果不佳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM