-
Notifications
You must be signed in to change notification settings - Fork 91
/
LoggerPro.FileAppender.pas
707 lines (629 loc) · 21 KB
/
LoggerPro.FileAppender.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
// *************************************************************************** }
//
// 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.FileAppender;
{$IF Defined(Android) or Defined(iOS)}
{$DEFINE MOBILE}
{$ENDIF}
interface
uses
LoggerPro,
System.Generics.Collections,
System.Classes,
System.SysUtils;
type
{
@abstract(Logs to file using one different file for each different TAG used.)
@author(Daniele Teti - [email protected])
Implements log rotations.
This appender is the default appender when no configuration is done on the @link(TLogger) class.
Without any configuration LoggerPro uses the @link(TLoggerProFileAppender) with the default configuration.
So the following two blocks of code are equivalent:
@longcode(#
...
TLogger.Initialize; //=> uses the TLoggerProFileAppender because no other configuration is provided
...
...
TLogger.AddAppender(TLoggerProFileAppender.Create);
TLogger.Initialize //=> uses the TLoggerProFileAppender as configured
...
#)
}
{ @abstract(The base class for different file appenders)
Do not use this class directly, but one of TLoggerProFileAppender or TLoggerProSimpleFileAppender.
Check the sample @code(file_appender.dproj)
}
TLoggerProFileAppenderBase = class(TLoggerProAppenderBase)
private
procedure RetryMove(const aFileSrc, aFileDest: string);
procedure RetryDelete(const aFileSrc: string);
protected
fEncoding: TEncoding;
fMaxBackupFileCount: Integer;
fMaxFileSizeInKiloByte: Integer;
fLogFileNameFormat: string;
fLogsFolder: string;
function CreateWriter(const aFileName: string; const aBufferSize: Integer = 32): TStreamWriter;
procedure CheckLogFileNameFormat(const LogFileNameFormat: String); virtual;
procedure EmitStartRotateLogItem(aWriter: TStreamWriter); virtual;
procedure EmitEndRotateLogItem(aWriter: TStreamWriter); virtual;
function GetLogFileName(const aTag: string; const aFileNumber: Integer): string; virtual;
procedure WriteToStream(const aStreamWriter: TStreamWriter; const aValue: string); inline;
procedure RotateFile(const aLogTag: string; out aNewFileName: string); virtual;
procedure InternalWriteLog(const aStreamWriter: TStreamWriter; const aLogItem: TLogItem);
public const
{ @abstract(Defines the default format string used by the @link(TLoggerProFileAppender).)
The positional parameters are the following:
@orderedList(
@item Number
@item Module
@item Tag
)
}
DEFAULT_FILENAME_FORMAT = '{module}.{number}.{tag}.log';
DEFAULT_FILENAME_FORMAT_WITH_PID = '{module}.{number}.{pid}.{tag}.log';
DEFAULT_FILENAME_FORMAT_WITHOUT_TAG = '{module}.{number}.log';
{ @abstract(Defines number of log file set to maintain during logs rotation) }
DEFAULT_MAX_BACKUP_FILE_COUNT = 5;
{ @abstract(Defines the max size of each log file)
The actual meaning is: "If the file size is > than @link(DEFAULT_MAX_FILE_SIZE_KB) then rotate logs. }
DEFAULT_MAX_FILE_SIZE_KB = 1000;
{ @abstract(Milliseconds to wait between the RETRY_COUNT times. }
RETRY_DELAY = 200;
{ @abstract(How many times do we have to retry if the file is locked?. }
RETRY_COUNT = 5;
constructor Create(
aMaxBackupFileCount: Integer = TLoggerProFileAppenderBase.DEFAULT_MAX_BACKUP_FILE_COUNT;
aMaxFileSizeInKiloByte: Integer = TLoggerProFileAppenderBase.DEFAULT_MAX_FILE_SIZE_KB;
aLogsFolder: string = '';
aLogFileNameFormat: string = TLoggerProFileAppenderBase.DEFAULT_FILENAME_FORMAT;
aLogItemRenderer: ILogItemRenderer = nil;
aEncoding: TEncoding = nil);
reintroduce; virtual;
procedure Setup; override;
end;
{ @abstract(The default file appender)
This file appender separates TLogItems with different tags into a log file for each tag.
To learn how to use this appender, check the sample @code(file_appender.dproj)
}
TLoggerProFileAppender = class(TLoggerProFileAppenderBase)
private
fWritersDictionary: TObjectDictionary<string, TStreamWriter>;
procedure AddWriter(const aLogTag: string; var aWriter: TStreamWriter; var aLogFileName: string);
procedure RotateLog(const aLogTag: string; aWriter: TStreamWriter);
public
procedure Setup; override;
procedure TearDown; override;
procedure WriteLog(const aLogItem: TLogItem); overload; override;
end;
{ @abstract(File appender with multiple tags)
This file appender writes all TLogItems into a single log file.
Combined with a @code(TLoggerProAppenderFilterImpl) you can filter out any log tags you like.
If you want to run several TLoggerProSimpleFileAppender in parallel you have to provide a different
LogFileFormat for each of them in the constructor in order to prevent name collisions.
To learn how to use this appender, check the sample @code(file_appender.dproj)
}
TLoggerProSimpleFileAppender = class(TLoggerProFileAppenderBase)
private
fFileWriter: TStreamWriter;
procedure RotateLog;
protected
procedure CheckLogFileNameFormat(const LogFileNameFormat: String); override;
public
const
DEFAULT_FILENAME_FORMAT = '{module}.{number}.log';
procedure Setup; override;
procedure TearDown; override;
procedure WriteLog(const aLogItem: TLogItem); overload; override;
constructor Create(
aMaxBackupFileCount: Integer = TLoggerProFileAppenderBase.DEFAULT_MAX_BACKUP_FILE_COUNT;
aMaxFileSizeInKiloByte: Integer = TLoggerProFileAppenderBase.DEFAULT_MAX_FILE_SIZE_KB;
aLogsFolder: string = '';
aLogFileNameFormat: string = TLoggerProSimpleFileAppender.DEFAULT_FILENAME_FORMAT;
aLogItemRenderer: ILogItemRenderer = nil;
aEncoding: TEncoding = nil);
override;
end;
TMakeFileNameProc = reference to procedure(out AFileName: string);
{ by an idea of Mark Lobanov <[email protected]> }
TLoggerProFileByFolderAppender = class(TLoggerProFileAppender)
private
fFileWriter: TStreamWriter;
fCurrentDate: TDateTime;
function GetLogFolder: string;
function GetFileFormat: string;
procedure RotateLog;
procedure ChangeLogFolder;
procedure RefreshCurrentDate;
procedure InternalRotateLog(aMakeFileNameProc: TMakeFileNameProc);
protected
function GetLogFileName(const aTag: string; const aFileNumber: Integer): string; override;
procedure CheckLogFileNameFormat(const aLogFileNameFormat: string); override;
public
constructor Create(
aMaxBackupFileCount: Integer = TLoggerProFileAppenderBase.DEFAULT_MAX_BACKUP_FILE_COUNT;
aMaxFileSizeInKiloByte: Integer = TLoggerProFileAppenderBase.DEFAULT_MAX_FILE_SIZE_KB;
const aLogsFolder: string = '';
const aLogItemRenderer: ILogItemRenderer = nil;
const aEncoding: TEncoding = nil); reintroduce;
procedure WriteLog(const ALogItem: TLogItem); override;
procedure Setup; override;
procedure TearDown; override;
end;
implementation
uses
System.IOUtils,
System.StrUtils,
System.Math,
System.DateUtils,
idGlobal
{$IF Defined(Android), System.SysUtils}
,Androidapi.Helpers
,Androidapi.JNI.GraphicsContentViewText
,Androidapi.JNI.JavaTypes
{$ENDIF}
;
function OccurrencesOfChar(const S: string; const C: char): integer;
var
i: Integer;
begin
result := 0;
for i := 1 to Length(S) do
if S[i] = C then
inc(result);
end;
procedure TLoggerProFileAppenderBase.CheckLogFileNameFormat(const LogFileNameFormat: String);
begin
//DEFAULT_FILENAME_FORMAT = '{module}.{number}.{tag}.log';
if not (LogFileNameFormat.Contains('{number}') and LogFileNameFormat.Contains('{tag}')) then
begin
raise ELoggerPro.CreateFmt('Wrong FileFormat [%s] - [HINT] A correct file format for %s requires {number} and {tag} placeholders ({module} is optional). A valid file format is : %s',
[
ClassName,
LogFileNameFormat,
TLoggerProFileAppenderBase.DEFAULT_FILENAME_FORMAT
]);
end;
end;
{ TLoggerProFileAppenderBase }
function TLoggerProFileAppenderBase.GetLogFileName(const aTag: string; const aFileNumber: Integer): string;
var
// lExt: string;
lModuleName: string;
lPath: string;
lFormat: string;
begin
{$IF Defined(Android)}
lModuleName := TAndroidHelper.ApplicationTitle.Replace(' ', '_', [rfReplaceAll]);
{$ENDIF}
{$IF not Defined(Mobile)}
lModuleName := TPath.GetFileNameWithoutExtension(GetModuleName(HInstance));
{$ENDIF}
{$IF Defined(IOS)}
raise Exception.Create('Platform not supported');
{$ENDIF}
lFormat := fLogFileNameFormat;
lPath := fLogsFolder;
lFormat := lFormat
.Replace('{module}', lModuleName, [rfReplaceAll])
.Replace('{number}', aFileNumber.ToString.PadLeft(
Max(2,fMaxBackupFileCount.ToString.Length), //min padding 2
'0') , [rfReplaceAll])
.Replace('{tag}', aTag, [rfReplaceAll])
.Replace('{pid}', CurrentProcessId.ToString.PadLeft(8,'0'), [rfReplaceAll]);
Result := TPath.Combine(lPath, lFormat);
end;
procedure TLoggerProFileAppenderBase.Setup;
begin
inherited;
if fLogsFolder = '' then
begin
{$IF (Defined(MSWINDOWS) or Defined(POSIX)) and (not Defined(MOBILE))}
fLogsFolder := TPath.GetDirectoryName(GetModuleName(HInstance));
{$ENDIF}
{$IF Defined(Android) or Defined(IOS)}
fLogsFolder := TPath.GetSharedDocumentsPath();
{$ENDIF}
end;
if not TDirectory.Exists(fLogsFolder) then
TDirectory.CreateDirectory(fLogsFolder);
end;
procedure TLoggerProFileAppenderBase.WriteToStream(const aStreamWriter: TStreamWriter; const aValue: string);
begin
aStreamWriter.WriteLine(aValue);
aStreamWriter.Flush;
end;
procedure TLoggerProFileAppenderBase.InternalWriteLog(const aStreamWriter: TStreamWriter; const aLogItem: TLogItem);
begin
WriteToStream(aStreamWriter, FormatLog(aLogItem));
end;
procedure TLoggerProFileAppenderBase.RetryDelete(const aFileSrc: string);
var
lRetries: Integer;
const
MAX_RETRIES = 5;
begin
lRetries := 0;
repeat
try
Sleep(50);
// the incidence of "Locked file goes to nearly zero..."
TFile.Delete(aFileSrc);
if not TFile.Exists(aFileSrc) then
begin
Break;
end;
except
on E: Exception do
begin
Inc(lRetries);
Sleep(100);
end;
end;
until lRetries = MAX_RETRIES;
if lRetries = MAX_RETRIES then
raise ELoggerPro.CreateFmt('Cannot delete file %s', [aFileSrc]);
end;
procedure TLoggerProFileAppenderBase.RetryMove(const aFileSrc, aFileDest: string);
var
lRetries: Integer;
const
MAX_RETRIES = 5;
begin
lRetries := 0;
repeat
try
Sleep(50);
// the incidence of "Locked file goes to nearly zero..."
TFile.Move(aFileSrc, aFileDest);
Break;
except
on E: EInOutError do
begin
Inc(lRetries);
Sleep(100);
end;
on E: Exception do
begin
raise;
end;
end;
until lRetries = MAX_RETRIES;
if lRetries = MAX_RETRIES then
raise ELoggerPro.CreateFmt('Cannot rename %s to %s', [aFileSrc, aFileDest]);
end;
procedure TLoggerProFileAppenderBase.RotateFile(const aLogTag: string; out aNewFileName: string);
var
lRenamedFile: string;
I: Integer;
lCurrentFileName: string;
begin
aNewFileName := GetLogFileName(aLogTag, 0);
// remove the last file of backup set
lRenamedFile := GetLogFileName(aLogTag, fMaxBackupFileCount - 1);
if TFile.Exists(lRenamedFile) then
begin
TFile.Delete(lRenamedFile);
if TFile.Exists(lRenamedFile) then // double check for slow file systems
begin
RetryDelete(lRenamedFile);
end;
end;
// shift the files names
for I := fMaxBackupFileCount - 1 downto 1 do
begin
lCurrentFileName := GetLogFileName(aLogTag, I);
lRenamedFile := GetLogFileName(aLogTag, I + 1);
if TFile.Exists(lCurrentFileName) then
begin
RetryMove(lCurrentFileName, lRenamedFile);
end;
end;
lRenamedFile := GetLogFileName(aLogTag, 1);
RetryMove(aNewFileName, lRenamedFile);
end;
constructor TLoggerProFileAppenderBase.Create(
aMaxBackupFileCount: Integer;
aMaxFileSizeInKiloByte: Integer;
aLogsFolder: string;
aLogFileNameFormat: string;
aLogItemRenderer: ILogItemRenderer;
aEncoding: TEncoding);
begin
inherited Create(aLogItemRenderer);
fLogsFolder := aLogsFolder;
fMaxBackupFileCount:= Max(1, aMaxBackupFileCount);
fMaxFileSizeInKiloByte := aMaxFileSizeInKiloByte;
CheckLogFileNameFormat(aLogFileNameFormat);
fLogFileNameFormat := aLogFileNameFormat;
if Assigned(aEncoding) then
fEncoding := aEncoding
else
fEncoding := TEncoding.DEFAULT;
end;
function TLoggerProFileAppenderBase.CreateWriter(const aFileName: string; const aBufferSize: Integer = 32): TStreamWriter;
var
lFileStream: TFileStream;
lFileAccessMode: Word;
lRetries: Integer;
begin
lFileAccessMode := fmOpenWrite or fmShareDenyNone;
if not TFile.Exists(aFileName) then
lFileAccessMode := lFileAccessMode or fmCreate;
// If the file si still blocked by a precedent execution or
// for some other reasons, we try to access the file for 5 times.
// If after 5 times (with a bit of delay in between) the file is still
// locked, then the exception is raised.
lRetries := 0;
while true do
begin
try
lFileStream := TFileStream.Create(aFileName, lFileAccessMode);
try
lFileStream.Seek(0, TSeekOrigin.soEnd);
Result := TStreamWriter.Create(lFileStream, fEncoding, aBufferSize);
Result.AutoFlush := true;
Result.OwnStream;
Break;
except
lFileStream.Free;
raise;
end;
except
if lRetries = RETRY_COUNT then
begin
raise;
end
else
begin
Inc(lRetries);
Sleep(RETRY_DELAY); // just wait a little bit
end;
end;
end;
end;
{ TLoggerProFileAppender }
procedure TLoggerProFileAppender.AddWriter(const aLogTag:string; var aWriter: TStreamWriter; var aLogFileName: string);
begin
aLogFileName := GetLogFileName(aLogTag, 0);
aWriter := CreateWriter(aLogFileName);
fWritersDictionary.Add(aLogTag, aWriter);
end;
procedure TLoggerProFileAppenderBase.EmitEndRotateLogItem(aWriter: TStreamWriter);
begin
WriteToStream(aWriter, '#[ROTATE LOG ' + datetimetostr(Now, FormatSettings) + ']');
end;
procedure TLoggerProFileAppenderBase.EmitStartRotateLogItem(aWriter: TStreamWriter);
begin
WriteToStream(aWriter, '#[START LOG ' + datetimetostr(Now, FormatSettings) + ']');
end;
procedure TLoggerProFileAppender.RotateLog(const aLogTag: string; aWriter: TStreamWriter);
var
lLogFileName: string;
begin
EmitEndRotateLogItem(aWriter);
//WriteToStream(aWriter, '#[ROTATE LOG ' + datetimetostr(Now, FormatSettings) + ']');
// remove the writer during rename
fWritersDictionary.Remove(aLogTag);
RotateFile(aLogTag, lLogFileName);
// re-create the writer
AddWriter(aLogTag, aWriter, lLogFileName);
EmitStartRotateLogItem(aWriter);
//WriteToStream(aWriter, '#[START LOG ' + datetimetostr(Now, FormatSettings) + ']');
end;
procedure TLoggerProFileAppender.Setup;
begin
inherited;
fWritersDictionary := TObjectDictionary<string, TStreamWriter>.Create([doOwnsValues]);
end;
procedure TLoggerProFileAppender.TearDown;
begin
fWritersDictionary.Free;
inherited;
end;
procedure TLoggerProFileAppender.WriteLog(const aLogItem: TLogItem);
var
lWriter: TStreamWriter;
lLogFileName:string;
begin
if not fWritersDictionary.TryGetValue(aLogItem.LogTag, lWriter) then
begin
AddWriter(aLogItem.LogTag, lWriter, lLogFileName);
end;
InternalWriteLog(lWriter, aLogItem);
if lWriter.BaseStream.Size > fMaxFileSizeInKiloByte * 1024 then
begin
RotateLog(aLogItem.LogTag, lWriter);
end;
end;
{ TLoggerProSimpleFileAppender }
procedure TLoggerProSimpleFileAppender.CheckLogFileNameFormat(const LogFileNameFormat: String);
begin
//DEFAULT_FILENAME_FORMAT = '{module}.{number}.{tag}.log';
if not LogFileNameFormat.Contains('{number}') then
begin
raise ELoggerPro.CreateFmt('Wrong FileFormat [%s] - [HINT] A correct file format for %s requires {number} placeholder ({module} is optional). A valid file format is : %s',
[
ClassName,
LogFileNameFormat,
TLoggerProSimpleFileAppender.DEFAULT_FILENAME_FORMAT
]);
end;
end;
constructor TLoggerProSimpleFileAppender.Create(aMaxBackupFileCount, aMaxFileSizeInKiloByte: Integer;
aLogsFolder: string; aLogFileNameFormat: String;
aLogItemRenderer: ILogItemRenderer;
aEncoding: TEncoding);
begin
inherited Create(
aMaxBackupFileCount,
aMaxFileSizeInKiloByte,
aLogsFolder,
aLogFileNameFormat,
aLogItemRenderer,
aEncoding);
end;
procedure TLoggerProSimpleFileAppender.RotateLog;
var
lLogFileName: string;
begin
EmitEndRotateLogItem(fFileWriter);
// remove the writer during rename
fFileWriter.Free;
RotateFile('', lLogFileName);
// re-create the writer
fFileWriter := CreateWriter(GetLogFileName('', 0));
EmitStartRotateLogItem(fFileWriter);
end;
procedure TLoggerProSimpleFileAppender.Setup;
begin
inherited;
fFileWriter := CreateWriter(GetLogFileName('', 0));
end;
procedure TLoggerProSimpleFileAppender.TearDown;
begin
fFileWriter.Free;
inherited;
end;
procedure TLoggerProSimpleFileAppender.WriteLog(const aLogItem: TLogItem);
begin
InternalWriteLog(fFileWriter, aLogItem);
if fFileWriter.BaseStream.Size > fMaxFileSizeInKiloByte * 1024 then
begin
RotateLog;
end;
end;
{ TLoggerProFileByFolderAppender }
constructor TLoggerProFileByFolderAppender.Create(
aMaxBackupFileCount, aMaxFileSizeInKiloByte: Integer;
const aLogsFolder: string;
const aLogItemRenderer: ILogItemRenderer;
const aEncoding: TEncoding);
var
lEncoding: TEncoding;
begin
if AEncoding = nil then
LEncoding := TEncoding.UTF8
else
LEncoding := AEncoding;
inherited Create(aMaxBackupFileCount, aMaxFileSizeInKiloByte,
aLogsFolder, GetFileFormat, aLogItemRenderer, LEncoding);
RefreshCurrentDate;
end;
procedure TLoggerProFileByFolderAppender.CheckLogFileNameFormat(const ALogFileNameFormat: string);
begin
//do nothing, user cannot change filename format in this appender
end;
function TLoggerProFileByFolderAppender.GetLogFolder: string;
const
LOG_DIR = 'Logs';
begin
if fLogsFolder.IsEmpty then
fLogsFolder := TPath.Combine(TPath.GetDirectoryName({ParamStr(0)} GetModuleName(HInstance)), LOG_DIR)
else
if not EndsText(LOG_DIR, fLogsFolder) then
fLogsFolder := TPath.Combine(FLogsFolder, LOG_DIR);
if not TDirectory.Exists(fLogsFolder) then
TDirectory.CreateDirectory(fLogsFolder);
Result := TPath.Combine(fLogsFolder, FormatDateTime('yyyymmdd', Now));
if not TDirectory.Exists(Result) then
TDirectory.CreateDirectory(Result);
end;
procedure TLoggerProFileByFolderAppender.InternalRotateLog(aMakeFileNameProc: TMakeFileNameProc);
var
lLogFileName: string;
begin
EmitEndRotateLogItem(fFileWriter);
FreeAndNil(fFileWriter);
aMakeFileNameProc(lLogFileName);
fFileWriter := CreateWriter(lLogFileName, 16 * 1024);
EmitStartRotateLogItem(fFileWriter);
end;
procedure TLoggerProFileByFolderAppender.ChangeLogFolder;
begin
InternalRotateLog(
procedure(out AFileName: string)
begin
AFileName := GetLogFileName(EmptyStr, 0);
end
);
end;
procedure TLoggerProFileByFolderAppender.RotateLog;
begin
InternalRotateLog(
procedure(out AFileName: string)
begin
RotateFile(EmptyStr, AFileName);
end
);
end;
function TLoggerProFileByFolderAppender.GetFileFormat: string;
begin
Result := TLoggerProFileAppenderBase.DEFAULT_FILENAME_FORMAT_WITHOUT_TAG;
end;
function TLoggerProFileByFolderAppender.GetLogFileName(const ATag: string; const AFileNumber: Integer): string;
var
lOnlyFileName: String;
begin
lOnlyFileName := TPath.GetFileName(inherited);
Result := TPath.Combine(GetLogFolder, lOnlyFileName);
end;
procedure TLoggerProFileByFolderAppender.RefreshCurrentDate;
begin
fCurrentDate := Date;
end;
procedure TLoggerProFileByFolderAppender.Setup;
begin
inherited;
fFileWriter := CreateWriter(GetLogFileName(EmptyStr, 0));
RefreshCurrentDate;
end;
procedure TLoggerProFileByFolderAppender.TearDown;
begin
fFileWriter.Free;
inherited;
end;
procedure TLoggerProFileByFolderAppender.WriteLog(const ALogItem: TLogItem);
var
lLogRow: string;
begin
if not SameDate(fCurrentDate, Date) then
begin
ChangeLogFolder;
RefreshCurrentDate;
end;
if Assigned(OnLogRow) then
begin
OnLogRow(ALogItem, lLogRow);
end
else
begin
lLogRow := LogItemRenderer.RenderLogItem(ALogItem);
end;
WriteToStream(fFileWriter, lLogRow);
if fFileWriter.BaseStream.Size > fMaxFileSizeInKiloByte * 1024 then
begin
RotateLog;
end;
end;
end.