forked from danieleteti/loggerpro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoggerPro.RedisAppender.pas
83 lines (72 loc) · 2.14 KB
/
LoggerPro.RedisAppender.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
unit LoggerPro.RedisAppender;
{ <@abstract(The unit to include if you want to use @link(TLoggerProRedisAppender))
@author(Daniele Teti) }
interface
uses
LoggerPro, System.Classes, System.DateUtils,
Redis.Commons {Redis.Commons is included in DelphiRedisClient, also available with GETIT} ,
Redis.NetLib.INDY {Redis.NetLib.INDY is included in DelphiRedisClient, also available with GETIT};
type
{
@abstract(Logs to a Redis instance)
To learn how to use this appender, check the sample @code(remote_redis_appender.dproj)
@author(Daniele Teti - [email protected])
}
TLoggerProRedisAppender = class(TLoggerProAppenderBase)
private
FRedis: IRedisClient;
FLogKeyPrefix: string;
FMaxSize: Int64;
public
constructor Create(aRedis: IRedisClient; aMaxSize: Int64 = 5000; aKeyPrefix: string = 'loggerpro'; aLogFormat: string = DEFAULT_LOG_FORMAT); reintroduce;
procedure Setup; override;
procedure TearDown; override;
procedure WriteLog(const aLogItem: TLogItem); override;
procedure TryToRestart(var Restarted: Boolean); override;
end;
implementation
uses
System.SysUtils;
constructor TLoggerProRedisAppender.Create(aRedis: IRedisClient; aMaxSize: Int64; aKeyPrefix: string; aLogFormat: string);
begin
inherited Create(aLogFormat);
FRedis := aRedis;
FLogKeyPrefix := aKeyPrefix;
FMaxSize := aMaxSize;
end;
procedure TLoggerProRedisAppender.Setup;
begin
inherited;
FRedis.Connect;
end;
procedure TLoggerProRedisAppender.TearDown;
begin
// do nothing
end;
procedure TLoggerProRedisAppender.TryToRestart(var Restarted: Boolean);
begin
inherited;
Restarted := False;
try
FRedis.Disconnect
except
end;
try
FRedis.Connect;
Restarted := True;
except
end;
end;
procedure TLoggerProRedisAppender.WriteLog(const aLogItem: TLogItem);
var
lText: string;
lKey: string;
begin
lText := FormatLog(aLogItem);
lKey := FLogKeyPrefix + '::logs'; // + aLogItem.LogTypeAsString.ToLower;
// Push the log item to the right of the list (logs:info, logs:warning, log:error)
FRedis.RPUSH(lKey, [lText]);
// Trim the list to the FMaxSize last elements
FRedis.LTRIM(lKey, -FMaxSize, -1);
end;
end.