Merge pull request #332 from andreaciotti/feature/configurable_log

Configurable log for requests
This commit is contained in:
Daniele Teti 2020-02-24 11:04:17 +01:00 committed by GitHub
commit 106e1c2bd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 92 additions and 6 deletions

View File

@ -0,0 +1,89 @@
unit MVCFramework.Log;
interface
uses
MVCFramework.Router,
Web.HTTPApp;
type
TLogActionProc = reference to procedure(const ARouter: TMVCRouter; const ARequest: TWebRequest;
const AResponse: TWebResponse);
TLogNoActionProc = reference to procedure(const ARequest: TWebRequest; const AResponse: TWebResponse);
TMVCFrameworkLog = class
private class var
FLogActionProc: TLogActionProc;
FLogNoActionProc: TLogNoActionProc;
public
class constructor Create;
class procedure LogAction(const ARouter: TMVCRouter; const ARequest: TWebRequest;
const AResponse: TWebResponse);
class procedure LogNoAction(const ARequest: TWebRequest; const AResponse: TWebResponse);
class procedure SetLogActionProc(const ALogActionProc: TLogActionProc);
class procedure SetLogNoActionProc(const ALogNoActionProc: TLogNoActionProc);
end;
implementation
uses
MVCFramework.Logger,
System.SysUtils;
procedure LogAction(const ARouter: TMVCRouter; const ARequest: TWebRequest; const AResponse: TWebResponse);
begin
Log(TLogLevel.levNormal, ARequest.Method + ':' + ARequest.RawPathInfo + ' -> ' +
ARouter.ControllerClazz.QualifiedClassName + ' - ' + IntToStr(AResponse.StatusCode) + ' ' +
AResponse.ReasonString);
end;
procedure LogNoAction(const ARequest: TWebRequest; const AResponse: TWebResponse);
begin
Log(TLogLevel.levNormal, ARequest.Method + ':' + ARequest.RawPathInfo + ' -> NO ACTION ' + ' - ' +
IntToStr(AResponse.StatusCode) + ' ' + AResponse.ReasonString);
end;
{ TMVCFrameworkLog }
class constructor TMVCFrameworkLog.Create;
begin
// Default log actions
SetLogActionProc(
procedure(const ARouter: TMVCRouter; const ARequest: TWebRequest; const AResponse: TWebResponse)
begin
Log(TLogLevel.levNormal, ARequest.Method + ':' + ARequest.RawPathInfo + ' -> ' +
ARouter.ControllerClazz.QualifiedClassName + ' - ' + IntToStr(AResponse.StatusCode) + ' ' +
AResponse.ReasonString)
end);
SetLogNoActionProc(
procedure(const ARequest: TWebRequest; const AResponse: TWebResponse)
begin
Log(TLogLevel.levNormal, ARequest.Method + ':' + ARequest.RawPathInfo + ' -> NO ACTION ' + ' - ' +
IntToStr(AResponse.StatusCode) + ' ' + AResponse.ReasonString);
end);
end;
class procedure TMVCFrameworkLog.LogAction(const ARouter: TMVCRouter; const ARequest: TWebRequest;
const AResponse: TWebResponse);
begin
FLogActionProc(ARouter, ARequest, AResponse);
end;
class procedure TMVCFrameworkLog.LogNoAction(const ARequest: TWebRequest; const AResponse: TWebResponse);
begin
FLogNoActionProc(ARequest, AResponse);
end;
class procedure TMVCFrameworkLog.SetLogActionProc(const ALogActionProc: TLogActionProc);
begin
Assert(Assigned(ALogActionProc));
FLogActionProc := ALogActionProc;
end;
class procedure TMVCFrameworkLog.SetLogNoActionProc(const ALogNoActionProc: TLogNoActionProc);
begin
Assert(Assigned(ALogNoActionProc));
FLogNoActionProc := ALogNoActionProc;
end;
end.

View File

@ -985,6 +985,7 @@ function CreateResponse(const StatusCode: UInt16; const ReasonString: String; co
implementation
uses
MVCFramework.Log,
MVCFramework.Router,
MVCFramework.SysControllers,
MVCFramework.Serializer.JsonDataObjects,
@ -2143,10 +2144,7 @@ begin
end;
ExecuteAfterControllerActionMiddleware(LContext, LRouter.MethodToCall.Name, LHandled);
LContext.Response.ContentType := LSelectedController.ContentType;
Log(TLogLevel.levNormal, ARequest.Method + ':' + ARequest.RawPathInfo + ' -> ' +
LRouter.ControllerClazz.QualifiedClassName + ' - ' + IntToStr(AResponse.StatusCode) + ' ' +
AResponse.ReasonString)
TMVCFrameworkLog.LogAction(LRouter, ARequest, AResponse);
end
else // execute-routing
begin
@ -2168,8 +2166,7 @@ begin
if not Result then
begin
// HTTP404(LContext);
Log(TLogLevel.levNormal, ARequest.Method + ':' + ARequest.RawPathInfo + ' -> NO ACTION ' + ' - ' +
IntToStr(AResponse.StatusCode) + ' ' + AResponse.ReasonString);
TMVCFrameworkLog.LogNoAction(ARequest, AResponse);
raise EMVCException.Create(HTTP_STATUS.NotFound, 'Not Found');
end;
end