diff options
author | Loïc Hoguin <[email protected]> | 2018-06-28 17:10:18 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2018-06-28 17:10:18 +0200 |
commit | a76c32db5e32ab6a6b35ce6625cb3aedcac067b6 (patch) | |
tree | 745b39ea422a946108cb406d1fbcd3ece678ec39 /src/cowboy_stream_h.erl | |
parent | 16143354361651cc7c14b81595760118efce8870 (diff) | |
download | cowboy-a76c32db5e32ab6a6b35ce6625cb3aedcac067b6.tar.gz cowboy-a76c32db5e32ab6a6b35ce6625cb3aedcac067b6.tar.bz2 cowboy-a76c32db5e32ab6a6b35ce6625cb3aedcac067b6.zip |
Introduce undocumented option logger
This commit reworks the logging that Cowboy does via
error_logger to make the module that will do the actual
logging configurable.
The logger module interface must be the same as logger
and lager: a separate function per log level with the
same log levels they support.
The default behavior remains to call error_logger,
although some messages were downgraded to warnings
instead of errors. Since error_logger only supports
three different log levels, some messages may get
downgraded/upgraded depending on what the original
log level was to make them compatible with error_logger.
The {log, Level, Format, Args} command was also
added to stream handlers. Stream handlers should
use this command to log messages because it allows
writing a stream handler to intercept some of those
messages and extract information or block them as
necessary.
The logger option only applies to Cowboy itself,
not to the messages Ranch logs, so more work remains
to be done in that area.
Diffstat (limited to 'src/cowboy_stream_h.erl')
-rw-r--r-- | src/cowboy_stream_h.erl | 37 |
1 files changed, 15 insertions, 22 deletions
diff --git a/src/cowboy_stream_h.erl b/src/cowboy_stream_h.erl index 21651f7..55a1ca2 100644 --- a/src/cowboy_stream_h.erl +++ b/src/cowboy_stream_h.erl @@ -134,24 +134,32 @@ info(StreamID, Info={'EXIT', Pid, normal}, State=#state{pid=Pid}) -> do_info(StreamID, Info, [stop], State); info(StreamID, Info={'EXIT', Pid, {{request_error, Reason, _HumanReadable}, _}}, State=#state{pid=Pid}) -> - %% @todo Optionally report the crash to help debugging. - %%report_crash(Ref, StreamID, Pid, Reason, Stacktrace), Status = case Reason of timeout -> 408; payload_too_large -> 413; _ -> 400 end, - %% @todo Headers? Details in body? More stuff in debug only? + %% @todo Headers? Details in body? Log the crash? More stuff in debug only? do_info(StreamID, Info, [ {error_response, Status, #{<<"content-length">> => <<"0">>}, <<>>}, stop ], State); info(StreamID, Exit={'EXIT', Pid, {Reason, Stacktrace}}, State=#state{ref=Ref, pid=Pid}) -> - report_crash(Ref, StreamID, Pid, Reason, Stacktrace), + Commands0 = [{internal_error, Exit, 'Stream process crashed.'}], + Commands = case Reason of + normal -> Commands0; + shutdown -> Commands0; + {shutdown, _} -> Commands0; + _ -> [{log, error, + "Ranch listener ~p, connection process ~p, stream ~p " + "had its request process ~p exit with reason " + "~999999p and stacktrace ~999999p~n", + [Ref, self(), StreamID, Pid, Reason, Stacktrace]} + |Commands0] + end, do_info(StreamID, Exit, [ - {error_response, 500, #{<<"content-length">> => <<"0">>}, <<>>}, - {internal_error, Exit, 'Stream process crashed.'} - ], State); + {error_response, 500, #{<<"content-length">> => <<"0">>}, <<>>} + |Commands], State); %% Request body, auto mode, no body buffered. info(StreamID, Info={read_body, Ref, auto, infinity}, State=#state{read_body_buffer= <<>>}) -> do_info(StreamID, Info, [], State#state{ @@ -244,21 +252,6 @@ send_request_body(Pid, Ref, fin, BodyLen, Data) -> Pid ! {request_body, Ref, fin, BodyLen, Data}, ok. -%% We use ~999999p here instead of ~w because the latter doesn't -%% support printable strings. -report_crash(_, _, _, normal, _) -> - ok; -report_crash(_, _, _, shutdown, _) -> - ok; -report_crash(_, _, _, {shutdown, _}, _) -> - ok; -report_crash(Ref, StreamID, Pid, Reason, Stacktrace) -> - error_logger:error_msg( - "Ranch listener ~p, connection process ~p, stream ~p " - "had its request process ~p exit with reason " - "~999999p and stacktrace ~999999p~n", - [Ref, self(), StreamID, Pid, Reason, Stacktrace]). - %% Request process. %% We catch all exceptions in order to add the stacktrace to |