Skip to content

Commit

Permalink
Added LogFmt support in LoggerPro
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Jan 8, 2025
1 parent 51288f8 commit 0a19f3b
Show file tree
Hide file tree
Showing 10 changed files with 1,431 additions and 29 deletions.
28 changes: 27 additions & 1 deletion lib/loggerpro/LoggerPro.ConsoleAppender.pas
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ TLoggerProConsoleAppender = class(TLoggerProAppenderBase)
procedure WriteLog(const aLogItem: TLogItem); override;
end;

TLoggerProConsoleLogFmtAppender = class(TLoggerProConsoleAppender)
public
constructor Create(ALogItemRenderer: ILogItemRenderer = nil); override;
function FormatLog(const ALogItem: TLogItem): string; override;
end;

// for some reason, AttachConsole has been left out of Winapi.windows.pas
function AttachConsole(PID: Cardinal): LongBool; stdcall;

Expand All @@ -74,7 +80,8 @@ implementation

uses
Winapi.Windows,
Winapi.Messages;
Winapi.Messages,
LoggerPro.Renderers;

// for some reason, AttachConsole has been left out of Winapi.windows.pas
const
Expand Down Expand Up @@ -186,4 +193,23 @@ procedure TLoggerProConsoleAppender.WriteLog(const aLogItem: TLogItem);
end;
end;

{ TLoggerProConsoleLogFmtAppender }

constructor TLoggerProConsoleLogFmtAppender.Create(ALogItemRenderer: ILogItemRenderer);
begin
inherited Create(TLogItemRendererLogFmt.Create);
end;

function TLoggerProConsoleLogFmtAppender.FormatLog(const ALogItem: TLogItem): string;
begin
if Assigned(FOnLogRow) then
begin
FOnLogRow(ALogItem, Result);
end
else
begin
Result := FLogItemRenderer.RenderLogItem(ALogItem);
end;
end;

end.
51 changes: 50 additions & 1 deletion lib/loggerpro/LoggerPro.FileAppender.pas
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,17 @@ TLoggerProFileByFolderAppender = class(TLoggerProFileAppender)
procedure TearDown; override;
end;

TLoggerProLogFmtFileAppender = class(TLoggerProSimpleFileAppender)
protected
function GetLogFileName(const aTag: string; const aFileNumber: Integer): string; override;
procedure EmitStartRotateLogItem(aWriter: TStreamWriter); override;
procedure EmitEndRotateLogItem(aWriter: TStreamWriter); override;
public
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);
reintroduce;
end;

implementation

Expand All @@ -196,8 +207,9 @@ implementation
System.StrUtils,
System.Math,
System.DateUtils,
LoggerPro.Renderers,
idGlobal
{$IF Defined(Android), System.SysUtils}
{$IF Defined(Android)}
,Androidapi.Helpers
,Androidapi.JNI.GraphicsContentViewText
,Androidapi.JNI.JavaTypes
Expand Down Expand Up @@ -702,5 +714,42 @@ procedure TLoggerProFileByFolderAppender.WriteLog(const ALogItem: TLogItem);
end;
end;

{ TLoggerProLogFmtFileAppender }

constructor TLoggerProLogFmtFileAppender.Create(aMaxBackupFileCount, aMaxFileSizeInKiloByte: Integer; aLogsFolder,
aLogFileNameFormat: string; aEncoding: TEncoding);
begin
inherited Create(
aMaxBackupFileCount,
aMaxFileSizeInKiloByte,
aLogsFolder,
aLogFileNameFormat,
TLogItemRendererLogFmt.Create,
aEncoding);
end;

procedure TLoggerProLogFmtFileAppender.EmitEndRotateLogItem(aWriter: TStreamWriter);
begin
// do nothing
end;

procedure TLoggerProLogFmtFileAppender.EmitStartRotateLogItem(aWriter: TStreamWriter);
begin
// do nothing
end;

function TLoggerProLogFmtFileAppender.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, '.logfmt' + lOrigExt);
end;

end.

40 changes: 40 additions & 0 deletions lib/loggerpro/LoggerPro.Renderers.pas
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ TLogItemRendererNoTagNoThreadID = class(TLogItemRendererDefault)
function RenderLogItem(const aLogItem: TLogItem): String; override;
end;

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


function GetDefaultLogItemRenderer: ILogItemRenderer;

Expand All @@ -71,6 +81,9 @@ function GetDefaultLogItemRenderer: ILogItemRenderer;

implementation

uses
System.DateUtils;

function GetDefaultLogItemRenderer: ILogItemRenderer;
begin
Result := gDefaultLogItemRenderer.Create;
Expand Down Expand Up @@ -161,5 +174,32 @@ function TLogItemRendererNoTagNoThreadID.RenderLogItem(const aLogItem: TLogItem)



