gpt4 book ai didi

c++ - Poco Logger 更改 Message 参数

转载 作者:行者123 更新时间:2023-11-30 03:48:16 26 4
gpt4 key购买 nike

我正在使用 Poco::Logger 来跟踪我的应用程序中的日志。

现在我想在我的所有日​​志中跟踪一个变量的值。因为我不想更改之前输出行的每一行。我想让它成为格式中的默认值。

查阅了Poco日志系统的文档和教程,发现:

  • A message can store an arbitrary number of name-value pairs.
  • Names and values can be arbitrary strings.
  • Message parameters can be referenced in a formatter.

这看起来正是我要找的。但是我找不到关于此的详细信息,似乎 Poco::logger 自己创建了一个 Poco::Message。

有什么办法可以实现我想做的事情吗?谢谢。

最佳答案

#include "Poco/Logger.h"
#include "Poco/AutoPtr.h"
#include "Poco/PatternFormatter.h"
#include "Poco/FormattingChannel.h"
#include "Poco/ConsoleChannel.h"
#include "Poco/Message.h"

using namespace Poco;

int main()
{
Message msg;
msg.setSource("MySource");
msg.setText("My message text");
msg["myParam"] = "My Param";

AutoPtr<FormattingChannel> pFCConsole = new FormattingChannel(new PatternFormatter("%s: %[myParam], %t"));
pFCConsole->setChannel(new ConsoleChannel);
pFCConsole->open();

Logger& consoleLogger = Logger::root(); // or whatever your logger is
consoleLogger.setChannel(pFCConsole);
consoleLogger.log(msg);

return 0;
}

运行:

$ ./Logger
MySource: My Param, My message text

目前仅支持std::string 自定义参数,因此如果值为数字,则必须进行转换。

关于c++ - Poco Logger 更改 Message 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33277881/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com