AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Projekte Open Source "Logging Facade" für Pascal auf Github
Thema durchsuchen
Ansicht
Themen-Optionen

Open Source "Logging Facade" für Pascal auf Github

Ein Thema von mjustin · begonnen am 27. Jan 2016 · letzter Beitrag vom 7. Feb 2016
 
Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#8

AW: Open Source "Logging Facade" für Pascal auf Github

  Alt 30. Jan 2016, 12:42
Wenn du einen Logger implementierst, dann musst du immer ganz schön viel Boilerplate-Code schreiben.

Da würde ich noch ein Interface mit Wrapper dazwischen schieben:
Delphi-Quellcode:
unit djLogWriter;

interface

uses
  System.SysUtils,
  djLogAPI;

type
  TLogLevel = ( llTrace, llDebug, llInfo, llWarn, llError );
  TLogLevels = set of TLogLevel;

type
  IWriteLog = interface
    procedure WriteMessage( const ALevel: TLogLevel; const AName, AMessage: string; const AException: Exception );
  end;

  TLogWriterWrapper = class( TInterfacedObject, ILogger )
  private { fields }
    FName : string;
    FWriter : IWriteLog;
    FLogFilter: TLogLevels;
    procedure SendToWriter( const ALevel: TLogLevel; const AMessage: string; const AException: Exception ); inline;
  private { ILogger }
    procedure Debug( const AMsg: string ); overload;
    procedure Debug( const AFormat: string; const AArgs: array of const ); overload;
    procedure Debug( const AMsg: string; const AException: Exception ); overload;

    procedure Error( const AMsg: string ); overload;
    procedure Error( const AFormat: string; const AArgs: array of const ); overload;
    procedure Error( const AMsg: string; const AException: Exception ); overload;

    procedure Info( const AMsg: string ); overload;
    procedure Info( const AFormat: string; const AArgs: array of const ); overload;
    procedure Info( const AMsg: string; const AException: Exception ); overload;

    procedure Warn( const AMsg: string ); overload;
    procedure Warn( const AFormat: string; const AArgs: array of const ); overload;
    procedure Warn( const AMsg: string; const AException: Exception ); overload;

    procedure Trace( const AMsg: string ); overload;
    procedure Trace( const AFormat: string; const AArgs: array of const ); overload;
    procedure Trace( const AMsg: string; const AException: Exception ); overload;

    function IsDebugEnabled: Boolean;
    function IsErrorEnabled: Boolean;
    function IsInfoEnabled: Boolean;
    function IsWarnEnabled: Boolean;
    function IsTraceEnabled: Boolean;

    function Name: string;
  public
    constructor Create( const AName: string; const AWriter: IWriteLog; const ALogFilter: TLogLevels );
  end;

implementation

{ TLogWriterWrapper }

constructor TLogWriterWrapper.Create( const AName: string; const AWriter: IWriteLog; const ALogFilter: TLogLevels );
begin
  inherited Create;
  FName := AName;
  FWriter := AWriter;
  FLogFilter := ALogFilter;
end;

procedure TLogWriterWrapper.Debug( const AMsg: string );
begin
  SendToWriter( llDebug, AMsg, nil );
end;

procedure TLogWriterWrapper.Debug( const AMsg: string; const AException: Exception );
begin
  SendToWriter( llDebug, AMsg, AException );
end;

procedure TLogWriterWrapper.Debug( const AFormat: string; const AArgs: array of const );
begin
  SendToWriter( llDebug, Format( AFormat, AArgs ), nil );
end;

procedure TLogWriterWrapper.Error( const AMsg: string );
begin
  SendToWriter( llError, AMsg, nil );
end;

procedure TLogWriterWrapper.Error( const AFormat: string; const AArgs: array of const );
begin
  SendToWriter( llError, Format( AFormat, AArgs ), nil );
end;

procedure TLogWriterWrapper.Error( const AMsg: string; const AException: Exception );
begin
  SendToWriter( llError, AMsg, AException );
end;

procedure TLogWriterWrapper.Info( const AFormat: string; const AArgs: array of const );
begin
  SendToWriter( llInfo, Format( AFormat, AArgs ), nil );
end;

procedure TLogWriterWrapper.Info( const AMsg: string );
begin
  SendToWriter( llInfo, AMsg, nil );
end;

procedure TLogWriterWrapper.Info( const AMsg: string; const AException: Exception );
begin
  SendToWriter( llInfo, AMsg, AException );
end;

function TLogWriterWrapper.IsDebugEnabled: Boolean;
begin
  Result := not( llDebug in FLogFilter );
end;

function TLogWriterWrapper.IsErrorEnabled: Boolean;
begin
  Result := not( llError in FLogFilter );
end;

function TLogWriterWrapper.IsInfoEnabled: Boolean;
begin
  Result := not( llInfo in FLogFilter );
end;

function TLogWriterWrapper.IsTraceEnabled: Boolean;
begin
  Result := not( llTrace in FLogFilter );
end;

function TLogWriterWrapper.IsWarnEnabled: Boolean;
begin
  Result := not( llWarn in FLogFilter );
end;

function TLogWriterWrapper.Name: string;
begin
  Result := FName;
end;

procedure TLogWriterWrapper.SendToWriter( const ALevel: TLogLevel; const AMessage: string; const AException: Exception );
begin
  if not( ALevel in FLogFilter )
  then
    FWriter.WriteMessage( ALevel, FName, AMessage, AException );
end;

procedure TLogWriterWrapper.Trace( const AMsg: string );
begin
  SendToWriter( llTrace, AMsg, nil );
end;

procedure TLogWriterWrapper.Trace( const AFormat: string; const AArgs: array of const );
begin
  SendToWriter( llTrace, Format( AFormat, AArgs ), nil );
end;

procedure TLogWriterWrapper.Trace( const AMsg: string; const AException: Exception );
begin
  SendToWriter( llTrace, AMsg, AException );
end;

procedure TLogWriterWrapper.Warn( const AMsg: string; const AException: Exception );
begin
  SendToWriter( llWarn, AMsg, AException );
end;

procedure TLogWriterWrapper.Warn( const AMsg: string );
begin
  SendToWriter( llWarn, AMsg, nil );
end;

procedure TLogWriterWrapper.Warn( const AFormat: string; const AArgs: array of const );
begin
  SendToWriter( llWarn, Format( AFormat, AArgs ), nil );
end;

end.
Mit einem Wrapper, kann man jetzt ganz kleine süße Log-Writer erstellen, die einfach nur IWriteLog implementieren.
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 16:00 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz