mirror of
https://github.com/danieleteti/delphimvcframework.git
synced 2024-11-15 15:55:54 +01:00
Merge pull request #332 from andreaciotti/feature/configurable_log
Configurable log for requests
This commit is contained in:
commit
106e1c2bd4
89
sources/MVCFramework.Log.pas
Normal file
89
sources/MVCFramework.Log.pas
Normal 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.
|
@ -985,6 +985,7 @@ function CreateResponse(const StatusCode: UInt16; const ReasonString: String; co
|
|||||||
implementation
|
implementation
|
||||||
|
|
||||||
uses
|
uses
|
||||||
|
MVCFramework.Log,
|
||||||
MVCFramework.Router,
|
MVCFramework.Router,
|
||||||
MVCFramework.SysControllers,
|
MVCFramework.SysControllers,
|
||||||
MVCFramework.Serializer.JsonDataObjects,
|
MVCFramework.Serializer.JsonDataObjects,
|
||||||
@ -2143,10 +2144,7 @@ begin
|
|||||||
end;
|
end;
|
||||||
ExecuteAfterControllerActionMiddleware(LContext, LRouter.MethodToCall.Name, LHandled);
|
ExecuteAfterControllerActionMiddleware(LContext, LRouter.MethodToCall.Name, LHandled);
|
||||||
LContext.Response.ContentType := LSelectedController.ContentType;
|
LContext.Response.ContentType := LSelectedController.ContentType;
|
||||||
Log(TLogLevel.levNormal, ARequest.Method + ':' + ARequest.RawPathInfo + ' -> ' +
|
TMVCFrameworkLog.LogAction(LRouter, ARequest, AResponse);
|
||||||
LRouter.ControllerClazz.QualifiedClassName + ' - ' + IntToStr(AResponse.StatusCode) + ' ' +
|
|
||||||
AResponse.ReasonString)
|
|
||||||
|
|
||||||
end
|
end
|
||||||
else // execute-routing
|
else // execute-routing
|
||||||
begin
|
begin
|
||||||
@ -2168,8 +2166,7 @@ begin
|
|||||||
if not Result then
|
if not Result then
|
||||||
begin
|
begin
|
||||||
// HTTP404(LContext);
|
// HTTP404(LContext);
|
||||||
Log(TLogLevel.levNormal, ARequest.Method + ':' + ARequest.RawPathInfo + ' -> NO ACTION ' + ' - ' +
|
TMVCFrameworkLog.LogNoAction(ARequest, AResponse);
|
||||||
IntToStr(AResponse.StatusCode) + ' ' + AResponse.ReasonString);
|
|
||||||
raise EMVCException.Create(HTTP_STATUS.NotFound, 'Not Found');
|
raise EMVCException.Create(HTTP_STATUS.NotFound, 'Not Found');
|
||||||
end;
|
end;
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user