22
votes

can anybody hint me a good, free Delphi logging framework? It should be easy to use and it should support different "log writers" including plain text, database and network.

8
Can't make a coment here... Telling from names, Log4delphi and Log4D are both inspired and modelled after Log4J Then how they are different and how to choose between ? - Arioch 'The

8 Answers

13
votes
8
votes

I know it's not free - but well worth it's money: CodeSite by Raize Software. Quality has its price! :-)

I always enjoyed working with CodeSite, especially the ability to add just about any type of objects to the log without huge conversions to a string format was often very helpful.

Again: not free, but worth its price in gold, if you really are serious about production-quality logging and viewing of those logs.

Marc

6
votes

I have been granted access to update the dormant Log4Delphi project and I have rolled up 4 years of bugfixes and patches into the latest 0.8 release available on Source-forge. I use this library in production and have found it to very stable and reliable and easy to use.

Log4Delphi Downloads Page

3
votes

I'm a big fan of CodeSite, too, but if you're looking for free, how about OutputDebugString with either the Delphi IDE or DebugView from SysInternals.

3
votes

Another alternative to Codesite is Overseer which is open sourced and part of the nexus project, but stands alone so does not require you to use their framework.

3
votes

A logger library shouldn't dump the contents synchronously. That will slow down the application. Instead, it needs to buffer the contents and dump them when it is flushed.

It should also be thread-safe and able to dump the contents from different threads. (And preferably be able to log the thread ID as well)

It should also be flexible and able to log multiple output formats.

Here's a library which does all this: loggerpro

2
votes

There is another new logging framework for Delphi, which comes in a single file (nxlogging.pas). nxlogging is a nice lightweight and powerful set of classes like log4d (appenders, formaters), but much easier tu use. It includes file appenders (rolling files, all in a single one, etc...) and a tcp appender too, so you can forward your logs to a central logserver.

1
votes

There is Log4D, another port of the Java Log4J logging framework for Delphi at Sourceforge.

Log4D project page at sourceforge

A description of its architecture can be found on CodeCentral and here.

Help files are available online at http://cc.embarcadero.com/item/16446.

It is currently based on log4j 1.2.12 and quite active, and very easy to use. It includes TLogODSAppender, TLogStreamAppender, TLogFileAppender, TLogRollingFileAppender.

The following example project creates a ODS appender. If you run it in the IDE, the log messages will appear in the ‘Event log’ window.

program Log4Dexample;

{$APPTYPE CONSOLE}

uses
  Log4D,
  SysUtils;

var
  Logger: TLogLogger;

begin
  try
    // basic configuration - creates a TLogODSAppender (ODS = OutputDebugString)
    TLogBasicConfigurator.Configure;

    // set the log level
    TLogLogger.GetRootLogger.Level := Trace;

    // create a named logger
    Logger := TLogLogger.GetLogger('exampleLogger');

    // write log messages
    Logger.Fatal('fatal output');
    Logger.Error('error output');
    Logger.Warn('warn output');
    Logger.Info('info output');
    Logger.Debug('debug output');
    Logger.Trace('trace output');

    ReadLn;

  except
    on E:Exception do
    begin
      Writeln(E.Classname, ': ', E.Message);
      ReadLn;
    end;
  end;
end.

Writing appenders is straightforward, here is an example of a simple console appender:

unit LogConsoleAppender;

interface

uses
  Log4D;

type
  { Send log messages to console output. }
  TLogConsoleAppender = class(TLogCustomAppender)
  protected
    procedure DoAppend(const Message: string); override;
  end;

implementation

{ TLogConsoleAppender }

procedure TLogConsoleAppender.DoAppend(const Message: string);
begin
  if IsConsole then
    Write(Message);
end;

initialization
  RegisterAppender(TLogConsoleAppender);

end.