Pacific Blue Software Logo

PBS Delphi Logger

Simple Thread-Safe Logging for Delphi

A lightweight, thread-safe logging system for Delphi applications. Write log messages to multiple outputs simultaneously - files, console, or GUI memos. Uses a singleton pattern so you can log from anywhere in your code without passing logger instances around.


Key Features


Basic Setup

Add outputs at application startup (e.g., in your .dpr file or main form's OnCreate):

uses
  PBS.LIB.Log, PBS.LIB.Log.Disk, PBS.LIB.Log.Console;

var
  LLog_Path : string;
begin
  // Setup file logging
  LLog_Path := 'C:\MyApp\Logs\';
  TLogger.Add_Output(TFile_Log_Output.Create(LLog_Path, 'MyApp'));
  
  // Add console output for debugging
  TLogger.Add_Output(TConsole_Log_Output.Create);
end;

The logger will create daily log files named MyApp_YYYYMMDD.log in the specified directory. If the path is empty, it defaults to a 'logs' folder next to your executable. The directory is created automatically if it doesn't exist.


How to Use

Once configured, you can log from anywhere in your application:

// Simple message with timestamp
TLogger.Write('Application started');

// Message with class and method context
TLogger.Write('Processing order #12345', 'TOrderProcessor', 'ProcessOrder');

// Message without timestamp
TLogger.Write('Raw message', '', '', False);

// Precise timing (includes milliseconds)
TLogger.Write_Precise('Query executed in 123ms');

// Exception handling
try
  // your code
except
  on E: Exception do
    TLogger.Write(E, 'TOrderProcessor', 'ProcessOrder');
end;

// Add visual separator
TLogger.Write_Error;  // Adds error separator line
TLogger.Write_New_Line;  // Adds blank line

GUI Integration - TMemo Output

You can also log directly to a TMemo on your form for real-time display:

uses
  PBS.LIB.Log.Memo;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  // Add memo output alongside file logging
  TLogger.Add_Output(TMemo_Log_Output.Create(Memo_Log));
end;

All log messages will now appear in both the file and the memo simultaneously.


Sample Log Output

Here's what your log file will look like:

14:23:45  Application started
14:23:45  TOrderProcessor: ProcessOrder: Processing order #12345
14:23:46.123  Query executed in 123ms
14:23:47  Error ------------------------------------
14:23:47  TOrderProcessor: ProcessOrder: Access violation at address 004A2B10

14:23:50  Shutdown complete

Technical Details

File Management

The file logger automatically handles file operations:

Thread Safety

All logger operations use critical sections for thread safety. Multiple threads can safely call TLogger.Write simultaneously. The file logger has additional locking for file operations.

Architecture

The logger uses a singleton pattern with pluggable outputs:

You can create custom outputs by inheriting from TLog_Output and implementing the Write_Line method.


Requirements


Advanced Usage

Multiple File Outputs

You can log to multiple files simultaneously:

var
  LMain_Path, LError_Path : string;
begin
  LMain_Path := 'C:\Logs\Main\';
  LError_Path := 'C:\Logs\Errors\';
  
  TLogger.Add_Output(TFile_Log_Output.Create(LMain_Path, 'App'));
  TLogger.Add_Output(TFile_Log_Output.Create(LError_Path, 'Errors'));
end;

Custom Output Target

Create your own output by inheriting from TLog_Output:

type
  TDatabase_Log_Output = class(TLog_Output)
  public
    procedure Write_Line(const AMessage: string); override;
  end;

procedure TDatabase_Log_Output.Write_Line(const AMessage: string);
begin
  // Insert into database table
  MyDB.Execute('INSERT INTO logs (message) VALUES (?)', [AMessage]);
end;

Clearing Outputs

You can remove all outputs if needed:

TLogger.Clear_Outputs;  // Frees all output objects

Download Source Code

PBS Delphi Logger - Multiple Output Targets


Back to Articles for Developers
Back to Articles on Delphi
Input Sanitization and Validation for Delphi
Convert Left Click to Right Click in TMS WebCore

If you found this useful, then please consider making a donation.

paypal
QR Code for donation Please donate if helpful