log4cxx Hello World: Logging for C++

Here's how to add simple logging to any C++ application without a big hassle. At the time of writing this page, Boost.Log is not yet officially released with Boost.

  1. Install the log4cxx. sudo apt-get install liblog4cxx
  2. Write code:
#include "log4cxx/logger.h"
#include "log4cxx/basicconfigurator.h"
#include "log4cxx/helpers/exception.h"
using namespace log4cxx;
using namespace log4cxx::helpers;
int main(){
    LoggerPtr logger(Logger::getLogger("main"));
    LOG4CXX_DEBUG(logger, "Hello World");
  1. Compile with -llog4cxx.
  2. Run it.

You should see something like:

1 [0x3f956f44b980] DEBUG main null - Hello World

Your rating: None Average: 4.5 (2 votes)