Added InlineFields and InlineDetails options to JSON formatter
This commit is contained in:
parent
d6a2cbcb36
commit
97239e26eb
@ -30,7 +30,8 @@ package X2LogJsonDXE2;
|
|||||||
|
|
||||||
requires
|
requires
|
||||||
rtl,
|
rtl,
|
||||||
X2LogDXE2;
|
X2LogDXE2,
|
||||||
|
soaprtl;
|
||||||
|
|
||||||
contains
|
contains
|
||||||
X2Log.TextFormatter.Json in '..\..\X2Log.TextFormatter.Json.pas',
|
X2Log.TextFormatter.Json in '..\..\X2Log.TextFormatter.Json.pas',
|
||||||
|
@ -125,6 +125,7 @@
|
|||||||
</DelphiCompile>
|
</DelphiCompile>
|
||||||
<DCCReference Include="rtl.dcp"/>
|
<DCCReference Include="rtl.dcp"/>
|
||||||
<DCCReference Include="X2LogDXE2.dcp"/>
|
<DCCReference Include="X2LogDXE2.dcp"/>
|
||||||
|
<DCCReference Include="soaprtl.dcp"/>
|
||||||
<DCCReference Include="..\..\X2Log.TextFormatter.Json.pas"/>
|
<DCCReference Include="..\..\X2Log.TextFormatter.Json.pas"/>
|
||||||
<DCCReference Include="..\..\X2Log.JsonDataObjects.pas"/>
|
<DCCReference Include="..\..\X2Log.JsonDataObjects.pas"/>
|
||||||
<BuildConfiguration Include="Release">
|
<BuildConfiguration Include="Release">
|
||||||
|
@ -69,6 +69,7 @@ type
|
|||||||
protected
|
protected
|
||||||
{ IX2LogTextFormatterHelper }
|
{ IX2LogTextFormatterHelper }
|
||||||
function GetDetailsFilename: string;
|
function GetDetailsFilename: string;
|
||||||
|
function SaveDetailsToStream(AStream: TStream): Boolean;
|
||||||
|
|
||||||
property Entry: TX2LogQueueEntry read FEntry;
|
property Entry: TX2LogQueueEntry read FEntry;
|
||||||
property LogFileName: string read FLogFileName;
|
property LogFileName: string read FLogFileName;
|
||||||
@ -246,4 +247,15 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
|
function TX2LogFileTextFormatterHelper.SaveDetailsToStream(AStream: TStream): Boolean;
|
||||||
|
var
|
||||||
|
logDetailsStreamable: IX2LogDetailsStreamable;
|
||||||
|
|
||||||
|
begin
|
||||||
|
Result := Supports(Entry.Details, IX2LogDetailsStreamable, logDetailsStreamable);
|
||||||
|
if Result then
|
||||||
|
logDetailsStreamable.SaveToStream(AStream);
|
||||||
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
@ -2,12 +2,15 @@ unit X2Log.TextFormatter.Intf;
|
|||||||
|
|
||||||
interface
|
interface
|
||||||
uses
|
uses
|
||||||
|
System.Classes,
|
||||||
|
|
||||||
X2Log.Intf;
|
X2Log.Intf;
|
||||||
|
|
||||||
type
|
type
|
||||||
IX2LogTextFormatterHelper = interface
|
IX2LogTextFormatterHelper = interface
|
||||||
['{D1A1DAD5-0F96-491F-8BD5-0B9D0BE87C32}']
|
['{D1A1DAD5-0F96-491F-8BD5-0B9D0BE87C32}']
|
||||||
function GetDetailsFilename: string;
|
function GetDetailsFilename: string;
|
||||||
|
function SaveDetailsToStream(AStream: TStream): Boolean;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
IX2LogTextFormatter = interface
|
IX2LogTextFormatter = interface
|
||||||
|
@ -22,29 +22,45 @@ type
|
|||||||
TX2LogJsonTextFormatter = class(TInterfacedObject, IX2LogTextFormatter)
|
TX2LogJsonTextFormatter = class(TInterfacedObject, IX2LogTextFormatter)
|
||||||
private
|
private
|
||||||
FSingleLine: Boolean;
|
FSingleLine: Boolean;
|
||||||
|
FInlineFields: Boolean;
|
||||||
|
FInlineDetails: Boolean;
|
||||||
FStyle: TX2LogJsonStyle;
|
FStyle: TX2LogJsonStyle;
|
||||||
protected
|
protected
|
||||||
function GetText(AHelper: IX2LogTextFormatterHelper; ALevel: TX2LogLevel; ADateTime: TDateTime; const AMessage: string; const ACategory: string; ADetails: IX2LogDetails): string;
|
function GetText(AHelper: IX2LogTextFormatterHelper; ALevel: TX2LogLevel; ADateTime: TDateTime; const AMessage: string; const ACategory: string; ADetails: IX2LogDetails): string;
|
||||||
procedure AddDictionaryDetails(AObject: TJsonObject; ADetails: IX2LogDetailsDictionary);
|
procedure AddDictionaryDetails(AObject: TJsonObject; ADetails: IX2LogDetailsDictionary);
|
||||||
|
|
||||||
property SingleLine: Boolean read FSingleLine;
|
property SingleLine: Boolean read FSingleLine;
|
||||||
|
property InlineFields: Boolean read FInlineFields;
|
||||||
|
property InlineDetails: Boolean read FInlineDetails;
|
||||||
property Style: TX2LogJsonStyle read FStyle;
|
property Style: TX2LogJsonStyle read FStyle;
|
||||||
public
|
public
|
||||||
constructor Create(ASingleLine: Boolean = True; AStyle: TX2LogJsonStyle = TX2LogJsonStyle.Default);
|
{
|
||||||
|
If AInlineFields is False, dictionary keys/values will be added to a
|
||||||
|
"fields" object instead of directly in the message, similar to Serilog's
|
||||||
|
Elasticsearch sink option with the same name.
|
||||||
|
|
||||||
|
If AInlineDetails is False, the details will be written to a separate file and
|
||||||
|
the detailsFile value will contain the file name.
|
||||||
|
}
|
||||||
|
constructor Create(ASingleLine: Boolean = True; AStyle: TX2LogJsonStyle = TX2LogJsonStyle.Default; AInlineFields: Boolean = True; AInlineDetails: Boolean = True);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
uses
|
uses
|
||||||
|
Soap.EncdDecd,
|
||||||
|
System.Classes,
|
||||||
System.SysUtils;
|
System.SysUtils;
|
||||||
|
|
||||||
|
|
||||||
{ TX2LogJsonTextFormatter }
|
{ TX2LogJsonTextFormatter }
|
||||||
constructor TX2LogJsonTextFormatter.Create(ASingleLine: Boolean; AStyle: TX2LogJsonStyle);
|
constructor TX2LogJsonTextFormatter.Create(ASingleLine: Boolean; AStyle: TX2LogJsonStyle; AInlineFields, AInlineDetails: Boolean);
|
||||||
begin
|
begin
|
||||||
inherited Create;
|
inherited Create;
|
||||||
|
|
||||||
FSingleLine := ASingleLine;
|
FSingleLine := ASingleLine;
|
||||||
|
FInlineFields := AInlineFields;
|
||||||
|
FInlineDetails := AInlineDetails;
|
||||||
FStyle := AStyle;
|
FStyle := AStyle;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -58,6 +74,8 @@ var
|
|||||||
line: TJsonObject;
|
line: TJsonObject;
|
||||||
dictionaryDetails: IX2LogDetailsDictionary;
|
dictionaryDetails: IX2LogDetailsDictionary;
|
||||||
detailsFileName: string;
|
detailsFileName: string;
|
||||||
|
detailsStream: TMemoryStream;
|
||||||
|
encodedStream: TStringStream;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
line := TJsonObject.Create;
|
line := TJsonObject.Create;
|
||||||
@ -82,12 +100,36 @@ begin
|
|||||||
|
|
||||||
if Supports(ADetails, IX2LogDetailsDictionary, dictionaryDetails) then
|
if Supports(ADetails, IX2LogDetailsDictionary, dictionaryDetails) then
|
||||||
begin
|
begin
|
||||||
AddDictionaryDetails(line, dictionaryDetails);
|
if InlineFields then
|
||||||
|
AddDictionaryDetails(line, dictionaryDetails)
|
||||||
|
else
|
||||||
|
AddDictionaryDetails(line.O['fields'], dictionaryDetails);
|
||||||
end else
|
end else
|
||||||
begin
|
begin
|
||||||
detailsFileName := AHelper.GetDetailsFilename;
|
if InlineDetails then
|
||||||
if Length(detailsFileName) > 0 then
|
begin
|
||||||
line.S['details'] := ExtractFileName(detailsFileName);
|
detailsStream := TMemoryStream.Create;
|
||||||
|
try
|
||||||
|
if AHelper.SaveDetailsToStream(detailsStream) then
|
||||||
|
begin
|
||||||
|
detailsStream.Position := 0;
|
||||||
|
encodedStream := TStringStream.Create;
|
||||||
|
try
|
||||||
|
EncodeStream(detailsStream, encodedStream);
|
||||||
|
line.S['details'] := encodedStream.DataString;
|
||||||
|
finally
|
||||||
|
FreeAndNil(encodedStream);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
finally
|
||||||
|
FreeAndNil(detailsStream);
|
||||||
|
end;
|
||||||
|
end else
|
||||||
|
begin
|
||||||
|
detailsFileName := AHelper.GetDetailsFilename;
|
||||||
|
if Length(detailsFileName) > 0 then
|
||||||
|
line.S['detailsFile'] := ExtractFileName(detailsFileName);
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
Result := line.ToJSON(SingleLine);
|
Result := line.ToJSON(SingleLine);
|
||||||
|
Loading…
Reference in New Issue
Block a user