forked from danieleteti/loggerpro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoggerPro.UDPSyslogAppender.pas
173 lines (156 loc) · 5.27 KB
/
LoggerPro.UDPSyslogAppender.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
unit LoggerPro.UDPSyslogAppender;
{ <@abstract(Contains the Syslog Logger (RFC 5424))
@author(https://github.com/nurettin)
@author(Daniele Teti)
}
interface
uses
LoggerPro,
IdBaseComponent,
IdComponent,
IdUDPBase,
IdUDPClient,
IdGlobal;
type
TLoggerProUDPSyslogAppender = class(TLoggerProAppenderBase)
private
FLoggerProSyslogAppenderClient: TIdUDPClient;
FIP: string;
FPort: Integer;
FHostName: string;
FUserName: string;
FApplication: string;
FVersion: string;
FProcID: string;
FUnixLineBreaks: Boolean;
FUTF8BOM: Boolean;
public
constructor Create(pIP: string; pPort: Integer; pHostName: string; pUserName: string; pApplication: string;
pVersion: string; pProcID: string; pUnixLineBreaks: Boolean; pUTF8BOM: Boolean = False); reintroduce;
procedure Setup; override;
procedure TearDown; override;
procedure WriteLog(const aLogItem: TLogItem); override;
property IP: string read FIP write FIP;
property Port: Integer read FPort write FPort;
property HostName: string read FHostName write FHostName;
property UserName: string read FUserName write FUserName;
property Application: string read FApplication write FApplication;
property Version: string read FVersion write FVersion;
property ProcID: string read FProcID write FProcID;
property UnixLineBreaks: Boolean read FUnixLineBreaks write FUnixLineBreaks;
end;
TLoggerProUDPSyslogPacket = class
strict private
FPriority: string;
FVersion: string;
FTimestamp: string;
FHostName: string;
FUserName: string;
FApplication: string;
FProcID: string;
FThreadID: string;
FMessageID: string;
FMessageData: string;
FUnixLineBreaks: Boolean;
FUTF8BOM: Boolean;
function GetSyslogData: string;
public
constructor Create(pLogItem: TLogItem; pHostName: string; pUserName: string; pApplication: string; pVersion: string;
pProcID: string; pUnixLineBreaks: Boolean; pUTF8BOM: Boolean = False);
property SyslogData: string read GetSyslogData;
end;
implementation
uses
System.DateUtils,
System.SysUtils;
{ TLoggerProUDPSyslogAppender }
constructor TLoggerProUDPSyslogAppender.Create(pIP: string; pPort: Integer; pHostName: string; pUserName: string;
pApplication: string; pVersion: string; pProcID: string; pUnixLineBreaks: Boolean; pUTF8BOM: Boolean);
begin
inherited Create;
FIP := pIP;
FPort := pPort;
FHostName := pHostName;
FUserName := pUserName;
FApplication := pApplication;
FVersion := pVersion;
FProcID := pProcID;
FUnixLineBreaks := pUnixLineBreaks;
FUTF8BOM := pUTF8BOM;
end;
procedure TLoggerProUDPSyslogAppender.Setup;
begin
inherited;
FLoggerProSyslogAppenderClient := TIdUDPClient.Create(nil);
end;
procedure TLoggerProUDPSyslogAppender.TearDown;
begin
FLoggerProSyslogAppenderClient.Free;
inherited;
end;
procedure TLoggerProUDPSyslogAppender.WriteLog(const aLogItem: TLogItem);
var
lPacket: TLoggerProUDPSyslogPacket;
begin
inherited;
lPacket := TLoggerProUDPSyslogPacket.Create(aLogItem, FHostName, FUserName, FApplication, FVersion, FProcID,
FUnixLineBreaks, FUTF8BOM);
try
FLoggerProSyslogAppenderClient.Broadcast(lPacket.SyslogData, FPort, FIP, IndyTextEncoding_UTF8);
finally
lPacket.Free;
end;
end;
{ TLoggerProUDPSyslogPacket }
function RFC5424Priority(pFacility, pSeverity: Integer): string;
begin
Result := '<' + IntToStr(pFacility * 8 + pSeverity) + '>';
end;
constructor TLoggerProUDPSyslogPacket.Create(pLogItem: TLogItem; pHostName: string; pUserName: string;
pApplication: string; pVersion: string; pProcID: string; pUnixLineBreaks: Boolean; pUTF8BOM: Boolean);
begin
// ## start https://github.com/danieleteti/loggerpro/issues/56
case pLogItem.LogType of
TLogType.Debug:
FPriority := RFC5424Priority(1, 7);
TLogType.Info:
FPriority := RFC5424Priority(1, 6);
TLogType.Warning:
FPriority := RFC5424Priority(1, 4); // 4 = slWarning
TLogType.Error:
FPriority := RFC5424Priority(1, 3); // 3 = slError
end;
if pLogItem.LogMessage.Contains('Access Violation') then
FPriority := RFC5424Priority(1, 2); // 2 = slCritical
// ## end
FApplication := pApplication;
FVersion := pVersion;
FTimestamp := DateToISO8601(pLogItem.Timestamp);
FHostName := pHostName;
FUserName := pUserName;
FApplication := pApplication;
FVersion := pVersion;
FProcID := pProcID;
FThreadID := IntToStr(pLogItem.ThreadID);
FMessageID := pLogItem.LogTag;
FUnixLineBreaks := pUnixLineBreaks;
if FUnixLineBreaks then
FMessageData := pLogItem.LogMessage.Replace(sLineBreak, '#10', [rfReplaceAll])
else
FMessageData := pLogItem.LogMessage;
FUTF8BOM := pUTF8BOM;
end;
function TLoggerProUDPSyslogPacket.GetSyslogData: string;
const
IANAVersion = '1';
begin
Result :=
// NOT; RFC 5424 6.2 HEADER
FPriority + IANAVersion + ' ' + FTimestamp + ' ' + FHostName + ' ' + FApplication + ' ' + FProcID + ' ' + FMessageID
// NOT; RFC 5424, 6.5 ex 1 no structured data
+ ' - ' + iif(FUTF8BOM, #$EF#$BB#$BF) + FUserName + ' ' + FVersion + ' '
+ FThreadID + ' ' + FMessageData;
// NOT; RFC 5424 structured data, uncomment and use if needed
// + ' [MySDID@1 ' + 'UserName="' + FUserName + '" Version="' + FVersion + '" ThreadId="' + FThreadID + '" MessageData="' + FMessageData + '"]' + ...;
end;
end.