2014-05-18 18:09:07 +00:00
|
|
|
unit X2Log;
|
|
|
|
|
|
|
|
interface
|
|
|
|
uses
|
|
|
|
System.Classes,
|
|
|
|
System.Generics.Collections,
|
|
|
|
System.SysUtils,
|
|
|
|
|
2014-05-25 14:20:58 +00:00
|
|
|
X2Log.Intf,
|
|
|
|
X2Log.Client.Base;
|
2014-05-18 18:09:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
type
|
2014-10-20 13:38:28 +00:00
|
|
|
TX2Log = class(TX2LogBaseClient, IX2Log)
|
2014-05-18 18:09:07 +00:00
|
|
|
private
|
|
|
|
FExceptionStrategy: IX2LogExceptionStrategy;
|
2014-10-20 12:07:44 +00:00
|
|
|
protected
|
2014-05-18 18:09:07 +00:00
|
|
|
property ExceptionStrategy: IX2LogExceptionStrategy read FExceptionStrategy;
|
|
|
|
public
|
|
|
|
constructor Create;
|
|
|
|
|
|
|
|
{ IX2Log }
|
|
|
|
procedure SetExceptionStrategy(AStrategy: IX2LogExceptionStrategy);
|
|
|
|
|
2014-10-20 12:07:44 +00:00
|
|
|
function Category(const ACategory: string): IX2Log;
|
|
|
|
|
|
|
|
procedure Verbose(const AMessage: string; const ADetails: string = '');
|
|
|
|
procedure VerboseEx(const AMessage: string; ADetails: IX2LogDetails = nil);
|
2016-10-10 20:17:53 +00:00
|
|
|
procedure VerboseS(const AMessage: string; ANamedParams: array of const);
|
2014-10-20 12:07:44 +00:00
|
|
|
|
|
|
|
procedure Info(const AMessage: string; const ADetails: string = '');
|
|
|
|
procedure InfoEx(const AMessage: string; ADetails: IX2LogDetails = nil);
|
2016-10-10 20:17:53 +00:00
|
|
|
procedure InfoS(const AMessage: string; ANamedParams: array of const);
|
2014-10-20 12:07:44 +00:00
|
|
|
|
|
|
|
procedure Warning(const AMessage: string; const ADetails: string = '');
|
|
|
|
procedure WarningEx(const AMessage: string; ADetails: IX2LogDetails = nil);
|
2016-10-10 20:17:53 +00:00
|
|
|
procedure WarningS(const AMessage: string; ANamedParams: array of const);
|
2014-10-20 12:07:44 +00:00
|
|
|
|
|
|
|
procedure Error(const AMessage: string; const ADetails: string = '');
|
|
|
|
procedure ErrorEx(const AMessage: string; ADetails: IX2LogDetails = nil);
|
2016-10-10 20:17:53 +00:00
|
|
|
procedure ErrorS(const AMessage: string; ANamedParams: array of const);
|
2014-10-20 12:07:44 +00:00
|
|
|
|
2014-05-30 12:51:01 +00:00
|
|
|
procedure Exception(AException: Exception; const AMessage: string = '');
|
2014-10-20 13:38:28 +00:00
|
|
|
procedure ExceptionEx(AException: Exception; const AMessage: string = ''; const ACategory: string = '');
|
2017-04-26 15:20:48 +00:00
|
|
|
procedure ExceptionS(AException: Exception; const AMessage: string; ANamedParams: array of const);
|
2014-05-18 18:09:07 +00:00
|
|
|
end;
|
2014-10-20 12:07:44 +00:00
|
|
|
|
|
|
|
|
2014-05-18 18:09:07 +00:00
|
|
|
implementation
|
|
|
|
uses
|
2014-10-20 12:07:44 +00:00
|
|
|
X2Log.Constants,
|
2014-10-20 13:38:28 +00:00
|
|
|
X2Log.Decorator,
|
2014-05-30 12:51:01 +00:00
|
|
|
X2Log.Details.Default,
|
2014-05-18 18:09:07 +00:00
|
|
|
X2Log.Exception.Default;
|
|
|
|
|
|
|
|
|
|
|
|
{ TX2Log }
|
|
|
|
constructor TX2Log.Create;
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
|
|
|
|
SetExceptionStrategy(nil);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TX2Log.SetExceptionStrategy(AStrategy: IX2LogExceptionStrategy);
|
|
|
|
begin
|
|
|
|
if Assigned(AStrategy) then
|
|
|
|
FExceptionStrategy := AStrategy
|
|
|
|
else
|
|
|
|
FExceptionStrategy := TX2LogDefaultExceptionStrategy.Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2014-10-20 12:07:44 +00:00
|
|
|
function TX2Log.Category(const ACategory: string): IX2Log;
|
|
|
|
begin
|
|
|
|
Result := TX2LogCategoryDecorator.Create(Self, ACategory);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2014-05-18 18:09:07 +00:00
|
|
|
procedure TX2Log.Verbose(const AMessage, ADetails: string);
|
2014-05-30 12:51:01 +00:00
|
|
|
begin
|
2014-10-20 12:07:44 +00:00
|
|
|
Log(TX2LogLevel.Verbose, AMessage, LogCategoryDefault, TX2LogStringDetails.CreateIfNotEmpty(ADetails));
|
2014-05-30 12:51:01 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TX2Log.VerboseEx(const AMessage: string; ADetails: IX2LogDetails);
|
2014-05-18 18:09:07 +00:00
|
|
|
begin
|
2014-10-20 12:07:44 +00:00
|
|
|
Log(TX2LogLevel.Verbose, AMessage, LogCategoryDefault, ADetails);
|
2014-05-18 18:09:07 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2016-10-10 20:17:53 +00:00
|
|
|
procedure TX2Log.VerboseS(const AMessage: string; ANamedParams: array of const);
|
|
|
|
begin
|
|
|
|
Log(TX2LogLevel.Verbose, Now, AMessage, LogCategoryDefault, TX2LogDictionaryDetails.CreateIfNotEmpty(ANamedParams));
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2014-05-18 18:09:07 +00:00
|
|
|
procedure TX2Log.Info(const AMessage, ADetails: string);
|
2014-05-30 12:51:01 +00:00
|
|
|
begin
|
2014-10-20 12:07:44 +00:00
|
|
|
Log(TX2LogLevel.Info, AMessage, LogCategoryDefault, TX2LogStringDetails.CreateIfNotEmpty(ADetails));
|
2014-05-30 12:51:01 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TX2Log.InfoEx(const AMessage: string; ADetails: IX2LogDetails);
|
2014-05-18 18:09:07 +00:00
|
|
|
begin
|
2014-10-20 12:07:44 +00:00
|
|
|
Log(TX2LogLevel.Info, AMessage, LogCategoryDefault, ADetails);
|
2014-05-18 18:09:07 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2016-10-10 20:17:53 +00:00
|
|
|
procedure TX2Log.InfoS(const AMessage: string; ANamedParams: array of const);
|
|
|
|
begin
|
|
|
|
Log(TX2LogLevel.Info, Now, AMessage, LogCategoryDefault, TX2LogDictionaryDetails.CreateIfNotEmpty(ANamedParams));
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2014-05-18 18:09:07 +00:00
|
|
|
procedure TX2Log.Warning(const AMessage, ADetails: string);
|
2014-05-30 12:51:01 +00:00
|
|
|
begin
|
2014-10-20 12:07:44 +00:00
|
|
|
Log(TX2LogLevel.Warning, AMessage, LogCategoryDefault, TX2LogStringDetails.CreateIfNotEmpty(ADetails));
|
2014-05-30 12:51:01 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TX2Log.WarningEx(const AMessage: string; ADetails: IX2LogDetails);
|
2014-05-18 18:09:07 +00:00
|
|
|
begin
|
2014-10-20 12:07:44 +00:00
|
|
|
Log(TX2LogLevel.Warning, AMessage, LogCategoryDefault, ADetails);
|
2014-05-18 18:09:07 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2016-10-10 20:17:53 +00:00
|
|
|
procedure TX2Log.WarningS(const AMessage: string; ANamedParams: array of const);
|
|
|
|
begin
|
|
|
|
Log(TX2LogLevel.Warning, Now, AMessage, LogCategoryDefault, TX2LogDictionaryDetails.CreateIfNotEmpty(ANamedParams));
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2014-05-18 18:09:07 +00:00
|
|
|
procedure TX2Log.Error(const AMessage, ADetails: string);
|
2014-05-30 12:51:01 +00:00
|
|
|
begin
|
2014-10-20 12:07:44 +00:00
|
|
|
Log(TX2LogLevel.Error, AMessage, LogCategoryDefault, TX2LogStringDetails.CreateIfNotEmpty(ADetails));
|
2014-05-30 12:51:01 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TX2Log.ErrorEx(const AMessage: string; ADetails: IX2LogDetails);
|
2014-05-18 18:09:07 +00:00
|
|
|
begin
|
2014-10-20 12:07:44 +00:00
|
|
|
Log(TX2LogLevel.Error, AMessage, LogCategoryDefault, ADetails);
|
2014-05-18 18:09:07 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2016-10-10 20:17:53 +00:00
|
|
|
procedure TX2Log.ErrorS(const AMessage: string; ANamedParams: array of const);
|
|
|
|
begin
|
|
|
|
Log(TX2LogLevel.Error, Now, AMessage, LogCategoryDefault, TX2LogDictionaryDetails.CreateIfNotEmpty(ANamedParams));
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2014-05-30 12:51:01 +00:00
|
|
|
procedure TX2Log.Exception(AException: Exception; const AMessage: string);
|
2014-10-20 12:07:44 +00:00
|
|
|
begin
|
2014-10-20 13:38:28 +00:00
|
|
|
ExceptionEx(AException, AMessage, LogCategoryDefault);
|
2014-10-20 12:07:44 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2014-10-20 13:38:28 +00:00
|
|
|
procedure TX2Log.ExceptionEx(AException: Exception; const AMessage, ACategory: string);
|
2014-05-18 18:09:07 +00:00
|
|
|
var
|
|
|
|
msg: string;
|
2017-04-26 15:20:48 +00:00
|
|
|
exceptionMsg: string;
|
|
|
|
detailsText: TStringBuilder;
|
2014-05-30 12:51:01 +00:00
|
|
|
details: IX2LogDetails;
|
2014-05-18 18:09:07 +00:00
|
|
|
|
|
|
|
begin
|
|
|
|
msg := AMessage;
|
2014-05-30 12:51:01 +00:00
|
|
|
details := nil;
|
2014-05-18 18:09:07 +00:00
|
|
|
|
2017-04-26 15:20:48 +00:00
|
|
|
detailsText := TStringBuilder.Create;
|
|
|
|
try
|
|
|
|
ExceptionStrategy.Execute(AException, exceptionMsg,
|
|
|
|
procedure(const AKey, AValue: string)
|
|
|
|
begin
|
|
|
|
detailsText.Append(AKey).Append(': ').Append(AValue).AppendLine;
|
|
|
|
end);
|
|
|
|
|
|
|
|
if Length(exceptionMsg) > 0 then
|
|
|
|
begin
|
|
|
|
if Length(msg) > 0 then
|
|
|
|
msg := msg + ': ';
|
|
|
|
|
|
|
|
msg := msg + exceptionMsg;
|
|
|
|
end;
|
|
|
|
|
|
|
|
details := TX2LogStringDetails.CreateIfNotEmpty(detailsText.ToString);
|
|
|
|
finally
|
|
|
|
FreeAndNil(detailsText);
|
|
|
|
end;
|
|
|
|
|
2014-10-20 13:38:28 +00:00
|
|
|
Log(TX2LogLevel.Error, msg, ACategory, details);
|
2014-05-18 18:09:07 +00:00
|
|
|
end;
|
|
|
|
|
2017-04-26 15:20:48 +00:00
|
|
|
|
|
|
|
procedure TX2Log.ExceptionS(AException: Exception; const AMessage: string; ANamedParams: array of const);
|
|
|
|
var
|
|
|
|
details: IX2LogDetailsDictionaryWriter;
|
|
|
|
exceptionMsg: string;
|
|
|
|
|
|
|
|
begin
|
|
|
|
details := TX2LogDictionaryDetails.CreateIfNotEmpty(ANamedParams);
|
|
|
|
|
|
|
|
ExceptionStrategy.Execute(AException, exceptionMsg,
|
|
|
|
procedure(const AKey, AValue: string)
|
|
|
|
begin
|
|
|
|
if not Assigned(details) then
|
|
|
|
details := TX2LogDictionaryDetails.Create([]);
|
|
|
|
|
|
|
|
details.SetStringValue(AKey, AValue);
|
|
|
|
end);
|
|
|
|
|
|
|
|
|
|
|
|
if Length(exceptionMsg) > 0 then
|
|
|
|
begin
|
|
|
|
if not Assigned(details) then
|
|
|
|
details := TX2LogDictionaryDetails.Create([]);
|
|
|
|
|
|
|
|
details.SetStringValue('Message', exceptionMsg);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
Log(TX2LogLevel.Error, Now, AMessage, LogCategoryDefault, details);
|
|
|
|
end;
|
|
|
|
|
2014-05-18 18:09:07 +00:00
|
|
|
end.
|