Click here to Skip to main content
11,798,866 members (41,986 online)
Click here to Skip to main content

LogDriver - A simplified log4j using C++

, 4 Jan 2002 152.8K 3.2K 63
Rate this:
Please Sign up or sign in to vote.
Some Logging Classes for C++ on the Windows Platform
<!-- Download Links --> <!-- Add the rest of your HTML here -->


This is version 0.1 of the CLog Class Hierarchy, so bear with me. This is the second revision of this article, I hope this new version is better!

Here is a class heirarchy designed to simplify logging of messages on a Windows platform. I should qualify and say that porting these classes to *nix is trivial. It should be obvious that the CEventLogAppender would need to be scrapped or retasked.

These classes are inspired by log4j. I was introduced to log4j which is a sub project of Apache Jakarta by a friend. The website is a great place to get more information, check it out at

My main reason for checking out log4j in the first place was that I had been told that my current method for logging was substandard. I did not believe this of course. However, after 5 minutes of working with log4j, I knew it for the truth! So, as a matter of course, I went on a quest, looking for log4cpp. Well the log4j site has links to two different projects:

One at;

and the other at

I checked them out and although they each have their merits, they were too complicated for my taste. It was at this point that the CLog and CLogAppender classes were born.


I decided that I wanted to create a set of logging classes with really simple, yet elegant, syntax. Something along the lines of

CLog myCLog(source);
myCLog.writeLog(debuglevel, message);

Well as it turns out, it's not quite that easy, but it's close.

How it works

The core concept to understanding the CLog hierarchy is the idea of severity. Log messages are given a severity at the time they are created. Log Appenders are given a severity threshold at the time they are created. Then, when a message is sent with a severity greater than the appender's threshold the message is logged.

This can be expressed more concisely as follows:
A message with severity x is written to a log appender with threshold y if x >= y.

Severity and threshold values are organized as follows:
debug < info < warn < error < fatal



Detailed programmatic informational messages used as an aid in troubleshooting problems by programmers.


Brief informative messages to use as an aid in troubleshooting problems by production support and programmers.


Messages intended to notify help desk, production support and programmers of possible issues with respect to the running application.


Messages that detail a programmatic error, these are typically messages intended for help desk, production support, programmers and occasionally users.


Severe messages that are programmatic violations that will usually result in application failure. These messages are intended for help desk, production support, programmers and possibly users.

What all this gobbledegook means is that you, as the programmer, has the duty to create meaningful messages and assign them the proper severity. If you are good at this, then, you will reap the benefit of incredibly informative logs with a standardized look and feel making everyone's life easier.

Project Files

This set of classes consists of a number of files; I will lay them out here for your perusal with a brief description.

Header files

contains a number of useful helper data structures
the main CLog Class Definition
the abstract CLogAppender Class Definition that the specific log appenders are derived from
the specific Console log appender class definition
the specific File log appender class definition
the specific Event log appender class definition

Implementation files

contains a number of useful helper data structures
the main CLog Class Implementation
the abstract CLogAppender Class Implementation
the specific Console log appender class implementation
the specific File log appender class implementation
the specific Event log appender class implementation

The test application (driver)

the simple test application that will use the log and log appender class to write to the console, test.dat, and the event log.

Trying it out

In order to use the logging classes here is a description of what is required.

  • You will need to create a .cpp file that will 'drive' the logger - in my case this was LogDriver.cpp.
  • include log.h, this will bring in the needed definitions, etc.
    #include "/inc/log.h"
  • instantiate an instance of the CLog class, this will be the 'source' of log messages
    CLog myLog("LogDriver");
  • instantiate and add as many log appenders as you like
    CConsoleLogAppender * pConsoleLogAppender
              = new CConsoleLogAppender(nsCLog::info);
    CFileLogAppender * pFileLogAppender
              = new CFileLogAppender(nsCLog::warning, "test.dat");
    CEventLogAppender * pEventLogAppender
              = new CEventLogAppender(nsCLog::debug);
  • write to the log(s) througout your codebase
    myLog.writeLog(nsCLog::warning, "This is my test log message");	

What you will have done is this:

  • Created a logger that's source is "LogDriver"
  • Created a log appender for console messages
  • Created a log appender to write to test.dat
  • Created a log appender to write to the event log
  • Told the LogDriver logger about the 3 appenders
  • Written to the various logs

What the messages will look like is:

01052002_124658 | LogDriver | warning | This is my test log message

The event log message is going to appear a little differently, but basically the same. Because I'm lazy it will probably be like:

The entry in Event Viewer:

