fastcgiboost-logboost-loggingspawn-fcgi

Using boost log with FastCGI


I am trying to use boost log in my FastCGI program, but looks they don't work together.

When I execute the program directly,

./sample_log

I can see logging messages logged into the file.

When I launch the program using spawn-fcgi,

sudo spawn-fcgi -p 8000 -n sample_log

NO message logged.

Here is the simple program I am using:

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

Solution

  • I have to set the keywords::auto_flush = true,

    to get the logging work with fcgi.