65.9K
CodeProject is changing. Read more.
Home

LogXML - A XML Logging Class

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.50/5 (11 votes)

May 12, 2003

1 min read

viewsIcon

95290

downloadIcon

3037

A simple XML style logging class for all purposes.

Introduction

Almost everybody needs sometimes something like a logging class. Me too. That is why I wrote such a class to do my logging although there are already some very convenient logging classes like CProtocol by jazper. But I thought of some general approach: Logging with XML.

Possibilities

The probably most interesting point is the possibility to log into a structure. Now you can log in a fairly natural way: simply go down one level if you call a function and after that go up and you will get a log structure that resembles your program's calling tree. All log entries will automatically get the right place.

Usage

Using LogXML is not very difficult. First of all:

  #include "LogXML.h"
Now you can instantiate LogXML by:

  LogXML* log = new LogXML("LogFileName.xml", "ApplicationName");
There are several ways to write a Log Entry, some kind of Log Record can be written by:

  log->Log("Error: Something is not correct. Please Fix.", // Entry
           __FILE__,               // Filename of Source File<
           "myLoggingFunction()"); // Function Name
Or you can write only one line by:

  log->WriteLine("TagName", "This is your Log Entry");

The output (log file) of these examples is:

<?xml-stylesheet href="Log.xsl" type="text/xsl" ?>
<LogFile>
    <AppName>ApplicationName</AppName>
    <Timestamp>1052656519.488</Timestamp>
    <Date>2003-05-11</Date>
    <Time>14:35:19.488</Time>
    <LogItem>
        <Date>2003-05-11</Date>
        <Time>14:35:19.488</Time>
        <Filename>LogXML.cpp</Filename>
        <Entry>Log File opened</Entry>
    </LogItem>
    <LogItem>
        <Date>2003-05-11</Date>
        <Time>14:35:19.488</Time>
        <Filename>C:\Code\LogXML\SampleSourceFile.h</Filename>
        <Function>SampleFunction()</Function>
        <Entry>Error: Something is not correct. Please Fix.</Entry>
    </LogItem>
    <TagName>This is your Log Entry</TagName>
    <LogItem>
        <Date>2003-05-11</Date>
        <Time>14:35:19.488</Time>
        <Filename>LogXML.cpp</Filename>
        <Entry>Log File closed</Entry>
    </LogItem> 
</LogFile>

Annotations

To use such a log file you can use a XML parser like XPath or everything else you want. To convert a file produced by LogXML to something human-readable you can use for example XSL. Since my knowledge of XSL is not the best, I only managed to write a file named Log.XSL that shows Log Records of Level 1, i.e. do not use LogXML::goDown() and use only LogXML::Log() to write entries if you want to use it. And this is our sample Log File shown by Internet Explorer (6.0):



History

  • May 12, 2003 - First Release 1.0.0