Logging in a web application - novalexei/mod_servlet GitHub Wiki

Logging in a web application

If you're writing anything more complex than "Hello world", you probably need logging system. There are plenty logging systems for C++ out there, but logging in mod_servlet is a bit tricky. This is not only multithreaded but also multi-application environment. And we want each application to have its own logging output and logging configuration. The mod_servlet provides fully functional Logging library. It has tons of useful features:

  • std::ostream style logging streams;
  • programmatic or property file configuration
  • single-threaded, synchronous and asynchronous logging modes.
  • UTF-8, UTF-16 and UTF-32 string logging.
  • Named loggers with individual logging levels.
  • Log file size and date rotation.
  • Locale support.
  • Extremely fast.

Probably some other stuff I forgot about too. But what is important, it is very simple to use in your web application. Just include the right header file:

#include <servlet/lib/logger.h>
namespace slog servlet::logging;

Define your logger:

auto lg = slog::get_logger();

or:

auto lg = slog::get_logger("my_log");

and use it:

lg->warning() << "Exception in servlet: " << ex.what() << std::endl;

if (lg->is_loggable(slog::LEVEL::DEBUG)
{
    lg->debug() << "Entering the function x(" << param << ")" << std::endl;
}

This log output will automatically go to the file {web-app-name}.log in the Apache2 server log directory. You also can configure the logging in your application. If mod_servlet finds logging.properties file under WEB-INF directory in your web application (right next to your web.xml) it will use it to configure logging for your particular web application. For file configuration instructions see log_registry (note that you don't need to load configuration file, mod_servlet will do it automatically when web application is loaded if the file is present. It really doesn't get more simple than this.

And here we can use the logging to track the servlet life cycle calls

To The Programming Guide