-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa44246
commit 847810d
Showing
9 changed files
with
952 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
unit LoggerPro.ElasticSearchAppender; | ||
|
||
interface | ||
|
||
uses | ||
LoggerPro, | ||
System.Net.HttpClientComponent, | ||
System.SysUtils, | ||
LoggerPro.RESTAppender, | ||
System.Net.HttpClient; | ||
|
||
type | ||
|
||
{ | ||
Log appender for an ElasticSearch 6.4+ endpoint | ||
Author: Daniele Teti (https://github.com/danieleteti/) and Salvatore Sparacino | ||
} | ||
|
||
TLoggerProElasticSearchAppender = class(TLoggerProRESTAppender) | ||
private | ||
fHTTP: THTTPClient; | ||
public | ||
constructor Create(aElasticHost: string; aElasticPort: integer; | ||
aElasticIndex: string); reintroduce; | ||
destructor Destroy; override; | ||
procedure Setup; override; | ||
procedure TearDown; override; | ||
procedure TryToRestart(var Restarted: Boolean); override; | ||
procedure WriteLog(const aLogItem: TLogItem); override; | ||
end; | ||
|
||
implementation | ||
|
||
uses | ||
System.Json, | ||
System.DateUtils, | ||
System.Classes; | ||
|
||
{ TLoggerProElasticSearchAppender } | ||
|
||
constructor TLoggerProElasticSearchAppender.Create(aElasticHost: string; | ||
aElasticPort: integer; aElasticIndex: string); | ||
var | ||
lUrl: string; | ||
begin | ||
fHTTP := THTTPClient.Create; | ||
lUrl := Format('%s:%d/%s/_doc', [aElasticHost, aElasticPort, aElasticIndex.ToLower]); | ||
inherited Create(lUrl, 'application/json'); | ||
end; | ||
|
||
destructor TLoggerProElasticSearchAppender.Destroy; | ||
begin | ||
fHTTP.Free; | ||
inherited; | ||
end; | ||
|
||
procedure TLoggerProElasticSearchAppender.Setup; | ||
begin | ||
inherited; | ||
end; | ||
|
||
procedure TLoggerProElasticSearchAppender.TearDown; | ||
begin | ||
inherited; | ||
end; | ||
|
||
procedure TLoggerProElasticSearchAppender.TryToRestart(var Restarted: Boolean); | ||
begin | ||
inherited; | ||
Restarted := True; | ||
end; | ||
|
||
procedure TLoggerProElasticSearchAppender.WriteLog(const aLogItem: TLogItem); | ||
var | ||
lRequestBody: TStringStream; | ||
lJSON: TJSONObject; | ||
begin | ||
lJSON := TJSONObject.Create; | ||
try | ||
lJSON.AddPair('log_tag', TJSONString.Create(aLogItem.LogTag)); | ||
lJSON.AddPair('log_level', TJSONString.Create(aLogItem.LogTypeAsString)); | ||
lJSON.AddPair('log_message', TJSONString.Create(aLogItem.LogMessage)); | ||
lJSON.AddPair('log_datetime', TJSONString.Create(DateToISO8601(aLogItem.TimeStamp))); | ||
lJSON.AddPair('timestamp', TJSONString.Create(DateToISO8601(aLogItem.TimeStamp))); | ||
|
||
lRequestBody := TStringStream.Create(lJSON.ToString); | ||
try | ||
InternalWriteLog(GetRESTUrl, aLogItem, lRequestBody); | ||
finally | ||
lRequestBody.Free | ||
end; | ||
finally | ||
lJSON.Free; | ||
end; | ||
end; | ||
|
||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
object MainForm: TMainForm | ||
Left = 0 | ||
Top = 0 | ||
Caption = 'LoggerPro SAMPLE' | ||
ClientHeight = 142 | ||
ClientWidth = 584 | ||
Color = clBtnFace | ||
Font.Charset = DEFAULT_CHARSET | ||
Font.Color = clWindowText | ||
Font.Height = -11 | ||
Font.Name = 'Tahoma' | ||
Font.Style = [] | ||
OldCreateOrder = False | ||
Visible = True | ||
PixelsPerInch = 96 | ||
TextHeight = 13 | ||
object Button1: TButton | ||
Left = 8 | ||
Top = 8 | ||
Width = 137 | ||
Height = 57 | ||
Caption = 'DEBUG' | ||
TabOrder = 0 | ||
OnClick = Button1Click | ||
end | ||
object Button2: TButton | ||
Left = 151 | ||
Top = 8 | ||
Width = 137 | ||
Height = 57 | ||
Caption = 'INFO' | ||
TabOrder = 1 | ||
OnClick = Button2Click | ||
end | ||
object Button3: TButton | ||
Left = 294 | ||
Top = 8 | ||
Width = 137 | ||
Height = 57 | ||
Caption = 'WARNING' | ||
TabOrder = 2 | ||
OnClick = Button3Click | ||
end | ||
object Button4: TButton | ||
Left = 437 | ||
Top = 8 | ||
Width = 137 | ||
Height = 57 | ||
Caption = 'ERROR' | ||
TabOrder = 3 | ||
OnClick = Button4Click | ||
end | ||
object Button5: TButton | ||
Left = 8 | ||
Top = 71 | ||
Width = 280 | ||
Height = 57 | ||
Caption = 'Multithread logging' | ||
TabOrder = 4 | ||
OnClick = Button5Click | ||
end | ||
end |
Oops, something went wrong.