forked from danieleteti/loggerpro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoggerPro.JSONLFileAppender.pas
113 lines (94 loc) · 3.16 KB
/
LoggerPro.JSONLFileAppender.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
unit LoggerPro.JSONLFileAppender;
{ <@abstract(The unit to include if you want to use @link(TLoggerProJSONLFileAppender))
@author(Daniele Teti) }
{$DEFINE USE_JDO}
interface
uses
LoggerPro.FileAppender,
System.Classes,
LoggerPro,
System.SysUtils;
type
{ @abstract(JSONL file appender with multiple tags)
This file appender writes all TLogItems into a single log file as JSONL
}
TLoggerProJSONLFileAppender = class(TLoggerProSimpleFileAppender)
protected
function GetLogFileName(const aTag: string; const aFileNumber: Integer): string; override;
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 = ''; aFileAppenderOptions: TFileAppenderOptions = [];
aLogFileNameFormat: string = TLoggerProSimpleFileAppender.DEFAULT_FILENAME_FORMAT; aEncoding: TEncoding = nil);
reintroduce;
end;
implementation
uses
System.IOUtils,
{$IF Defined(USE_JDO)}
JsonDataObjects
{$ELSE}
System.JSON
{$ENDIF}
;
{ TLoggerProJSONLFileAppender }
constructor TLoggerProJSONLFileAppender.Create(
aMaxBackupFileCount, aMaxFileSizeInKiloByte: Integer;
aLogsFolder: string;
aFileAppenderOptions: TFileAppenderOptions;
aLogFileNameFormat: string;
aEncoding: TEncoding);
begin
inherited Create(aMaxBackupFileCount, aMaxFileSizeInKiloByte,
aLogsFolder, aFileAppenderOptions, aLogFileNameFormat,
DEFAULT_LOG_FORMAT, aEncoding);
end;
procedure TLoggerProJSONLFileAppender.EmitEndRotateLogItem(aWriter: TStreamWriter);
begin
// do nothing
end;
procedure TLoggerProJSONLFileAppender.EmitStartRotateLogItem(aWriter: TStreamWriter);
begin
// do nothing
end;
function TLoggerProJSONLFileAppender.FormatLog(const ALogItem: TLogItem): string;
var
lJSON: TJsonObject;
begin
lJSON := TJSONObject.Create;
try
{$IF Defined(USE_JDO)}
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.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('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;
begin
lOrigFName := inherited;
lOrigExt := TPath.GetExtension(lOrigFName);
if lOrigExt.IsEmpty then
begin
lOrigExt := '.log';
end;
Result := TPath.ChangeExtension(lOrigFName, '.jsonl' + lOrigExt);
end;
end.