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.
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.
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
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.
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
The file logger automatically handles file operations:
Prefix_YYYYMMDD.logClose_After_Secs property)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.
The logger uses a singleton pattern with pluggable outputs:
TLogger - Main singleton class, manages outputsTLog_Output - Abstract base class for outputsTFile_Log_Output - Logs to daily rotating filesTConsole_Log_Output - Logs to console (WriteLn)TMemo_Log_Output - Logs to TMemo controlYou can create custom outputs by inheriting from TLog_Output and
implementing the Write_Line method.
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;
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;
You can remove all outputs if needed:
TLogger.Clear_Outputs; // Frees all output objects
Please donate if helpful