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");
  3. Compile with -llog4cxx.

  4. Run it.

You should see something like:

1 [0x3f956f44b980] DEBUG main null - Hello World


80 Words

2011-02-04 22:56 +0000