Type: Error
Date: 1/5/2002
Time: 12:46:58 PM
Source: LogDriver
Category: (1500)
Event: 0
User: N/A
Computer: Your Computer Name
The message (double click the entry): The description for Event ID ( 0 ) in Source ( LogDriver ) cannot be found. The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer. The following information is part of the event: 01052002_124658 | LogDriver | warning | This is my test log message.

Let me know what you think - preferably constructive if not positive, I'm not asking for flames Smile | :) .

Read the _todo.txt document before sending me 'it oughta do X!' messages.


That's it - really.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Will Senn
Program Manager Underwriters Laboratories, Workplace Health and Sa
United States United States
Will Currently serves as the Senior Director of Research and Development at Underwriters Laboratories Workplace Health and Safety in Franklin, Tennessee. He is Responsible for the direction of a globally distributed, talented, and diverse group of architects, software engineers, user experience designers and developers in the development of advanced software platforms and products for the Workplace Health and Safety business unit of UL.

You may also be interested in...

Comments and Discussions

QuestionCleaning up lazy descriptions Pin
masteryoda2121-Jul-11 5:39
membermasteryoda2121-Jul-11 5:39 
GeneralAbout your CLog destructor Pin
chenxiemin4-Nov-10 15:31
memberchenxiemin4-Nov-10 15:31 
QuestionI need information on log4cplus Pin
Display17-Mar-08 20:13
memberDisplay17-Mar-08 20:13 
GeneralAny issue about wrappping LogDriver in a separate Dll? (string across dll) Pin
devvvy26-Aug-07 15:02
memberdevvvy26-Aug-07 15:02 
QuestionCan it be used in linux? Pin
Ummul Syahmi30-Jul-07 22:07
memberUmmul Syahmi30-Jul-07 22:07 
QuestionHow to add thread safety Pin
anoopriya21-May-07 23:30
memberanoopriya21-May-07 23:30 
QuestionBuilding using the Latest Compiler Pin
splendided27-Apr-06 4:23
membersplendided27-Apr-06 4:23 
AnswerRe: Building using the Latest Compiler Pin
Will Senn27-Apr-06 4:38
memberWill Senn27-Apr-06 4:38 
GeneralRe: Building using the Latest Compiler Pin
splendided27-Apr-06 5:11
membersplendided27-Apr-06 5:11 
GeneralMemory leak in CFileLogAppender Pin
eejc2-Mar-06 21:45
membereejc2-Mar-06 21:45 
QuestionHow to close the file Pin
fabzz2-Nov-05 2:40
memberfabzz2-Nov-05 2:40 
AnswerRe: How to close the file Pin
Landrion13-Nov-05 19:39
memberLandrion13-Nov-05 19:39 
How about return from this function pFileLogAppender, store it and after pass to this function as old.

CFileLogAppender* CSiges2SSC::SwitchLogFile(string verbosity, CFileLogAppender* oldLogAppender)


// First call can be NULL
if (oldLogAppender)

CFileLogAppender * pFileLogAppender = new CFileLogAppender (ImpostaVerbosita(verbosity));



return pFileLogAppender;
GeneralRe: How to close the file Pin
Landrion13-Nov-05 19:47
memberLandrion13-Nov-05 19:47 
GeneralRe: How to close the file Pin
fabzz13-Nov-05 21:12
memberfabzz13-Nov-05 21:12 
GeneralI need help with LogDriver Pin
JaVinci16-May-05 5:28
memberJaVinci16-May-05 5:28 
GeneralRe: I need help with LogDriver Pin
Will Senn16-May-05 5:47
memberWill Senn16-May-05 5:47 
GeneralRe: I need help with LogDriver Pin
JaVinci16-May-05 9:49
memberJaVinci16-May-05 9:49 
GeneralRe: I need help with LogDriver Pin
Will Senn17-May-05 2:38
memberWill Senn17-May-05 2:38 
GeneralLatest updates Pin
rromerot29-Apr-04 5:05
memberrromerot29-Apr-04 5:05 
GeneralRe: Latest updates Pin
Will Senn30-Apr-04 4:36
sussWill Senn30-Apr-04 4:36 
GeneralRe: Latest updates Pin
rromerot30-Apr-04 6:33
memberrromerot30-Apr-04 6:33 
GeneralRe: Latest updates Pin
Will Senn30-Apr-04 6:37
memberWill Senn30-Apr-04 6:37 
GeneralRe: Latest updates Pin
nicolnie22-May-05 19:13
membernicolnie22-May-05 19:13 
GeneralMemory leak Pin
Fabio Angelini25-Jan-02 4:01
memberFabio Angelini25-Jan-02 4:01 
GeneralRe: Memory leak Pin
decuser25-Jan-02 10:56
memberdecuser25-Jan-02 10:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151002.1 | Last Updated 5 Jan 2002
Article Copyright 2002 by Will Senn
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid