Skip to content

Commit

Permalink
Updated LoggerPro to v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Jan 23, 2024
2 parents fada5f5 + 88cb06e commit 7ec97f4
Show file tree
Hide file tree
Showing 43 changed files with 611 additions and 310 deletions.
6 changes: 3 additions & 3 deletions lib/loggerpro/LoggerPro.EMailAppender.pas
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ TLoggerProEMailAppender = class(TLoggerProAppenderBase)
/// </summary>
procedure PrepareMessage(const aLogItem: TLogItem; out aSubject, aBody: String); virtual;
public
constructor Create(aSMTP: TIdSMTP; const aFromAddresses, aToAddresses: String; aLogLayout: string = TLogLayout.LOG_LAYOUT_0); reintroduce;
constructor Create(aSMTP: TIdSMTP; const aFromAddresses, aToAddresses: String; aLogItemRenderer: ILogItemRenderer = nil); reintroduce;
procedure Setup; override;
procedure TearDown; override;
procedure WriteLog(const aLogItem: TLogItem); override;
Expand All @@ -60,9 +60,9 @@ implementation
uses
System.SysUtils, IdMessage;

constructor TLoggerProEMailAppender.Create(aSMTP: TIdSMTP; const aFromAddresses, aToAddresses: String; aLogLayout: string);
constructor TLoggerProEMailAppender.Create(aSMTP: TIdSMTP; const aFromAddresses, aToAddresses: String; aLogItemRenderer: ILogItemRenderer);
begin
inherited Create(aLogLayout);
inherited Create(aLogItemRenderer);
FSMTP := aSMTP;
FFromAddresses := aFromAddresses;
FToAddresses := aToAddresses;
Expand Down
13 changes: 7 additions & 6 deletions lib/loggerpro/LoggerPro.FileAppender.pas
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ TLoggerProFileAppenderBase = class(TLoggerProAppenderBase)
aMaxFileSizeInKiloByte: Integer = TLoggerProFileAppenderBase.DEFAULT_MAX_FILE_SIZE_KB;
aLogsFolder: string = '';
aLogFileNameFormat: string = TLoggerProFileAppenderBase.DEFAULT_FILENAME_FORMAT;
aLogLayout: string = TLogLayout.LOG_LAYOUT_0;
aLogItemRenderer: ILogItemRenderer = nil;
aEncoding: TEncoding = nil);
reintroduce; virtual;
procedure Setup; override;
Expand Down Expand Up @@ -151,7 +151,7 @@ TLoggerProSimpleFileAppender = class(TLoggerProFileAppenderBase)
aMaxFileSizeInKiloByte: Integer = TLoggerProFileAppenderBase.DEFAULT_MAX_FILE_SIZE_KB;
aLogsFolder: string = '';
aLogFileNameFormat: string = TLoggerProSimpleFileAppender.DEFAULT_FILENAME_FORMAT;
aLogLayout: string = TLogLayout.LOG_LAYOUT_0;
aLogItemRenderer: ILogItemRenderer = nil;
aEncoding: TEncoding = nil);
override;
end;
Expand Down Expand Up @@ -312,10 +312,10 @@ constructor TLoggerProFileAppenderBase.Create(
aMaxFileSizeInKiloByte: Integer;
aLogsFolder: string;
aLogFileNameFormat: string;
aLogLayout: string;
aLogItemRenderer: ILogItemRenderer;
aEncoding: TEncoding);
begin
inherited Create(aLogLayout);
inherited Create(aLogItemRenderer);
fLogsFolder := aLogsFolder;
fMaxBackupFileCount:= Max(1, aMaxBackupFileCount);
fMaxFileSizeInKiloByte := aMaxFileSizeInKiloByte;
Expand Down Expand Up @@ -451,15 +451,16 @@ procedure TLoggerProSimpleFileAppender.CheckLogFileNameFormat(const LogFileNameF
end;

constructor TLoggerProSimpleFileAppender.Create(aMaxBackupFileCount, aMaxFileSizeInKiloByte: Integer;
aLogsFolder: string; aLogFileNameFormat, aLogLayout: string;
aLogsFolder: string; aLogFileNameFormat: String;
aLogItemRenderer: ILogItemRenderer;
aEncoding: TEncoding);
begin
inherited Create(
aMaxBackupFileCount,
aMaxFileSizeInKiloByte,
aLogsFolder,
aLogFileNameFormat,
aLogLayout,
aLogItemRenderer,
aEncoding);
end;

Expand Down
61 changes: 41 additions & 20 deletions lib/loggerpro/LoggerPro.JSONLFileAppender.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// LoggerPro
//
// Copyright (c) 2010-2023 Daniele Teti
// Copyright (c) 2010-2024 Daniele Teti
//
// https://github.com/danieleteti/loggerpro
//
Expand Down Expand Up @@ -32,8 +32,7 @@ interface
uses
LoggerPro.FileAppender,
System.Classes,
LoggerPro,
System.SysUtils;
LoggerPro, System.SysUtils;

type
{ @abstract(JSONL file appender with multiple tags)
Expand All @@ -45,7 +44,6 @@ TLoggerProJSONLFileAppender = class(TLoggerProSimpleFileAppender)
procedure EmitStartRotateLogItem(aWriter: TStreamWriter); override;
procedure EmitEndRotateLogItem(aWriter: TStreamWriter); override;
public
function FormatLog(const ALogItem: TLogItem): string; override;
constructor Create(aMaxBackupFileCount: Integer = TLoggerProFileAppender.DEFAULT_MAX_BACKUP_FILE_COUNT;
aMaxFileSizeInKiloByte: Integer = TLoggerProFileAppender.DEFAULT_MAX_FILE_SIZE_KB; aLogsFolder: string = '';
aLogFileNameFormat: string = TLoggerProSimpleFileAppender.DEFAULT_FILENAME_FORMAT; aEncoding: TEncoding = nil);
Expand All @@ -65,6 +63,18 @@ implementation
;


type
TLogItemRendererJSONL = class(TLogItemRenderer)
private
fFormatSettings: TFormatSettings;
protected
// ILogLayoutRenderer
procedure Setup; override;
procedure TearDown; override;
function RenderLogItem(const aLogItem: TLogItem): String; override;
end;


{ TLoggerProJSONLFileAppender }

constructor TLoggerProJSONLFileAppender.Create(
Expand All @@ -78,7 +88,7 @@ constructor TLoggerProJSONLFileAppender.Create(
aMaxFileSizeInKiloByte,
aLogsFolder,
aLogFileNameFormat,
TLogLayout.LOG_LAYOUT_0,
TLogItemRendererJSONL.Create,
aEncoding);
end;

Expand All @@ -92,7 +102,23 @@ procedure TLoggerProJSONLFileAppender.EmitStartRotateLogItem(aWriter: TStreamWri
// do nothing
end;

function TLoggerProJSONLFileAppender.FormatLog(const ALogItem: TLogItem): string;
function TLoggerProJSONLFileAppender.GetLogFileName(const aTag: string; const aFileNumber: Integer): string;
var
lOrigFName, lOrigExt: string;
begin
lOrigFName := inherited;
lOrigExt := TPath.GetExtension(lOrigFName);
if lOrigExt.IsEmpty then
begin
lOrigExt := '.log';
end;
Result := TPath.ChangeExtension(lOrigFName, '.jsonl' + lOrigExt);
end;


{ TLogItemRendererJSONL }

function TLogItemRendererJSONL.RenderLogItem(const aLogItem: TLogItem): String;
var
lJSON: TJsonObject;
begin
Expand All @@ -102,36 +128,31 @@ function TLoggerProJSONLFileAppender.FormatLog(const ALogItem: TLogItem): string
lJSON.S['type'] := ALogItem.LogTypeAsString;
lJSON.S['message'] := ALogItem.LogMessage;
lJSON.S['tag'] := ALogItem.LogTag;
lJSON.S['ts'] := DateTimeToStr(ALogItem.TimeStamp, Self.FormatSettings).TrimRight;
lJSON.S['ts'] := DateTimeToStr(ALogItem.TimeStamp, fFormatSettings).TrimRight;
lJSON.I['tid'] := ALogItem.ThreadID;
{$ELSE}
lJSON.AddPair('type', ALogItem.LogTypeAsString);
lJSON.AddPair('message', ALogItem.LogMessage);
lJSON.AddPair('tag', ALogItem.LogTag);
lJSON.AddPair('ts', DateTimeToStr(ALogItem.TimeStamp, Self.FormatSettings).TrimRight);
lJSON.AddPair('ts', DateTimeToStr(ALogItem.TimeStamp, fFormatSettings).TrimRight);
lJSON.AddPair('tid', ALogItem.ThreadID);
{$ENDIF}
Result := lJSON.ToJSON;
if Assigned(OnLogRow) then
OnLogRow(ALogItem, Result)
finally
lJSON.Free;
end;
end;

function TLoggerProJSONLFileAppender.GetLogFileName(const aTag: string; const aFileNumber: Integer): string;
var
lOrigFName, lOrigExt: string;
procedure TLogItemRendererJSONL.Setup;
begin
lOrigFName := inherited;
lOrigExt := TPath.GetExtension(lOrigFName);
if lOrigExt.IsEmpty then
begin
lOrigExt := '.log';
end;
Result := TPath.ChangeExtension(lOrigFName, '.jsonl' + lOrigExt);
inherited;
fFormatSettings := GetDefaultFormatSettings;
end;

procedure TLogItemRendererJSONL.TearDown;
begin
inherited;

end;

end.
6 changes: 3 additions & 3 deletions lib/loggerpro/LoggerPro.NSQAppender.pas
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ TLoggerProNSQAppenderBase = class(TLoggerProAppenderBase, ILogAppender)
/// creation event is defined </param>
constructor Create(aTopic: string=''; aEphemeral: Boolean = False;
aNSQUrl: string=DEFAULT_NSQ_URL;
aLogFormat: string=TLogLayout.LOG_LAYOUT_0);
aLogItemRenderer: ILogItemRenderer = nil);
reintroduce;
property NSQUrl: string read GetNSQUrl write SetNSQUrl;
property Ephemeral: Boolean read FEphemeral write SetEphemeral;
Expand All @@ -127,9 +127,9 @@ implementation
uses System.NetEncoding;

constructor TLoggerProNSQAppenderBase.Create(aTopic: string; aEphemeral: Boolean;
aNSQUrl: string; aLogFormat: string);
aNSQUrl: string; aLogItemRenderer: ILogItemRenderer);
begin
inherited Create(aLogFormat);
inherited Create(aLogItemRenderer);
FEphemeral := aEphemeral;
FNSQUrl := 'http://127.0.0.1:4151';
FUserName := aNSQUrl;
Expand Down
6 changes: 3 additions & 3 deletions lib/loggerpro/LoggerPro.OutputDebugStringAppender.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// LoggerPro
//
// Copyright (c) 2010-2023 Daniele Teti
// Copyright (c) 2010-2024 Daniele Teti
//
// https://github.com/danieleteti/loggerpro
//
Expand Down Expand Up @@ -39,7 +39,7 @@ TLoggerProOutputDebugStringAppender = class(TLoggerProAppenderBase)
FModuleName: string;
{$ENDIF}
public
constructor Create(ALogLayout: string = TLogLayout.LOG_LAYOUT_0); override;
constructor Create(aLogItemRenderer: ILogItemRenderer = nil); override;
procedure Setup; override;
procedure TearDown; override;
procedure WriteLog(const aLogItem: TLogItem); override;
Expand All @@ -58,7 +58,7 @@ implementation

{ TLoggerProOutputDebugStringAppender }

constructor TLoggerProOutputDebugStringAppender.Create(ALogLayout: string);
constructor TLoggerProOutputDebugStringAppender.Create(aLogItemRenderer: ILogItemRenderer);
begin
inherited;
end;
Expand Down
15 changes: 5 additions & 10 deletions lib/loggerpro/LoggerPro.RESTAppender.pas
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,13 @@ TLoggerProRESTAppender = class(TLoggerProAppenderBase, ILogAppender)
procedure SetRESTUrl(const Value: string);
procedure WriteLog(const aLogItem: TLogItem); override;
constructor Create(aRESTUrl: string = DEFAULT_REST_URL; aContentType: string = 'text/plain';
aLogExtendedInfo: TLoggerProExtendedInfo = DEFAULT_EXTENDED_INFO; aLogFormat: string = DEFAULT_LOG_FORMAT); reintroduce;
aLogExtendedInfo: TLoggerProExtendedInfo = DEFAULT_EXTENDED_INFO; aLogItemRenderer: ILogItemRenderer = nil); reintroduce;
property RESTUrl: string read GetRESTUrl write SetRESTUrl;
property OnCreateData: TOnCreateData read FOnCreateData write SetOnCreateData;
property OnNetSendError: TOnNetSendError read FOnNetSendError write SetOnNetSendError;
procedure TearDown; override;
procedure Setup; override;
function CreateData(const SrcLogItem: TLogItem): TStream; virtual;
function FormatLog(const aLogItem: TLogItem): string; override;
end;

implementation
Expand Down Expand Up @@ -139,10 +138,10 @@ function GetComputerNameFromWindows: string;
{$ENDIF}


constructor TLoggerProRESTAppender.Create(aRESTUrl: string = DEFAULT_REST_URL; aContentType: string = 'text/plain';
aLogExtendedInfo: TLoggerProExtendedInfo = DEFAULT_EXTENDED_INFO; aLogFormat: string = DEFAULT_LOG_FORMAT);
constructor TLoggerProRESTAppender.Create(aRESTUrl: string; aContentType: string;
aLogExtendedInfo: TLoggerProExtendedInfo; aLogItemRenderer: ILogItemRenderer);
begin
inherited Create(aLogFormat);
inherited Create(aLogItemRenderer);
fRESTUrl := aRESTUrl;
fExtendedInfo := aLogExtendedInfo;
fContentType := aContentType;
Expand Down Expand Up @@ -170,12 +169,8 @@ function TLoggerProRESTAppender.CreateData(const SrcLogItem: TLogItem): TStream;
end;
end;

function TLoggerProRESTAppender.FormatLog(const aLogItem: TLogItem): string;
begin
Result := Format(LogFormat, [datetimetostr(aLogItem.TimeStamp, FormatSettings), aLogItem.ThreadID, aLogItem.LogTypeAsString,
aLogItem.LogMessage, GetExtendedInfo, aLogItem.LogTag]);
end;

{TODO -oDanieleT -cGeneral : Currently ExtendedInfo are not logged}
function TLoggerProRESTAppender.GetExtendedInfo: string;
begin
Result := '';
Expand Down
6 changes: 3 additions & 3 deletions lib/loggerpro/LoggerPro.RedisAppender.pas
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ TLoggerProRedisAppender = class(TLoggerProAppenderBase)
FLogKeyPrefix: string;
FMaxSize: Int64;
public
constructor Create(aRedis: IRedisClient; aMaxSize: Int64 = 5000; aKeyPrefix: string = 'loggerpro'; aLogLayout: string = TLogLayout.LOG_LAYOUT_0); reintroduce;
constructor Create(aRedis: IRedisClient; aMaxSize: Int64 = 5000; aKeyPrefix: string = 'loggerpro'; aLogItemRenderer: ILogItemRenderer = nil); reintroduce;
procedure Setup; override;
procedure TearDown; override;
procedure WriteLog(const aLogItem: TLogItem); override;
Expand All @@ -55,9 +55,9 @@ implementation
uses
System.SysUtils;

constructor TLoggerProRedisAppender.Create(aRedis: IRedisClient; aMaxSize: Int64; aKeyPrefix: string; aLogLayout: string);
constructor TLoggerProRedisAppender.Create(aRedis: IRedisClient; aMaxSize: Int64; aKeyPrefix: string; aLogItemRenderer: ILogItemRenderer);
begin
inherited Create(aLogLayout);
inherited Create(aLogItemRenderer);
FRedis := aRedis;
FLogKeyPrefix := aKeyPrefix;
FMaxSize := aMaxSize;
Expand Down
Loading

0 comments on commit 7ec97f4

Please sign in to comment.