feat(log): add printContext method for improved context logging

This commit is contained in:
Quinten Mennen 2025-09-23 16:15:07 +02:00
parent f119d98c70
commit c255878b2f
5 changed files with 36 additions and 35 deletions

View File

@ -1,4 +1,6 @@
#include <webserv/log/Channel.hpp> #include <webserv/log/Channel.hpp>
#include <sstream>
#include <iomanip>
Log::Level Channel::getLogLevel() const Log::Level Channel::getLogLevel() const
{ {
@ -9,3 +11,27 @@ void Channel::setLogLevel(Log::Level level)
{ {
logLevel_ = level; logLevel_ = level;
} }
std::string Channel::printContext(const std::map<std::string, std::string> &context)
{
std::stringstream ss;
if (!context.empty())
{
bool first = true;
for (const auto &[key, value] : context)
{
if (!first)
{
ss << "\n";
}
ss << "\t| " << std::setw(15) << std::right << std::setfill(' ') << key << " : " << value;
first = false;
}
ss << "\n\n";
}
return ss.str();
}

View File

@ -19,6 +19,7 @@ class Channel
protected: protected:
[[nodiscard]] Log::Level getLogLevel() const; [[nodiscard]] Log::Level getLogLevel() const;
[[nodiscard]] static std::string printContext(const std::map<std::string, std::string> &context);
void setLogLevel(Log::Level level); void setLogLevel(Log::Level level);
private: private:

View File

@ -47,20 +47,6 @@ void FileChannel::log(const Log::Level &logLevel, const std::string &message,
<< "[" << Log::logLevelToString(logLevel) << "] " << message << '\n'; << "[" << Log::logLevelToString(logLevel) << "] " << message << '\n';
// Log the context if it exists // Log the context if it exists
if (!context.empty()) fileStream_ << printContext(context);
{
fileStream_ << "\n\t| Context: {";
bool first = true;
for (const auto &[key, value] : context)
{
if (!first)
{
fileStream_ << ", ";
}
fileStream_ << key << ": " << value;
first = false;
}
fileStream_ << "}\n";
}
fileStream_ << std::flush; fileStream_ << std::flush;
} }

View File

@ -5,6 +5,7 @@
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
#include <map> #include <map>
#include <ostream>
StdoutChannel::StdoutChannel(Log::Level logLevel) StdoutChannel::StdoutChannel(Log::Level logLevel)
{ {
@ -18,24 +19,11 @@ void StdoutChannel::log(const Log::Level &logLevel, const std::string &message,
{ {
return; return;
} }
std::cout << "[" << std::setw(3) << std::setfill('0') << Log::getElapsedTime() << "] "; std::ostream &out = (logLevel >= Log::Level::Warn) ? std::cerr : std::cout;
out << "[" << std::setw(3) << std::setfill('0') << Log::getElapsedTime() << "] ";
std::string prefix = "[" + Log::logLevelToColoredString(logLevel) + "] "; std::string prefix = "[" + Log::logLevelToColoredString(logLevel) + "] ";
std::cout << prefix; out << prefix;
std::cout << message; out << message << '\n';
if (!context.empty()) out << printContext(context);
{ out << std::flush;
std::cout << "\n\t| Context: {";
bool first = true;
for (const auto &[key, value] : context)
{
if (!first)
{
std::cout << ", ";
}
std::cout << key << ": " << value;
first = false;
}
std::cout << "}\n";
}
std::cout << "\n" << std::flush;
} }

View File

@ -18,7 +18,7 @@ int main(int argc, char **argv)
Log::setFileChannel("webserv.log", std::ios_base::app, Log::Level::Trace); Log::setFileChannel("webserv.log", std::ios_base::app, Log::Level::Trace);
Log::setStdoutChannel(Log::Level::Trace); Log::setStdoutChannel(Log::Level::Trace);
Log::info("\n======================\nStarting webserv...\n======================\n"); Log::info("\n======================\nStarting webserv...\n======================\n");
Log::warning("Testing context: " + LOCATION, {{"key1", "value1"}, {"key2", "value2"}});
ConfigManager::getInstance().init(argv[1]); // NOLINT ConfigManager::getInstance().init(argv[1]); // NOLINT
Server server(ConfigManager::getInstance()); Server server(ConfigManager::getInstance());
server.start(); server.start();