log4cplus on Ubuntu

Requirements:
A valid gcc toolchain and the source code can be obtained from log4cplus at sourceforge.
Unpack the source code and just follow the instruction from the install.txt. Make sure you have the necessary permission (sudo).

  1. Start the terminal and navigate to the location where the source code was unpacked.
    Configure the package for your system.
    sudo ./configure
  2. Compile the package.
    make
  3. Install with the necessary privileges the package.
    sudo make install
  4. Refresh the library cache.
    sudo ldconfig

The default library and header location for version 1.0.4 are /usr/local/lib and /usr/local/include. Make sure the liblog4cplus.a, liblog4cplus.so libraries and headers exists.

QT Creator integration:
Create a new console application and open the created project file.

#-------------------------------------------------
#
# Project created by QtCreator 2012-01-08T07:03:26
#
#-------------------------------------------------
QT       += core
QT       -= gui

TARGET = Log4CplusClient
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app

SOURCES += main.cpp

Add the log4cplus library and header by adding the following two lines to the project file:

TEMPLATE = app

LIBS += -L/usr/local/lib/log4cplus -llog4cplus
INCLUDEPATH += -L/usr/local/include/log4cplus

SOURCES += main.cpp

And try to compile it without any errors.

Create a simple property file (e.g. named log.properties) for the application and place this file into the output folder where your application gets compiled to (e.g. *-build-desktop).
Make sure you have the privileges to create a file in the location defined by the property file.

log4cplus.rootLogger=INFO, STDOUT, FILEAPPENDER
#log4cplus.logger.main=INFO
#log4cplus.logger.utils=FILEAPPENDER

log4cplus.appender.STDOUT=log4cplus::ConsoleAppender
log4cplus.appender.STDOUT.layout=log4cplus::PatternLayout
log4cplus.appender.STDOUT.layout.ConversionPattern=%d{%m/%d/%y %H:%M:%S} [%t] %-5p %c{2} %%%x%% - %m [%l]%n

log4cplus.appender.FILEAPPENDER=log4cplus::RollingFileAppender
log4cplus.appender.FILEAPPENDER.File=/tmp/consoleapplication.log
log4cplus.appender.FILEAPPENDER.MaxFileSize=5MB
#log4cplus.appender.FILEAPPENDER.MaxFileSize=500KB
log4cplus.appender.FILEAPPENDER.MaxBackupIndex=1
log4cplus.appender.FILEAPPENDER.layout=log4cplus::TTCCLayout

In QT creator modify the main.cpp to the following.

#include <log4cplus/configurator.h>
#include <log4cplus/logger.h>

int main(int argc, char *argv[])
{
// Use the log4cplus namespace
using namespace log4cplus;

// Load the properties
PropertyConfigurator::doConfigure("log.properties");

// Create the logger
const Logger logger = Logger::getInstance("main");

// Log with INFO level
if (logger.isEnabledFor(INFO_LOG_LEVEL))
{
LOG4CPLUS_INFO(logger, "Application startup");
}

// Log with INFO level
if (logger.isEnabledFor(INFO_LOG_LEVEL))
{
LOG4CPLUS_INFO(logger, "Application shutdown");
}
}

Make sure you have set the build target to DEBUG not RELEASE. When using the RELEASE mode the compiler should complain about the missing pthread references.

Compile and run the application. If you did not refresh the library cache after installing the log4cplus package the compiler should complain about the liblog4cplus.so.4 library. If the property file cannot be found, the logger will complain about not having any appenders.

log4cplus:ERROR No appenders could be found for logger (main).
log4cplus:ERROR Please initialize the log4cplus system properly.

The following output should be generated on the console.

01/08/12 06:50:05 [3079005904] INFO main %% - Application startup [../Log4CplusClient/main.cpp:18]
01/08/12 06:50:05 [3079005904] INFO main %% - Application shutdown [../Log4CplusClient/main.cpp:24]

The consoleapplication.log looks like.

2090 [3592] INFO main <> - Application startup
2106 [3592] INFO main <> - Application shutdown
Advertisements

About Jan Tschada

Software engineer from Germany
This entry was posted in Common and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s