-
Notifications
You must be signed in to change notification settings - Fork 91
/
LoggerPro.DMSEventStreamsAppender.pas
393 lines (359 loc) · 11.4 KB
/
LoggerPro.DMSEventStreamsAppender.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// *************************************************************************** }
//
// LoggerPro
//
// Copyright (c) 2010-2024 Daniele Teti
//
// https://github.com/danieleteti/loggerpro
//
// ***************************************************************************
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ***************************************************************************
unit LoggerPro.DMSEventStreamsAppender;
interface
uses
System.Classes,
MVCFramework.Commons, {this demo requires DMVCFramework}
System.SysUtils,
LoggerPro,
System.Net.HttpClient,
EventStreamsRPCProxy,
JsonDataObjects;
type
{
Log appender for a DMSContainer EventStreams endpoint
Author: Daniele Teti (https://github.com/danieleteti/)
}
TOnCreateJSONData = reference to procedure(const Sender: TObject; const LogItem: TLogItem;
const ExtendedInfo: TLoggerProExtendedInfo;
Data: TJSONObject);
TOnNetSendError = reference to procedure(const Sender: TObject; const LogItem: TLogItem; const NetError: Exception;
var RetryCount: Integer);
{
dmsatByTag:
all messages with the same tag go in the same queue
dmsatByType:
all messages with the same type go in the same queue
dmsatByTagThenType:
messages are organized in one queue for each tag,
then one queue for each type (es. myapp.mytag.error, myapp.mytag.warning etc)
dmsatByTypeThenTag:
messages are organized in one queue for each type,
then one queue for each tag (es. myapp.error.mytag, myapp.warning.mytag etc)
}
TDMSQueueAggregationType = (dmsatByTag, dmsatByType, dmsatByTagThenType, dmsatByTypeThenTag);
TLoggerProDMSContainerAppender = class(TLoggerProAppenderBase, ILogAppender)
strict private
fOnCreateJSONData: TOnCreateJSONData;
fOnNetSendError: TOnNetSendError;
fExtendedInfo: TLoggerProExtendedInfo;
fEventStreamsProxy: TEventStreamsRPCProxy;
fDMSContainerAPIKey: String;
fExtendedInfoData: array [low(TLogExtendedInfo) .. high(TLogExtendedInfo)] of string;
procedure SetOnCreateJSONData(const Value: TOnCreateJSONData);
procedure SetOnNetSendError(const Value: TOnNetSendError);
private
fQueueNameBase: string;
fLogItemAggregationType: TDMSQueueAggregationType;
strict protected
procedure LoadExtendedInfo;
function GetExtendedInfo: TJSONObject;
protected const
DEFAULT_EXTENDED_INFO = [TLogExtendedInfo.EIUserName, TLogExtendedInfo.EIComputerName,
TLogExtendedInfo.EIProcessName,
TLogExtendedInfo.EIProcessID, TLogExtendedInfo.EIDeviceID];
procedure InternalWriteLog(const aLogItem: TLogItem; const aJSONObject: TJSONObject);
public
procedure WriteLog(const aLogItem: TLogItem); override;
constructor Create(aEventStreamsProxy: TEventStreamsRPCProxy;
aDMSContainerAPIKey: String;
aEventStreamsQueueNameBase: String = 'queues.logs.';
aLogItemAggregationType: TDMSQueueAggregationType = dmsatByTag;
aLogExtendedInfo: TLoggerProExtendedInfo = DEFAULT_EXTENDED_INFO); reintroduce;
destructor Destroy; override;
property OnCreateJSONData: TOnCreateJSONData read fOnCreateJSONData write SetOnCreateJSONData;
property OnNetSendError: TOnNetSendError read fOnNetSendError write SetOnNetSendError;
procedure TearDown; override;
procedure Setup; override;
function CreateData(const SrcLogItem: TLogItem): TJSONObject; virtual;
function GetDefaultLog(const aLogItem: TLogItem): TJSONObject; virtual;
class function GetModuleBaseName: String;
end;
implementation
uses
{$IF Defined(MSWINDOWS) }
Winapi.Windows,
{$ENDIF}
{$IF Defined(Android) }
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.Os,
Androidapi.JNI.Util,
Androidapi.Helpers,
{$ENDIF}
System.NetEncoding,
System.IOUtils,
System.Net.URLClient;
{$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 TLoggerProDMSContainerAppender.Create(
aEventStreamsProxy: TEventStreamsRPCProxy;
aDMSContainerAPIKey: String;
aEventStreamsQueueNameBase: String;
aLogItemAggregationType: TDMSQueueAggregationType;
aLogExtendedInfo: TLoggerProExtendedInfo);
begin
inherited Create;
fEventStreamsProxy := aEventStreamsProxy;
fQueueNameBase := aEventStreamsQueueNameBase;
fLogItemAggregationType := aLogItemAggregationType;
if not fQueueNameBase.EndsWith('.') then
fQueueNameBase := fQueueNameBase + '.';
fExtendedInfo := aLogExtendedInfo;
fDMSContainerAPIKey := aDMSContainerAPIKey;
LoadExtendedInfo;
end;
function TLoggerProDMSContainerAppender.CreateData(const SrcLogItem: TLogItem): TJSONObject;
begin
Result := nil;
try
if Assigned(fOnCreateJSONData) then
begin
fOnCreateJSONData(Self, SrcLogItem, fExtendedInfo, Result);
end
else
begin
Result := GetDefaultLog(SrcLogItem);
end;
except
on E: Exception do
begin
FreeAndNil(Result);
raise;
end;
end;
end;
destructor TLoggerProDMSContainerAppender.Destroy;
begin
fEventStreamsProxy.Free;
inherited;
end;
function TLoggerProDMSContainerAppender.GetDefaultLog(const aLogItem: TLogItem): TJSONObject;
begin
Result := TJSONObject.Create;
try
Result.S['timestamp'] := datetimetostr(aLogItem.TimeStamp, FormatSettings);
Result.L['tid'] := aLogItem.ThreadID;
Result.S['type'] := aLogItem.LogTypeAsString;
Result.S['text'] := aLogItem.LogMessage;
Result.O['info'] := GetExtendedInfo;
// Result.S['tag'] := aLogItem.LogTag;
except
Result.Free;
raise;
end;
end;
function TLoggerProDMSContainerAppender.GetExtendedInfo: TJSONObject;
begin
Result := TJSONObject.Create;
try
{$IF Defined(MSWINDOWS)}
if TLogExtendedInfo.EIUserName in fExtendedInfo then
begin
Result.S['username'] := fExtendedInfoData[TLogExtendedInfo.EIUserName];
end;
if TLogExtendedInfo.EIComputerName in fExtendedInfo then
begin
Result.S['computername'] := fExtendedInfoData[TLogExtendedInfo.EIComputerName];
end;
if TLogExtendedInfo.EIProcessName in fExtendedInfo then
begin
Result.S['processname'] := fExtendedInfoData[TLogExtendedInfo.EIProcessName];
end;
if TLogExtendedInfo.EIProcessID in fExtendedInfo then
begin
Result.S['pid'] := fExtendedInfoData[TLogExtendedInfo.EIProcessID];
end;
{$ENDIF}
{$IF Defined(Android)}
if TLogExtendedInfo.EIProcessName in fExtendedInfo then
begin
Result.S['processname'] := fExtendedInfoData[TLogExtendedInfo.EIProcessName];
end;
{$ENDIF}
except
Result.Free;
raise;
end;
end;
class function TLoggerProDMSContainerAppender.GetModuleBaseName: String;
begin
{$IF Defined(MSWINDOWS)}
Result := TPath.ChangeExtension(TPath.GetFileName(GetModuleName(HInstance)), '');
{$ENDIF}
{$IF Defined(Android)}
Result := TAndroidHelper.ApplicationTitle;
{$ENDIF}
if Result.IsEmpty then
begin
raise ELoggerPro.Create('Current platform not supported by ' + ClassName);
end;
end;
procedure TLoggerProDMSContainerAppender.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 TLoggerProDMSContainerAppender.SetOnCreateJSONData(const Value: TOnCreateJSONData);
begin
fOnCreateJSONData := Value;
end;
procedure TLoggerProDMSContainerAppender.SetOnNetSendError(const Value: TOnNetSendError);
begin
fOnNetSendError := Value;
end;
procedure TLoggerProDMSContainerAppender.Setup;
begin
inherited;
end;
procedure TLoggerProDMSContainerAppender.TearDown;
begin
inherited;
end;
procedure TLoggerProDMSContainerAppender.InternalWriteLog(const aLogItem: TLogItem;
const aJSONObject: TJSONObject);
var
lRetryCount: Integer;
lJSONResp: TJSONObject;
const
MAX_RETRY_COUNT = 5;
begin
lRetryCount := 0;
repeat
try
lJSONResp := fEventStreamsProxy.EnqueueMessage(fDMSContainerAPIKey, fQueueNameBase + aLogItem.LogTag,
aJSONObject);
try
finally
lJSONResp.Free;
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;
// finally
// FreeAndNil(lHTTPCli);
// end;
end;
procedure TLoggerProDMSContainerAppender.WriteLog(const aLogItem: TLogItem);
var
lRetryCount: Integer;
lJSONResp: TJSONObject;
lQueueName: string;
const
MAX_RETRY_COUNT = 5;
begin
lRetryCount := 0;
repeat
try
case fLogItemAggregationType of
dmsatByTag:
lQueueName := fQueueNameBase + aLogItem.LogTag;
dmsatByType:
lQueueName := fQueueNameBase + aLogItem.LogTypeAsString;
dmsatByTagThenType:
lQueueName := fQueueNameBase + aLogItem.LogTag + '.' + aLogItem.LogTypeAsString;
dmsatByTypeThenTag:
lQueueName := fQueueNameBase + aLogItem.LogTypeAsString + '.' + aLogItem.LogTag;
else
raise ELoggerPro.Create('Invalid Aggregation type');
end;
lJSONResp := fEventStreamsProxy.EnqueueMessage(fDMSContainerAPIKey,
lQueueName,
CreateData(aLogItem));
try
finally
lJSONResp.Free;
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;
end.