Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Catalog · How the catalog works

Random project

Mir-Logger

Mir-Logger is a Mir framework logging library with configurable levels, colored console output, and file logging.

View on GitHub

Anyone may press Interesting, signed in or not. It helps others find this project.

Member rating

No ratings yet.

Sign in to rate this project.

README

Logging Example

The logger is not part of the framework core. It lives in the example application as an external-style dependency registered by the application bootstrap.

Units:

text
Mir-Logger/Logger.Port.pas
Mir-Logger/Logger.Options.pas
Mir-Logger/Logger.pas

Port

pascal
ILogger = interface
  procedure Log(const Level: TLogLevel; const Message: string);
  procedure Debug(const Message: string);
  procedure Info(const Message: string);
  procedure Warning(const Message: string);
  procedure Error(const Message: string);
end;

Log levels

Logger.Port.pas defines:

pascal
TLogLevel = (llDebug, llInfo, llWarning, llError);

Logger.Options.pas maps string values from configuration to this enum.

Valid configured values:

  • Debug
  • Info
  • Warning
  • Error

Values are normalized using trim and uppercase comparison.

Options

TLoggerOptions is an example options section:

pascal
type
  TLoggerOptions = class(TOptionsSection)
  private
    FLogLevel: string;
    FFilePath: string;

    function GetSectionName: string; override;
  public
    property LogLevel: string read FLogLevel write FLogLevel;
    property FilePath: string read FFilePath write FFilePath;
  end;

The section name is defined by GetSectionName:

pascal
function TLoggerOptions.GetSectionName: string;
begin
  Result := 'Logger';
end;

The root app config must contain:

json
{
  "Logger": {
    "LogLevel": "Info",
    "FilePath": "./Logs/App.log"
  }
}

Registration

The framework does not register the logger by default. The example application registers TLogger and its options in dpr or bootstrap unit:

pascal
App.AddOptions<TLoggerOptions>;
App.AddSingleton<ILogger, TLogger>;

TLogger receives its configuration through constructor injection:

pascal
constructor TLogger.Create(const AOptions: IOptions<TLoggerOptions>);

Behavior

TLogger writes to:

  • console, with ANSI colors by level;
  • file, using the configured FilePath.

It validates:

  • LogLevel is present;
  • FilePath is present;
  • the log file path is valid;
  • the log file can be opened.

Writes are synchronized using TCriticalSection.