-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathLoggerPro.TimeRotatingFileAppender.pas
More file actions
290 lines (252 loc) · 8.23 KB
/
LoggerPro.TimeRotatingFileAppender.pas
File metadata and controls
290 lines (252 loc) · 8.23 KB
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
// *************************************************************************** }
//
// LoggerPro
//
// Copyright (c) 2010-2026 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.TimeRotatingFileAppender;
interface
uses
LoggerPro,
System.Types,
System.SysUtils,
System.Classes,
System.IOUtils,
System.DateUtils,
System.Generics.Collections;
type
{ @abstract(File appender that rotates based on time intervals instead of file size)
Creates a new log file when the time interval changes.
File naming includes timestamp to allow easy identification and sorting.
}
TLoggerProTimeRotatingFileAppender = class(TLoggerProAppenderBase)
private
FLogsFolder: string;
FFileBaseName: string;
FInterval: TTimeRotationInterval;
FMaxBackupFiles: Integer;
FCurrentWriter: TStreamWriter;
FCurrentTimestamp: string;
FEncoding: TEncoding;
function GetTimestampForInterval(aDateTime: TDateTime): string;
function GetLogFileName(const aTimestamp: string): string;
procedure RotateIfNeeded(aCurrentDateTime: TDateTime);
procedure CreateNewWriter(const aTimestamp: string);
procedure CleanupOldFiles;
procedure WriteToStream(const aValue: string);
public
const DEFAULT_MAX_BACKUP_FILES = 30;
{ Creates a time-rotating file appender.
@param aInterval The rotation interval (Hourly, Daily, Weekly, Monthly)
@param aMaxBackupFiles Maximum number of old log files to keep (0 = unlimited)
@param aLogsFolder Folder for log files (empty = application folder)
@param aFileBaseName Base name for log files (empty = module name)
@param aLogItemRenderer Optional custom renderer
@param aEncoding File encoding (default UTF-8) }
constructor Create(
aInterval: TTimeRotationInterval = TTimeRotationInterval.Daily;
aMaxBackupFiles: Integer = DEFAULT_MAX_BACKUP_FILES;
aLogsFolder: string = '';
aFileBaseName: string = '';
aLogItemRenderer: ILogItemRenderer = nil;
aEncoding: TEncoding = nil); reintroduce;
procedure Setup; override;
procedure TearDown; override;
procedure WriteLog(const aLogItem: TLogItem); override;
property Interval: TTimeRotationInterval read FInterval;
property MaxBackupFiles: Integer read FMaxBackupFiles;
property LogsFolder: string read FLogsFolder;
end;
implementation
{$IF Defined(Android)}
uses
Androidapi.Helpers,
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.JavaTypes;
{$ENDIF}
{ TLoggerProTimeRotatingFileAppender }
constructor TLoggerProTimeRotatingFileAppender.Create(
aInterval: TTimeRotationInterval;
aMaxBackupFiles: Integer;
aLogsFolder: string;
aFileBaseName: string;
aLogItemRenderer: ILogItemRenderer;
aEncoding: TEncoding);
begin
inherited Create(aLogItemRenderer);
if aMaxBackupFiles < 0 then
raise ELoggerPro.CreateFmt('MaxBackupFiles must be >= 0, got %d', [aMaxBackupFiles]);
FInterval := aInterval;
FMaxBackupFiles := aMaxBackupFiles;
FLogsFolder := aLogsFolder;
FFileBaseName := aFileBaseName;
if Assigned(aEncoding) then
FEncoding := aEncoding
else
FEncoding := TEncoding.UTF8;
end;
procedure TLoggerProTimeRotatingFileAppender.Setup;
var
lModuleName: string;
begin
inherited;
// Set default logs folder
if FLogsFolder.IsEmpty then
begin
{$IF Defined(MSWINDOWS) or Defined(POSIX)}
{$IF not Defined(MOBILE)}
FLogsFolder := TPath.GetDirectoryName(GetModuleName(HInstance));
{$ENDIF}
{$ENDIF}
{$IF Defined(Android) or Defined(IOS)}
FLogsFolder := TPath.GetSharedDocumentsPath;
{$ENDIF}
end;
if not TDirectory.Exists(FLogsFolder) then
TDirectory.CreateDirectory(FLogsFolder);
// Set default file base name
if FFileBaseName.IsEmpty then
begin
{$IF Defined(Android)}
lModuleName := TAndroidHelper.ApplicationTitle.Replace(' ', '_', [rfReplaceAll]);
{$ELSEIF not Defined(MOBILE)}
lModuleName := TPath.GetFileNameWithoutExtension(GetModuleName(HInstance));
{$ELSE}
lModuleName := 'app';
{$ENDIF}
FFileBaseName := lModuleName;
end;
// Create initial writer
FCurrentTimestamp := GetTimestampForInterval(Now);
CreateNewWriter(FCurrentTimestamp);
// Cleanup old files on startup
CleanupOldFiles;
end;
procedure TLoggerProTimeRotatingFileAppender.TearDown;
begin
if Assigned(FCurrentWriter) then
begin
FCurrentWriter.Flush;
FCurrentWriter.Free;
FCurrentWriter := nil;
end;
inherited;
end;
function TLoggerProTimeRotatingFileAppender.GetTimestampForInterval(aDateTime: TDateTime): string;
var
lYear, lMonth, lDay, lHour, lWeek: Word;
begin
DecodeDate(aDateTime, lYear, lMonth, lDay);
lHour := HourOf(aDateTime);
case FInterval of
TTimeRotationInterval.Hourly:
Result := Format('%.4d%.2d%.2d%.2d', [lYear, lMonth, lDay, lHour]);
TTimeRotationInterval.Daily:
Result := Format('%.4d%.2d%.2d', [lYear, lMonth, lDay]);
TTimeRotationInterval.Weekly:
begin
lWeek := WeekOfTheYear(aDateTime);
Result := Format('%.4dW%.2d', [lYear, lWeek]);
end;
TTimeRotationInterval.Monthly:
Result := Format('%.4d%.2d', [lYear, lMonth]);
else
Result := Format('%.4d%.2d%.2d', [lYear, lMonth, lDay]);
end;
end;
function TLoggerProTimeRotatingFileAppender.GetLogFileName(const aTimestamp: string): string;
begin
Result := TPath.Combine(FLogsFolder, FFileBaseName + '.' + aTimestamp + '.log');
end;
procedure TLoggerProTimeRotatingFileAppender.CreateNewWriter(const aTimestamp: string);
var
lFileName: string;
lFileStream: TFileStream;
lFileAccessMode: Word;
begin
lFileName := GetLogFileName(aTimestamp);
lFileAccessMode := fmOpenWrite or fmShareDenyNone;
if not TFile.Exists(lFileName) then
lFileAccessMode := lFileAccessMode or fmCreate;
lFileStream := TFileStream.Create(lFileName, lFileAccessMode);
try
lFileStream.Seek(0, TSeekOrigin.soEnd);
FCurrentWriter := TStreamWriter.Create(lFileStream, FEncoding, 32);
FCurrentWriter.AutoFlush := True;
FCurrentWriter.OwnStream;
except
lFileStream.Free;
raise;
end;
end;
procedure TLoggerProTimeRotatingFileAppender.RotateIfNeeded(aCurrentDateTime: TDateTime);
var
lNewTimestamp: string;
begin
lNewTimestamp := GetTimestampForInterval(aCurrentDateTime);
if lNewTimestamp <> FCurrentTimestamp then
begin
// Close current writer
if Assigned(FCurrentWriter) then
begin
FCurrentWriter.Flush;
FCurrentWriter.Free;
FCurrentWriter := nil;
end;
// Update timestamp and create new writer
FCurrentTimestamp := lNewTimestamp;
CreateNewWriter(FCurrentTimestamp);
// Cleanup old files after rotation
CleanupOldFiles;
end;
end;
procedure TLoggerProTimeRotatingFileAppender.CleanupOldFiles;
var
lFiles: TStringDynArray;
lPattern: string;
I: Integer;
begin
if FMaxBackupFiles <= 0 then
Exit; // No cleanup needed
lPattern := FFileBaseName + '.*.log';
lFiles := TDirectory.GetFiles(FLogsFolder, lPattern);
// Sort by name (timestamps ensure chronological order)
TArray.Sort<string>(lFiles);
// Delete oldest files if we exceed the limit
for I := 0 to Length(lFiles) - FMaxBackupFiles - 1 do
begin
try
TFile.Delete(lFiles[I]);
except
// Ignore deletion errors (file might be locked)
end;
end;
end;
procedure TLoggerProTimeRotatingFileAppender.WriteToStream(const aValue: string);
begin
FCurrentWriter.WriteLine(aValue);
FCurrentWriter.Flush;
end;
procedure TLoggerProTimeRotatingFileAppender.WriteLog(const aLogItem: TLogItem);
begin
RotateIfNeeded(aLogItem.TimeStamp);
WriteToStream(FormatLog(aLogItem));
end;
end.