forked from danieleteti/loggerpro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoggerPro.RESTAppender.pas
308 lines (279 loc) · 8.84 KB
/
LoggerPro.RESTAppender.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
unit LoggerPro.RESTAppender;
interface
uses
Classes,
SysUtils,
LoggerPro,
System.Net.HttpClient;
type
{
Log appender for a REST endpoint
Author: Daniele Teti (https://github.com/danieleteti/)
Some ideas from NSQ appender from Stéphane "Fulgan" GROBETY (https://github.com/Fulgan/)
}
TOnCreateData = reference to procedure(const Sender: TObject; const LogItem: TLogItem; const ExtendedInfo: TLoggerProExtendedInfo;
var Data: TStream);
TOnNetSendError = reference to procedure(const Sender: TObject; const LogItem: TLogItem; const NetError: Exception;
var RetryCount: Integer);
TLoggerProRESTAppender = class(TLoggerProAppenderBase, ILogAppender)
strict private
FOnCreateData: TOnCreateData;
FOnNetSendError: TOnNetSendError;
fExtendedInfo: TLoggerProExtendedInfo;
fContentType: string;
fRESTUrl: string;
{.$IFDEF MSWINDOWS}
fExtendedInfoData: array [low(TLogExtendedInfo) .. high(TLogExtendedInfo)] of string;
{.$ENDIF}
procedure SetOnCreateData(const Value: TOnCreateData);
procedure SetOnNetSendError(const Value: TOnNetSendError);
strict protected
procedure LoadExtendedInfo;
function GetExtendedInfo: string;
protected const
{ @abstract(Defines the default format string used by the @link(TLoggerProRESTAppender).)
The positional parameters are the following:
@orderedList(
@item SetNumber 0
@item TimeStamp
@item ThreadID
@item LogType
@item LogMessage
@item Extended Information
@item LogTag
)
}
DEFAULT_LOG_FORMAT = '%0:s [TID %1:10u][%2:-8s] %3:s {EI%4:s}[%5:s]';
DEFAULT_EXTENDED_INFO = [TLogExtendedInfo.EIUserName, TLogExtendedInfo.EIComputerName, TLogExtendedInfo.EIProcessName,
TLogExtendedInfo.EIProcessID, TLogExtendedInfo.EIDeviceID];
DEFAULT_REST_URL = 'http://127.0.0.1:8080/api/logs';
procedure InternalWriteLog(const aURI: string; const aLogItem: TLogItem; const aStream: TStream);
public
function GetRESTUrl: string;
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;
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
uses
System.NetEncoding,
System.IOUtils,
System.Net.URLClient
{$IF Defined(MSWINDOWS) }
,
Winapi.Windows
{$ENDIF}
{$IF Defined(Android) }
,
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.Os,
Androidapi.JNI.Util,
Androidapi.Helpers
{$ENDIF}
;
{$IFDEF MSWINDOWS }
function GetUserFromWindows: string;
var
iLen: Cardinal;
begin
iLen := 256;
Result := StringOfChar(#0, iLen);
GetUserName(PChar(Result), iLen);
SetLength(Result, iLen - 1);
end;
function GetComputerNameFromWindows: string;
var
iLen: Cardinal;
begin
iLen := MAX_COMPUTERNAME_LENGTH + 1;
Result := StringOfChar(#0, iLen);
GetComputerName(PChar(Result), iLen);
SetLength(Result, iLen);
end;
{$ENDIF}
constructor TLoggerProRESTAppender.Create(aRESTUrl: string = DEFAULT_REST_URL; aContentType: string = 'text/plain';
aLogExtendedInfo: TLoggerProExtendedInfo = DEFAULT_EXTENDED_INFO; aLogFormat: string = DEFAULT_LOG_FORMAT);
begin
inherited Create(aLogFormat);
fRESTUrl := aRESTUrl;
fExtendedInfo := aLogExtendedInfo;
fContentType := aContentType;
LoadExtendedInfo;
end;
function TLoggerProRESTAppender.CreateData(const SrcLogItem: TLogItem): TStream;
begin
Result := nil;
try
if Assigned(FOnCreateData) then
begin
FOnCreateData(Self, SrcLogItem, fExtendedInfo, Result);
end
else
begin
Result := TStringStream.Create(FormatLog(SrcLogItem), TEncoding.UTF8);
end;
except
on E: Exception do
begin
FreeAndNil(Result);
raise;
end;
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;
function TLoggerProRESTAppender.GetExtendedInfo: string;
begin
Result := '';
{$IFDEF MSWINDOWS}
if TLogExtendedInfo.EIUserName in fExtendedInfo then
begin
Result := Result + ';UserName=' + fExtendedInfoData[TLogExtendedInfo.EIUserName];
end;
if TLogExtendedInfo.EIComputerName in fExtendedInfo then
begin
Result := Result + ';ComputerName=' + fExtendedInfoData[TLogExtendedInfo.EIComputerName];
end;
if TLogExtendedInfo.EIProcessName in fExtendedInfo then
begin
Result := Result + ';ProcessName=' + fExtendedInfoData[TLogExtendedInfo.EIProcessName];
end;
if TLogExtendedInfo.EIProcessID in fExtendedInfo then
begin
Result := Result + ';PID=' + fExtendedInfoData[TLogExtendedInfo.EIProcessID];
end;
{$ENDIF}
{$IF Defined(Android)}
if TLogExtendedInfo.EIProcessName in fExtendedInfo then
begin
Result := Result + ';ProcessName=' + fExtendedInfoData[TLogExtendedInfo.EIProcessName];
end;
{$ENDIF}
Result := '[' + Result.Substring(1) + ']';
end;
function TLoggerProRESTAppender.GetRESTUrl: string;
begin
Result := fRESTUrl;
end;
procedure TLoggerProRESTAppender.LoadExtendedInfo;
begin
{$IF DEFINED(MSWINDOWS)}
if TLogExtendedInfo.EIProcessID in fExtendedInfo then
begin
fExtendedInfoData[TLogExtendedInfo.EIProcessID] := IntToStr(GetCurrentProcessId);
end;
if TLogExtendedInfo.EIUserName in fExtendedInfo then
begin
fExtendedInfoData[TLogExtendedInfo.EIUserName] := GetUserFromWindows;
end;
if TLogExtendedInfo.EIComputerName in fExtendedInfo then
begin
fExtendedInfoData[TLogExtendedInfo.EIComputerName] := GetComputerNameFromWindows;
end;
if TLogExtendedInfo.EIProcessName in fExtendedInfo then
begin
fExtendedInfoData[TLogExtendedInfo.EIProcessName] := TPath.GetFileName(GetModuleName(HInstance));
end;
if TLogExtendedInfo.EIProcessID in fExtendedInfo then
begin
fExtendedInfoData[TLogExtendedInfo.EIProcessID] := IntToStr(GetCurrentProcessId);
end;
{$ENDIF}
{$IF Defined(Android)}
if TLogExtendedInfo.EIProcessName in fExtendedInfo then
begin
fExtendedInfoData[TLogExtendedInfo.EIProcessName] := TAndroidHelper.ApplicationTitle;
end;
{$ENDIF}
end;
procedure TLoggerProRESTAppender.SetRESTUrl(const Value: string);
begin
fRESTUrl := Value;
end;
procedure TLoggerProRESTAppender.SetOnCreateData(const Value: TOnCreateData);
begin
FOnCreateData := Value;
end;
procedure TLoggerProRESTAppender.SetOnNetSendError(const Value: TOnNetSendError);
begin
FOnNetSendError := Value;
end;
procedure TLoggerProRESTAppender.Setup;
begin
inherited;
end;
procedure TLoggerProRESTAppender.TearDown;
begin
inherited;
end;
procedure TLoggerProRESTAppender.InternalWriteLog(const aURI: string; const aLogItem: TLogItem; const aStream: TStream);
var
lHTTPCli: THTTPClient;
lRetryCount: Integer;
lResp: IHTTPResponse;
const
MAX_RETRY_COUNT = 5;
begin
lRetryCount := 0;
lHTTPCli := THTTPClient.Create;
try
if Assigned(aStream) then
begin
repeat
try
{$IF CompilerVersion >= 31}
lHTTPCli.ConnectionTimeout := 1000;
lHTTPCli.ResponseTimeout := 3000;
{$ENDIF}
aStream.Seek(0, soFromBeginning);
lResp := lHTTPCli.Post(aURI, aStream, nil, [TNetHeader.Create('content-type', fContentType)]);
if not(lResp.StatusCode in [200, 201]) then
begin
raise ELoggerPro.Create(lResp.ContentAsString);
end;
Break;
except
on E: Exception do
begin
// if there is an event handler for net exception, call it
if Assigned(FOnNetSendError) then
OnNetSendError(Self, aLogItem, E, lRetryCount);
Inc(lRetryCount);
// if the handler has set FRetryCount to a positive value then retry the call
if lRetryCount >= MAX_RETRY_COUNT then
break;
end;
end;
until False;
end;
finally
FreeAndNil(lHTTPCli);
end;
end;
procedure TLoggerProRESTAppender.WriteLog(const aLogItem: TLogItem);
var
lURI: string;
lData: TStream;
begin
lURI := RESTUrl + '/' + TNetEncoding.URL.Encode(aLogItem.LogTag.Trim) + '/' + TNetEncoding.URL.Encode(aLogItem.LogTypeAsString);
lData := CreateData(aLogItem);
try
if Assigned(lData) then
InternalWriteLog(lURI, aLogItem, lData);
finally
lData.Free;
end;
end;
end.