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.
- Install the log4cxx.
sudo apt-get install liblog4cxx
Write code:
# include "log4cxx/logger.h" # include "log4cxx/basicconfigurator.h" # include "log4cxx/helpers/exception.h" using namespace log4cxx; using namespace log4cxx::helpers; int main(){ BasicConfigurator::configure(); LoggerPtr logger(Logger::getLogger("main")); LOG4CXX_DEBUG(logger, "Hello World"); }
Compile with
-llog4cxx
.Run it.
You should see something like:
1 [0x3f956f44b980] DEBUG main null - Hello World