3030% % Internal logging interface
3131-export ([log /1 , log /2 ]).
3232
33+ -include_lib (" kernel/include/logger.hrl" ).
3334-include (" cowmachine_state.hrl" ).
3435-include (" cowmachine_log.hrl" ).
3536
@@ -84,7 +85,7 @@ request_1(Controller, Req, Env, Options, Context) ->
8485 catch
8586 throw :{stop_request , 500 , {Reason , Stacktrace }} when is_list (Stacktrace ) ->
8687 log (#{ at => ? AT , level => error , code => 500 , text => " Stop request" ,
87- reason => Reason , stacktrace => Stacktrace }, Req ),
88+ reason => Reason , stack => Stacktrace }, Req ),
8889 handle_stop_request (500 , Site , undefined , Req , Env , State , Context );
8990 throw :{stop_request , 500 , Reason } ->
9091 log (#{ at => ? AT , level => error , code => 500 , text => " Stop request" , reason => Reason }, Req ),
@@ -93,7 +94,7 @@ request_1(Controller, Req, Env, Options, Context) ->
9394 handle_stop_request (ResponseCode , Site , {throw , Reason }, Req , Env , State , Context );
9495 throw :{stop_request , 500 }:Stacktrace ->
9596 log (#{ at => ? AT , level => error , code => 500 , text => " Stop request" ,
96- stacktrace => Stacktrace }, Req ),
97+ stack => Stacktrace }, Req ),
9798 handle_stop_request (500 , Site , undefined , Req , Env , State , Context );
9899 throw :{stop_request , ResponseCode } when is_integer (ResponseCode ), ResponseCode >= 400 , ResponseCode < 500 ->
99100 handle_stop_request (ResponseCode , Site , undefined , Req , Env , State , Context );
@@ -108,12 +109,12 @@ request_1(Controller, Req, Env, Options, Context) ->
108109 throw :Reason :Stacktrace ->
109110 log (#{ at => ? AT , level => error , code => 500 , text => " Unexpected throw" ,
110111 class => throw , reason => Reason ,
111- stacktrace => Stacktrace }, Req ),
112+ stack => Stacktrace }, Req ),
112113 handle_stop_request (500 , Site , {throw , {Reason , Stacktrace }}, Req , Env , State , Context );
113114 Class :Reason :Stacktrace ->
114115 log (#{ at => ? AT , level => error , code => 500 , text => " Unexpected exception" ,
115116 class => Class , reason => Reason ,
116- stacktrace => Stacktrace }, Req ),
117+ stack => Stacktrace }, Req ),
117118 {stop , cowboy_req :reply (500 , Req )}
118119 end .
119120
@@ -132,19 +133,19 @@ handle_stop_request(ResponseCode, _Site, Reason, Req, Env, State, Context) ->
132133 ContextRespCode = cowmachine_req :set_response_code (ResponseCode , ContextResult ),
133134 cowmachine_response :send_response (ContextRespCode )
134135 catch
135- throw :{stop_request , Code , Reason } ->
136+ throw :{stop_request , Code , CReason } ->
136137 log (#{ at => ? AT , level => warning ,
137138 text => " Stop request" ,
138139 code => Code ,
139- reason => Reason }, Req ),
140+ reason => CReason }, Req ),
140141 {stop , cowboy_req :reply (Code , Req )};
141- Class :Reason :Stacktrace ->
142+ Class :CReason :Stacktrace ->
142143 log (#{ at => ? AT , level => warning ,
143144 text => " Unexpected exception" ,
144145 code => 500 ,
145146 class => Class ,
146- reason => Reason ,
147- stacktrace => Stacktrace
147+ reason => CReason ,
148+ stack => Stacktrace
148149 }, Req ),
149150 {stop , cowboy_req :reply (500 , Req )}
150151 end .
@@ -155,7 +156,10 @@ handle_stop_request(ResponseCode, _Site, Reason, Req, Env, State, Context) ->
155156% %
156157
157158log (#{ level := Level } = Report ) ->
158- log_report (Level , Report #{in => cowmachine }).
159+ log_report (Level , Report #{
160+ in => cowmachine ,
161+ node => node ()
162+ }).
159163
160164log (#{ level := Level } = Report , Req ) when is_map (Req ) ->
161165 Report1 = lists :foldl (fun ({Key , Fun }, Acc ) ->
@@ -165,23 +169,25 @@ log(#{ level := Level } = Report, Req) when is_map(Req) ->
165169 end
166170 end , Report , [{src , fun src /1 },
167171 {dst , fun dst /1 },
168- {path , fun path /1 }]),
169- log_report (Level , Report1 #{in => cowmachine }).
170-
171- log_report (debug , _Report ) ->
172- % % Ignore for now - re-enable for logger
173- ok ;
174- log_report (Level , Report ) when is_map (Report ) ->
175- % % @todo also implement error logging for erlang 21 and higher.
176- Function = case Level of
177- error -> error_report ;
178- warning -> warning_report ;
179- info -> info_report
180- end ,
181- error_logger :Function (maps :to_list (Report )).
172+ {path , fun path /1 }]),
173+ log_report (Level , Report1 #{
174+ in => cowmachine ,
175+ node => node ()
176+ }).
177+
178+ log_report (debug , Report ) when is_map (Report ) ->
179+ ? LOG_DEBUG (Report );
180+ log_report (info , Report ) when is_map (Report ) ->
181+ ? LOG_INFO (Report );
182+ log_report (notice , Report ) when is_map (Report ) ->
183+ ? LOG_NOTICE (Report );
184+ log_report (warning , Report ) when is_map (Report ) ->
185+ ? LOG_WARNING (Report );
186+ log_report (error , Report ) when is_map (Report ) ->
187+ ? LOG_ERROR (Report ).
182188
183189src (#{ peer := {IP , Port } }) -> {ok , ip_info (IP , Port )};
184- src (_ ) -> undefined .
190+ src (_ ) -> undefined .
185191
186192dst (#{ sock := {IP , Port } } ) -> {ok , ip_info (IP , Port )};
187193dst (#{ port := Port }) -> {ok , #{ port => Port }};
0 commit comments