diff --git a/sources/MVCFramework.Log.pas b/sources/MVCFramework.Log.pas new file mode 100644 index 00000000..3c9cdd3b --- /dev/null +++ b/sources/MVCFramework.Log.pas @@ -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. diff --git a/sources/MVCFramework.pas b/sources/MVCFramework.pas index 1416cca9..2ad06a60 100644 --- a/sources/MVCFramework.pas +++ b/sources/MVCFramework.pas @@ -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