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

时间:2017-11-08 02:41:53

标签: multithreading logging poco-libraries

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

是否有Poco只以1Mb为增量将日志保存到磁盘,并在记录器关闭时将剩余的任何内容写入文件?

另外,这是否有可能创造大量的线程?从我所看到的AsyncChannel只是将消息放入队列,所以我想只创建了一个额外的线程?

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

#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);
    }

}

1 个答案:

答案 0 :(得分:1)

  

是否有Poco只以1Mb为增量将日志保存到磁盘,并在记录器关闭时将剩余的任何内容写入文件?

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

如果这不满足您的性能要求,那么您可以选择编写自己的包装器,请参阅LogStream以获取示例 - 显然,您希望为{{3}实现自己的逻辑}}。 (如果图书馆允许你简单地传递你自己的streambuf会更简单,但遗憾的是它没有。)

  

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

不,AsyncChannel将LogStreamBuf::writeToDevice()在一个线程中处理launch itself中的所有通知(即日志消息)。