Poco 日志记录 - 可以缓冲日志条目吗?

Poco logging - possible to buffer log entries?

我正在使用 FileChannel 和 AsyncChannel 通过 Poco 异步记录市场数据,这每秒会创建大量日志条目。 Poco 似乎将每条消息分别写入文件并且不进行缓冲。 我认为这给我的 HDD/filesystem 带来了相当大的压力,并且我遇到了我认为相关的应用程序崩溃。

有没有办法让 Poco 仅以 1Mb 的增量将日志保存到磁盘,并在记录器关闭时将缓冲区中剩余的所有内容写入文件?

另外,这是否有可能创建大量线程?根据我的阅读,AsyncChannel 只是将消息放入队列,所以我猜只创建了 1 个额外的线程?

以下基本上是我使用的代码:

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

class APocoClass
{
private:
    Poco::AutoPtr<Poco::FileChannel> pFileChannel;
    Poco::AutoPtr<Poco::PatternFormatter> pPF;
    Poco::AutoPtr<Poco::FormattingChannel> pFormattingChannel;
    Poco::AutoPtr<Poco::AsyncChannel> pFileChannelAsync;
    Poco::Logger & _pocoLogger;
public:

    APocoClass() :
        pFileChannel(new Poco::FileChannel()),
        pPF(new Poco::PatternFormatter("%Y%m%d %H:%M:%S.%F: %t")),
        pFormattingChannel(new Poco::FormattingChannel(pPF, pFileChannel)),
        pFileChannelAsync(new Poco::AsyncChannel(pFormattingChannel)),
        _pocoLogger(Poco::Logger::create("PocoLogger", pFileChannelAsync, Poco::Message::PRIO_INFORMATION))
    {

        pFileChannelAsync->setProperty("priority", "lowest");
        pFileChannel->setProperty("path", "MsgStorage/poco.log");
        pFileChannel->setProperty("rotation", "daily");
        pFileChannel->setProperty("times", "utc");
        pFileChannel->setProperty("archive", "timestamp");

    }

    ~APocoClass() {
        _pocoLogger.shutdown();
        _pocoLogger.close();
        pFileChannelAsync = nullptr;
        pFileChannel = nullptr;
    }

    //following is called every time we have a new market data message to log
    void MessageReceived(const string & message) {
        Poco::Message m("PocoLogger", message, Poco::Message::Priority::PRIO_INFORMATION);
        _pocoLogger.log(m);
    }

}

Is there anyway to have Poco only save the log to disk in say 1Mb increments, and also write anything remaining in the buffer to file on close of the logger?

您无法通过 FileChannel 进行如此精确的控制,但您可以使用 flush 属性(默认 true)确定是否在每个日志条目上刷新缓冲区.将其设置为 false,看看情况是否有所改善。

如果这不能满足您的性能要求,那么您可以选择编写自己的包装器,请参阅 LogStream for an example - obviously, you'll want to implement your own logic for LogStreamBuf::writeToDevice()。 (如果库允许你简单地传入你自己的 streambuf 会简单得多,但不幸的是它不允许。)

And separately, is there any chance this is going to create huge numbers of threads?

不,AsyncChannel 会 launch itself in a thread and process all notifications (ie. log messages) in that single thread