将 boost 日志与 FastCGI 结合使用

Using boost log with FastCGI

我正在尝试在我的 FastCGI 程序中使用 boost log,但看起来它们不能一起工作。

当我直接执行程序时,

./sample_log

我可以看到记录到文件中的日志消息。

当我使用 spawn-fcgi 启动程序时,

sudo spawn-fcgi -p 8000 -n sample_log

没有记录任何消息。

这是我正在使用的简单程序:

#include <boost/log/trivial.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/from_stream.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/dynamic_bitset.hpp>

#include <iostream>
#include <fstream>

using namespace std;

namespace logging = boost::log;
namespace attrs = boost::log::attributes;
namespace sinks = boost::log::sinks;
namespace expr = boost::log::expressions;
namespace trivial = boost::log::trivial;
namespace src = boost::log::sources;
namespace keywords = boost::log::keywords;

int main()
{
    logging::add_file_log
        (
            keywords::file_name = "sample.log",
            // This makes the sink to write log records that look like this:
            // 1: <normal> A normal severity message
            // 2: <error> An error severity message
            keywords::format =
            (
                expr::stream
                << expr::attr< unsigned int >("LineID") << " |"
                << " [ " << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S") << "]"
                << ": <" << logging::trivial::severity << "> "
                << expr::smessage
            )
        );

    logging::add_common_attributes();

    using namespace logging::trivial;
    src::severity_logger< severity_level > lg;

    LOG(lg, trace) << "A trace severity message";
    LOG(lg, debug) << "A debug severity message";
    LOG(lg, info) << "An informational severity message";
    LOG(lg, warning) << "A warning severity message";
    LOG(lg, error) << "An error severity message";
    LOG(lg, fatal) << "A fatal severity message";

    FCGX_Request request;

    FCGX_Init();
    FCGX_InitRequest(&request, 0, 0);

    while (FCGX_Accept_r(&request) == 0) {
        ......
    }

    return;
}

我必须设置 keywords::auto_flush = 真,

使用 fcgi 进行日志记录。