【问题标题】:Poco logging - possible to buffer log entries?Poco 日志记录 - 可以缓冲日志条目吗?
【发布时间】:2017-11-08 02:41:52
【问题描述】:

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

有没有办法让 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);
    }

}

【问题讨论】:

    标签: multithreading logging poco-libraries


    【解决方案1】:

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

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

    如果这不能满足您的性能要求,那么您可以选择编写自己的包装器,参见 LogStream 的示例 - 显然,您需要为 LogStreamBuf::writeToDevice() 实现自己的逻辑。 (如果库允许您简单地传入自己的streambuf,那会简单得多,但不幸的是它没有。)

    另外,这是否有可能创建大量线程?

    不,AsyncChannel 将在一个线程中 launch itself 并处理 that single thread 中的所有通知(即日志消息)。

    【讨论】:

    • 太好了,我会尝试刷新属性。希望能改善它。
    猜你喜欢
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 2017-01-31
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多