diff --git a/include/minirest.hrl b/include/minirest.hrl index 22f869f..b06e55b 100644 --- a/include/minirest.hrl +++ b/include/minirest.hrl @@ -67,7 +67,7 @@ function :: atom(), filter :: fun(), authorization :: {Module :: atom(), Function :: atom()} | undefined, - log :: fun() | undefined, + log_meta :: map(), error_codes :: list(error_code()) }). diff --git a/src/minirest_handler.erl b/src/minirest_handler.erl index 8b34e20..a7437f7 100644 --- a/src/minirest_handler.erl +++ b/src/minirest_handler.erl @@ -31,7 +31,7 @@ init(Request0, State)-> {Code, Request1} = handle(Request0, State), ReqEnd = erlang:monotonic_time(), Meta = get_log_meta(), - run_log_hook(Meta, ReqStart, ReqEnd, Code, Request1), + run_log_hook(State, Meta, ReqStart, ReqEnd, Code, Request1), {ok, Request1, State}. %% In previous versions, the metadata was statically derived from the `authorize` method, @@ -56,8 +56,8 @@ handle(Request, State) -> StatusCode, cowboy_req:reply(StatusCode, Headers, <<"">>, Request) }; - {ok, Handler = #handler{path = Path, log = Log, method = MethodAtom}} -> - init_log_meta(#{operation_id => list_to_binary(Path), log => Log, method => MethodAtom}), + {ok, Handler = #handler{path = Path, log_meta = LogMeta, method = MethodAtom}} -> + init_log_meta(LogMeta#{operation_id => list_to_binary(Path), method => MethodAtom}), case do_authorize(Request, Handler) of {ok, AuthMeta} -> update_log_meta(AuthMeta), @@ -255,11 +255,14 @@ maybe_ignore_code_check(400, 'BAD_REQUEST') -> true; maybe_ignore_code_check(500, 'INTERNAL_ERROR') -> true; maybe_ignore_code_check(_, _) -> false. -run_log_hook(#{log := Log} = Meta0, ReqStart, ReqEnd, Code, Req) when is_function(Log) -> - Meta = maps:without([log], Meta0), +run_log_hook(#{log := {M, F, InitMeta}}, Meta0, ReqStart, ReqEnd, Code, Req) -> + Meta = maps:merge(InitMeta, Meta0), + _ = M:F(Meta#{req_start => ReqStart, req_end => ReqEnd, code => Code}, Req), + ok; +run_log_hook(#{log := Log}, Meta, ReqStart, ReqEnd, Code, Req) when is_function(Log) -> _ = Log(Meta#{req_start => ReqStart, req_end => ReqEnd, code => Code}, Req), ok; -run_log_hook(_log_meta, _ReqStart, _ReqEnd, _Code, _Req) -> +run_log_hook(_State, _Meta, _ReqStart, _ReqEnd, _Code, _Req) -> ok. init_log_meta(Init) -> diff --git a/src/minirest_trails.erl b/src/minirest_trails.erl index feaa736..ad8cbe7 100644 --- a/src/minirest_trails.erl +++ b/src/minirest_trails.erl @@ -73,13 +73,13 @@ trails_schemas(BasePath, Authorization, Log, Module, {Path, Metadata, Function, function = Function, authorization = maps:get(security, MethodDef, []) =/= [] andalso Authorization, filter = maps:get(filter, Options, undefined), - log = Log, + log_meta = maps:get(log_meta, MethodDef, #{}), error_codes = ErrorCodes }, minirest_info_api:add_codes(ErrorCodes), maps:put(binary_method(Method), HandlerState, HandlerStates) end, - HandlerStates = maps:fold(Fun, #{}, Metadata), + HandlerStates = maps:fold(Fun, #{log => Log}, Metadata), CompletePath = append_base_path(BasePath, Path), trails:trail(CompletePath, ?HANDLER, HandlerStates, Metadata).