Repository navigation
Expand file tree
/
Copy pathLoggerPro.UDPSyslogAppender.pas
More file actions
210 lines (190 loc) · 6.59 KB
/
Copy pathLoggerPro.UDPSyslogAppender.pas
File metadata and controls
210 lines (190 loc) · 6.59 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
// *************************************************************************** }
//
// LoggerPro
//
// Copyright (c) 2010-2026 Daniele Teti
//
// https://github.com/danieleteti/loggerpro
//
// Contributors for this file:
// https://github.com/nurettin
//
// ***************************************************************************
//
// 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.UDPSyslogAppender;
// Contains the Syslog Logger (RFC 5424)
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;
FUseLocalTime: Boolean;
public
constructor Create(pIP: string; pPort: Integer; pHostName: string; pUserName: string; pApplication: string;
pVersion: string; pProcID: string; pUnixLineBreaks: Boolean; pUTF8BOM: Boolean = False; pUseLocalTime: 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;
property UseLocalTime: Boolean read FUseLocalTime write FUseLocalTime;
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;
FUseLocalTime: Boolean;
function GetSyslogData: string;
public
constructor Create(pLogItem: TLogItem; pHostName: string; pUserName: string; pApplication: string; pVersion: string;
pProcID: string; pUnixLineBreaks: Boolean; pUTF8BOM: Boolean = False; pUseLocalTime: 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; pUseLocalTime: Boolean);
begin
inherited Create;
FIP := pIP;
FPort := pPort;
FHostName := pHostName;
FUserName := pUserName;
FApplication := pApplication;
FVersion := pVersion;
FProcID := pProcID;
FUnixLineBreaks := pUnixLineBreaks;
FUTF8BOM := pUTF8BOM;
FUseLocalTime := pUseLocalTime;
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, FUseLocalTime);
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; pUseLocalTime: Boolean);
var
lTimestamp: TDateTime;
begin
// RFC 5424 Severity mapping
// 7 = Debug, 6 = Informational, 4 = Warning, 3 = Error, 2 = Critical
case pLogItem.LogType of
TLogType.Debug:
FPriority := RFC5424Priority(1, 7);
TLogType.Info:
FPriority := RFC5424Priority(1, 6);
TLogType.Warning:
FPriority := RFC5424Priority(1, 4);
TLogType.Error:
FPriority := RFC5424Priority(1, 3);
TLogType.Fatal:
FPriority := RFC5424Priority(1, 2);
end;
FApplication := pApplication;
FVersion := pVersion;
// Convert UTC to local time if requested
lTimestamp := pLogItem.Timestamp;
if pUseLocalTime then
lTimestamp := TTimeZone.Local.ToLocalTime(lTimestamp);
FTimestamp := DateToISO8601(lTimestamp);
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;
FUseLocalTime := pUseLocalTime;
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.