Skip to content

Commit

Permalink
feat(OSPWeb): When full request logging is enabled, log request as so…
Browse files Browse the repository at this point in the history
…on as it comes in. Improve formatting.
  • Loading branch information
obiltschnig committed Feb 9, 2024
1 parent 4d3770f commit 98a0b9c
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions platform/OSP/Web/src/WebServerDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,17 @@ void WebServerDispatcher::handleRequest(Poco::Net::HTTPServerRequest& request, P
std::string username;
try
{
if (_logFullRequest && _pContext->logger().information())
{
std::ostringstream ostr;
ostr << "Request from " << request.clientAddress().toString() << ":\n\t";
request.write(ostr);
std::string message(ostr.str());
Poco::trimRightInPlace(message);
Poco::replaceInPlace(message, "\r\n"s, "\n\t"s);
_pContext->logger().information(message);
}

addCustomResponseHeaders(response);

URI uri(request.getURI());
Expand Down Expand Up @@ -427,6 +438,19 @@ void WebServerDispatcher::handleRequest(Poco::Net::HTTPServerRequest& request, P
}
}

if (_logFullRequest && _pContext->logger().information())
{
std::ostringstream ostr;
ostr << "Response to " << request.clientAddress().toString();
if (!username.empty()) ostr << " [user: " << username << "]";
ostr << ":\n\t";
response.write(ostr);
std::string message = ostr.str();
Poco::trimRightInPlace(message);
Poco::replaceInPlace(message, "\r\n"s, "\n\t"s);
_pContext->logger().information(message);
}

if (_accessLogger.information())
{
logRequest(request, response, username);
Expand All @@ -437,22 +461,11 @@ void WebServerDispatcher::handleRequest(Poco::Net::HTTPServerRequest& request, P
void WebServerDispatcher::logRequest(const Poco::Net::HTTPServerRequest& request, const Poco::Net::HTTPServerResponse& response, const std::string& username)
{
std::string reqText;
if (_logFullRequest)
{
std::ostringstream ostr;
ostr << request.clientAddress().host().toString() << " [" << username << "]\n";
request.write(ostr);
response.write(ostr);
reqText = ostr.str();
}
else
{
reqText += request.getMethod();
reqText += ' ';
reqText += request.getURI();
reqText += ' ';
reqText += request.getVersion();
}
reqText += request.getMethod();
reqText += ' ';
reqText += request.getURI();
reqText += ' ';
reqText += request.getVersion();

Poco::Message message(_accessLogger.name(), reqText, Poco::Message::PRIO_INFORMATION);

Expand Down

0 comments on commit 98a0b9c

Please sign in to comment.