Skip to content
Aurélien Brabant edited this page Nov 12, 2021 · 3 revisions

Logger API

Basic usage

In order for webserv to provide useful debug information, a Logger class is available. It can easily be used like so:

Logger generalLogger("logdir", "logfile.txt");
int age = 42;

generalLogger << "This is some really useful information: I'm " << age << " years old";

This would output "This is some really useful information: I'm 42 years old" in a file at location ./logdir/logfile.txt.

Log levels

Every serious logging system usually provide the ability to divide logs into different categories depending on how important is the logged information. Our logger API provides a handy and easy to use interface to deal with that:

Logger generalLogger("logdir", "logfile.txt");

generalLogger.setWebservLogLevel(Logger::WARNING);

generalLogger << Logger::INFO << "Info debug\n";
generalLogger << "Still info debug\n";
generalLogger << Logger::WARNING << "Warning debug\n";

The above snippet would only log "Warning debug\n" in the log file, excluding INFO level logs because the global log level has been set to Logger::WARNING.

There are four available log levels, the first being the more specific:

  • DEBUG (0)
  • INFO (1)
  • WARNING (2)
  • ERROR (3)

If the webserv log level is greater or equal to the log level of the currently logged information, then the information is effectively logged, otherwise the output statement does nothing.

Printing timestamp

The static member function Logger::getTimestamp returns the current timestamp. For example, given that current date is 2021/11/11 01:29 AM the returned timestamp would be [2021-11-11-01-29-00].

Below is an example of how it would typically be used:

generalLogger << Logger::getTimestamp() << " Oh my dog something happened!\n";

Clone this wiki locally