Skip to content

Commit

Permalink
add elapsed microseconds to the access log
Browse files Browse the repository at this point in the history
  • Loading branch information
eklitzke committed May 15, 2012
1 parent 0c33f32 commit 702d235
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/request.cc
Expand Up @@ -2,7 +2,10 @@

#include "./request.h"

#include <boost/date_time/posix_time/posix_time.hpp>

namespace garfield {
Request::Request() {
Request::Request()
:connection_time_(boost::posix_time::microsec_clock::universal_time()) {
}
}
4 changes: 2 additions & 2 deletions src/request.h
Expand Up @@ -25,7 +25,7 @@ class Request {
std::string peername;

// Get the connection time.
const boost::posix_time::microsec_clock& connection_time() const {
const boost::posix_time::ptime& connection_time() const {
return connection_time_;
}

Expand All @@ -34,7 +34,7 @@ class Request {
boost::asio::streambuf streambuf;

private:
boost::posix_time::microsec_clock connection_time_;
boost::posix_time::ptime connection_time_;
HeadersDict headers_;
};
}
Expand Down
5 changes: 4 additions & 1 deletion src/server.cc
Expand Up @@ -116,19 +116,22 @@ void HTTPServer::OnRequest(Connection *conn, Request *req, RequestError err) {
std::placeholders::_1,
std::placeholders::_2));

boost::posix_time::time_duration td = (
boost::posix_time::microsec_clock::universal_time()- req->connection_time());
boost::local_time::local_time_facet *facet(
new boost::local_time::local_time_facet("%Y-%m-%d %H:%M:%S.%f"));
std::stringstream date_stream;
date_stream.imbue(std::locale(date_stream.getloc(), facet));
date_stream << boost::local_time::local_microsec_clock::local_time(boost::local_time::time_zone_ptr());
Log(ACCESS, "%s [%s] \"%s %s HTTP/1.%d\" %d %zd \"%s\" \"%s\"",
Log(ACCESS, "%s [%s] \"%s %s HTTP/1.%d\" %d %zd %ld \"%s\" \"%s\"",
req->peername.c_str(),
date_stream.str().c_str(),
req->method.c_str(),
req->path.c_str(),
req->version.second,
resp->status(),
expected_size,
td.total_microseconds(),
FmtLogField(req->headers()->GetHeader("Referer")),
FmtLogField(req->headers()->GetHeader("User-Agent")));
}
Expand Down

0 comments on commit 702d235

Please sign in to comment.