feat(log): add printContext method for improved context logging
This commit is contained in:
parent
f119d98c70
commit
c255878b2f
@ -1,4 +1,6 @@
|
||||
#include <webserv/log/Channel.hpp>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
Log::Level Channel::getLogLevel() const
|
||||
{
|
||||
@ -9,3 +11,27 @@ void Channel::setLogLevel(Log::Level 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();
|
||||
}
|
||||
@ -19,6 +19,7 @@ class Channel
|
||||
|
||||
protected:
|
||||
[[nodiscard]] Log::Level getLogLevel() const;
|
||||
[[nodiscard]] static std::string printContext(const std::map<std::string, std::string> &context);
|
||||
void setLogLevel(Log::Level level);
|
||||
|
||||
private:
|
||||
|
||||
@ -47,20 +47,6 @@ void FileChannel::log(const Log::Level &logLevel, const std::string &message,
|
||||
<< "[" << Log::logLevelToString(logLevel) << "] " << message << '\n';
|
||||
|
||||
// Log the context if it exists
|
||||
if (!context.empty())
|
||||
{
|
||||
fileStream_ << "\n\t| Context: {";
|
||||
bool first = true;
|
||||
for (const auto &[key, value] : context)
|
||||
{
|
||||
if (!first)
|
||||
{
|
||||
fileStream_ << ", ";
|
||||
}
|
||||
fileStream_ << key << ": " << value;
|
||||
first = false;
|
||||
}
|
||||
fileStream_ << "}\n";
|
||||
}
|
||||
fileStream_ << printContext(context);
|
||||
fileStream_ << std::flush;
|
||||
}
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <ostream>
|
||||
|
||||
StdoutChannel::StdoutChannel(Log::Level logLevel)
|
||||
{
|
||||
@ -18,24 +19,11 @@ void StdoutChannel::log(const Log::Level &logLevel, const std::string &message,
|
||||
{
|
||||
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::cout << prefix;
|
||||
std::cout << message;
|
||||
if (!context.empty())
|
||||
{
|
||||
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;
|
||||
out << prefix;
|
||||
out << message << '\n';
|
||||
out << printContext(context);
|
||||
out << std::flush;
|
||||
}
|
||||
@ -18,7 +18,7 @@ int main(int argc, char **argv)
|
||||
Log::setFileChannel("webserv.log", std::ios_base::app, Log::Level::Trace);
|
||||
Log::setStdoutChannel(Log::Level::Trace);
|
||||
Log::info("\n======================\nStarting webserv...\n======================\n");
|
||||
|
||||
Log::warning("Testing context: " + LOCATION, {{"key1", "value1"}, {"key2", "value2"}});
|
||||
ConfigManager::getInstance().init(argv[1]); // NOLINT
|
||||
Server server(ConfigManager::getInstance());
|
||||
server.start();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user