SourceForge > Log4Delphi
 

Log4Delphi Tutorial

About This Tutorial

This tutorial provides a step by step guide to using Log4Delphi. It does not attempt to explain or provide an in depth discussion on any details. If you want more detail check the User Guide.

Step 1: Obtaining a Source Distribution

1.1 Download the Source Distribution

Download a source distribution of Log4Delphi log4delphi-version.archive from here. I used log4delphi-0.4-src.tar.gz for this tutorial.

1.2 Extract The Archive

Extract the archive using the tool of your choice to the location of your choice. I used Zipgenius to extract it to my F:\ drive. This resulted in a folder named F:\log4delphi-0.4

Step 2: Creating a New Project

2.1 Launch Delphi And Save Project

  1. Create a folder named tutorial in the location of your choice. I used my F:\ drive again.
  2. Launch Delphi and save the project in the tutorial folder you created.
  3. Now add a button to the form and label its caption "Click Me!".
    Form with Button
  4. Copy all the Pascal source files from the log4delphi-version/src/delphi folder, including the util folder and its contents into the tutorial folder, excluding the TConsoleUnit.pas file.
  5. Now add the source files to your project by selecting Project > Add To Project from the main menu in Delphi.
  6. Select the source files and click the Open button.
    Adding the source code.
  7. Now copy the file named log4delphi.properties in the log4delphi/example folder to your tutorial folder.

Step 3: Perfromming Logging

  1. Select Project > View Source from the main menu in Delphi.
  2. Add the line TConfiguratorUnit.doPropertiesConfiguration('log4delphi.properties'); beneath the line that reads Application.Initialize;.
  3. Now add an onClick event handler to the button. In the handler add the line TLogger.getInstance.debug('Button Clicked!');.
  4. Finally add an onDestroy handler to the main form. Add the following code to the handler: TLogger.freeInstances;.

Step 4: Evaluating The Log File

  1. Compile and run the application. Click the button three times and then exit the application.
  2. You should have a log file in the tutorial folder named app.log with the following content
    DEBUG - Button Clicked
    DEBUG - Button Clicked
    DEBUG - Button Clicked

Source Code

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
   TLoggerUnit;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
   TLogger.getInstance.debug('Button Clicked!');
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
   TLogger.freeInstances;
end;

end.