log4cplus on Windows

Requirements:
VS Studio C++ 2010 Express edition and the source code can be obtained from log4cplus at sourceforge.

Unpack the log4cplus sources. Open the log4cplus.sln solution file located in the msvc8 folder. The solution explorer should contain the log4cplus_dll project.
The default target is set to DEBUG mode. Make sure to alter the target to DEBUG_UNICODE!
Open the context menu for this entry and hit compile.

The log4cplusUD.lib and log4cplusUD.dll should be created.

1>     Bibliothek ".\.\Win32\log4cplus_dll.Debug_Unicode\log4cplusUD.lib" und Objekt ".\.\Win32\log4cplus_dll.Debug_Unicode\log4cplusUD.exp" werden erstellt.
1>  log4cplus_dll.vcxproj -> *\log4cplus-1.0.4\msvc8\.\Win32\log4cplus_dll.Debug_Unicode\log4cplus_dll.dll
========== Alles neu erstellen: 1 erfolgreich, Fehler bei 0, 0 übersprungen ==========

Create some folders (e.g. bin, lib, and include) at a specific location and place the binary, the library and the header files into it.


Add the bin folder (e.g. C:\Users\Public\bin) to the path environment variable.

VS 2010 integration:
Create a new console application using VS 2010. Open the project properties and add the additional include folder. Make sure you are targeting the DEBUG mode.

Add the additional lib folder and add the required log4cplusUD.lib as follows.

Try to compile without any erros. Make sure if you target DEBUG mode that you used lib4cplusUD.lib not the lib4cplusD.lib or the lib4cplus.lib!

Modify the working directory for the debugger to the $TargetDir. Otherwise you have to fiddle the path to the properties file on your own. Take a look at the #ifdef DEBUG pragma of the main.cpp underneath.

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. *\Debug).
Make sure you have the privileges to create a file in the location – and this location exists – 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 VS modify the main.cpp to the following.

#include "stdafx.h"

#include
#include

#ifdef DEBUG
#include
#endif

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

    // Load the properties
    #ifdef DEBUG
	std::wstring applicationPath(argv[0]);
	std::wstringstream fullqualifedPath;
	fullqualifedPath << applicationPath.substr(0, applicationPath.find_last_of(L"/\\")) << L"\\" << L"log.properties";
	PropertyConfigurator::doConfigure(fullqualifedPath.str());
#else
	PropertyConfigurator::doConfigure(L"log.properties");
#endif

    // 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.

Compile and run the application. If the PATH variable does not include the log4cplusUD.dll an error message should appear at startup. 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

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