Skip to main content

C++ logging using Apache Log4cxx on Linux

I'd demonstrate the set up of Apache log4cxx on Ubuntu 12.x in following steps:

1. Download all required packages
2. Build Log4cxx via Apache Maven
3. Use the libraries in a NetBeans C++ project on Ubuntu Linux.


Reference URL: http://www.yolinux.com/TUTORIALS/Log4cxx.html

This URL is quite detailed and explains things for other *nix operating systems as well. I wanted to start with minimum steps required, hence this post.

I have a Windows 7 desktop and have Ubuntu 12.x 64-bit running on it via Oracle Virtualbox. So Ubuntu is running as a guest on my Windows 7 host.

[The reference URL mentions different versions of  'libaprXX' libs but we have to use 'libapr1-dev' and 'libaprutil-dev', will see that later.]





Prerequisites (install all of the following one by one)

  • autoconf and automake --> sudo apt-get install autoconf automake
  • libxml2 and libxml2-devel (http://xmlsoft.org/) --> sudo apt-get install libxml2 libxml2-dev
  • gmp (http://swox.com/gmp/) --> sudo apt-get install libgmpxx4ldbl libgmp3-dev
  • boost and boost-devel --> sudo apt-get install libboost-dev
  • cppunit --> sudo apt-get install libcppunit-dev
  • apr, apr-devel and apr-util, apr-util-devel --> sudo apt-get install libapr1-dev libaprutil1-dev doxygen
  • Install Apache Maven. (Refer to the instructions given on reference URL if Maven is not installed). Once Maven is properly installed and is in PATH then following command must be successful and display the Maven version:




Once crossed the "installations" stage, perform the following steps:

  1. Download log4cxx latest version from here http://logging.apache.org/log4cxx
  2. Unzip and change directory to downloaded folder, lets say you downloaded 'apache-log4cxx-0.10.0'
  3. cd "path-to apache-log4cxx-0.10.0"
  4. ./configure --prefix=/usr
  5. make
  6. make install
If you encounter errors, go to above reference URL which shows some common pitfalls during this process.

If everything went fine, you will have the following installed:

  • /usr/include/log4cxx/...
  • /usr/lib/liblog4cxx.so.10.0.0
  • /usr/lib/liblog4cxx.la
  • /usr/lib/liblog4cxx.a
  • /usr/lib/pkgconfig/liblog4cxx.pc

Using the Apache log4cxx library in a C++ project

My NetBeans C++ project has the following structure (Note that log4j configuration file is in project resource path)

projectstructure


Need to set the library include path in project properties

Right click on project and select "Properties"




Then go to "Linker" tab and set the 'Additional Library Directories' and 'Libraries' as follows

linkertab

 

We are ready to use the logging library in code now:

codesnippet

Following lines are important:
  1. Lines 33-38 showing how to include required headers and namespaces.
  2. Line 46 - how to include log4j xml configuration
  3. Line 58 - How to use the API to invoke logging methods

How does log4j.xml look like

Following is an excerpt of log4j.xml. As you can see the log file name is "appxlogfile.log". By default it will be created in project's working dir when run.
log4jxml

[Note: You can pick the complete snippet from reference URL]

Does it work

(common programmer's excuse) At least on my box it does :)

When this program is run, a log file is created in project's working dir as shown here:

filegenerated

And its contents are:

logfileless

This is similar to Apache log4j's logging when used in Java based projects.
 
Please leave your feedback in the comments.

...happy logging.

Comments

Popular posts from this blog

C++11 std::thread Construction and Assignment Tricks

C++11 arrived with std::thread and related classes built within the standard. With the move semantics in place, the construction and assignment practices of 'std threads' deserve a small write up. Let's define a 'Runnable' first: class Runnable {    public:       virtual void operator ()() = 0;       virtual ~Runnable(){} }; class Task : Runnable {      public:          void operator ()()          {               //do sth here..          } };                                                 //later..      #include <thread>     Task runnable;     std::thread t1; //not attached to any thread                                                 std::thread t2 ( runnable ); //attached and running   Copy Construction of std::thread is not allowed            std::thread t3 ( t2 ); // compilation error            std::thread t4 = t2; // compilation error error : use of deleted function std::thread (const std::th

..Where Apache Camel meets Spring JMS

This post is relevant for developers who are aware of Apache Camel and use its JMS component to consume from a JMS broker. We are only discussing the message consumption part here. I'm assuming you are conversant with Camel's terminology of routes, endpoints, processors and EIPs. If not, following is a good place to start: Link --->  Apache Camel Home   Ever wondered what happens underneath when a Camel consumer route is defined to take messages from a JMS endpoint ? This post is an attempt to explain how Camel integrates with JMS via Spring APIs and how the business logic defined in a Camel route is invoked ? Following is a simplified sequence diagram showing the set-up process which will eventually lead to message consumption from JMS broker. . Creation of JMS endpoints in a camel context is initiated by definition (via Java/XML DSL). A JMS endpoint takes a number of configurable attributes as seen here Camel JMS Component . As soon as context is bootstrappe