{ TLogItemRendererLogFmt }

function TLogItemRendererLogFmt.RenderLogItem(const aLogItem: TLogItem): String;
begin
Result :=
Format('time=%s threadid=%d type=%s msg=%s tag=%s',
[
DateToISO8601(ALogItem.TimeStamp, False),
ALogItem.ThreadID,
ALogItem.LogTypeAsString,
ALogItem.LogMessage.QuotedString('"'),
ALogItem.LogTag
]);
end;

procedure TLogItemRendererLogFmt.Setup;
begin
inherited;
fFormatSettings := GetDefaultFormatSettings;
end;

procedure TLogItemRendererLogFmt.TearDown;
begin
inherited;

end;


end.
20 changes: 14 additions & 6 deletions lib/loggerpro/LoggerPro.SimpleConsoleAppender.pas
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,19 @@ interface
}
TLoggerProSimpleConsoleAppender = class(TLoggerProAppenderBase)
public
procedure Setup; override;
procedure TearDown; override;
procedure WriteLog(const aLogItem: TLogItem); override;
end;

implementation
TLoggerProSimpleConsoleLogFmtAppender = class(TLoggerProSimpleConsoleAppender)
public
constructor Create; override;
end;

implementation

procedure TLoggerProSimpleConsoleAppender.Setup;
begin
inherited;
end;
uses
LoggerPro.Renderers;

procedure TLoggerProSimpleConsoleAppender.TearDown;
begin
Expand All @@ -67,4 +68,11 @@ procedure TLoggerProSimpleConsoleAppender.WriteLog(const aLogItem: TLogItem);
Writeln(FormatLog(aLogItem));
end;

{ TLoggerProSimpleConsoleLogFmtAppender }

constructor TLoggerProSimpleConsoleLogFmtAppender.Create;
begin
inherited Create(TLogItemRendererLogFmt.Create);
end;

end.
12 changes: 9 additions & 3 deletions lib/loggerpro/LoggerPro.pas
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,15 @@ TLoggerProAppenderBase = class abstract(TInterfacedObject, ILogAppender)
FLogLevel: TLogType;
FEnabled: Boolean;
FLastErrorTimeStamp: TDateTime;
FOnLogRow: TOnAppenderLogRow;
FLogItemRenderer: ILogItemRenderer;
FFormatSettings: TFormatSettings;
protected
FOnLogRow: TOnAppenderLogRow;
FLogItemRenderer: ILogItemRenderer;
property FormatSettings: TFormatSettings read FFormatSettings;
property LogItemRenderer: ILogItemRenderer read FLogItemRenderer;
public
constructor Create(ALogItemRenderer: ILogItemRenderer = nil); virtual;
constructor Create(ALogItemRenderer: ILogItemRenderer); overload; virtual;
constructor Create; overload; virtual;
procedure Setup; virtual;
function FormatLog(const ALogItem: TLogItem): string; virtual;
procedure WriteLog(const aLogItem: TLogItem); virtual; abstract;
Expand Down Expand Up @@ -887,6 +888,11 @@ constructor TLoggerProAppenderBase.Create(aLogItemRenderer: ILogItemRenderer);
Self.FOnLogRow := nil;
end;

constructor TLoggerProAppenderBase.Create;
begin
Create(nil);
end;

function TLoggerProAppenderBase.FormatLog(const ALogItem: TLogItem): string;
begin
if Assigned(FOnLogRow) then
Expand Down
37 changes: 37 additions & 0 deletions lib/loggerpro/samples/06_logfmt_appender/LoggerProConfig.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
unit LoggerProConfig;

interface

uses
LoggerPro, LoggerPro.Renderers;

function Log: ILogWriter;

implementation

uses
LoggerPro.ConsoleAppender,
LoggerPro.FileAppender,
Winapi.Windows;

var
_Log: ILogWriter;

function Log: ILogWriter;
begin
Result := _Log;
end;

initialization



LoggerPro.Renderers.gDefaultLogItemRenderer := TLogItemRendererNoTag; //optional

_Log := BuildLogWriter([
TLoggerProConsoleLogFmtAppender.Create,
TLoggerProLogFmtFileAppender.Create]);
if not IsConsole then
AllocConsole;

end.
16 changes: 16 additions & 0 deletions lib/loggerpro/samples/06_logfmt_appender/logfmt_appender.dpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
program logfmt_appender;

uses
Vcl.Forms,
LoggerProConfig in 'LoggerProConfig.pas',
MainFormU in '..\common\MainFormU.pas' {MainForm};

{$R *.res}

begin
ReportMemoryLeaksOnShutdown := True;
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TMainForm, MainForm);
Application.Run;
end.
Loading

0 comments on commit 0a19f3b

Please sign in to comment.