aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_stream.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2018-06-28 17:10:18 +0200
committerLoïc Hoguin <[email protected]>2018-06-28 17:10:18 +0200
commita76c32db5e32ab6a6b35ce6625cb3aedcac067b6 (patch)
tree745b39ea422a946108cb406d1fbcd3ece678ec39 /src/cowboy_stream.erl
parent16143354361651cc7c14b81595760118efce8870 (diff)
downloadcowboy-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.erl')
-rw-r--r--src/cowboy_stream.erl43
1 files changed, 20 insertions, 23 deletions
diff --git a/src/cowboy_stream.erl b/src/cowboy_stream.erl
index 1a1031e..6776863 100644
--- a/src/cowboy_stream.erl
+++ b/src/cowboy_stream.erl
@@ -78,7 +78,7 @@
-export([info/3]).
-export([terminate/3]).
-export([early_error/5]).
--export([report_error/5]).
+-export([make_error_log/5]).
%% Note that this and other functions in this module do NOT catch
%% exceptions. We want the exception to go all the way down to the
@@ -148,47 +148,44 @@ early_error(StreamID, Reason, PartialReq, Resp, Opts) ->
PartialReq, Resp, Opts#{stream_handlers => Tail})
end.
--spec report_error(atom(), list(), error | exit | throw, any(), list()) -> ok.
-report_error(init, [StreamID, Req, Opts], Class, Exception, Stacktrace) ->
- error_logger:error_msg(
+-spec make_error_log(init | data | info | terminate | early_error,
+ list(), error | exit | throw, any(), list())
+ -> {log, error, string(), list()}.
+make_error_log(init, [StreamID, Req, Opts], Class, Exception, Stacktrace) ->
+ {log, error,
"Unhandled exception ~p:~p in cowboy_stream:init(~p, Req, Opts)~n"
"Stacktrace: ~p~n"
"Req: ~p~n"
"Opts: ~p~n",
- [Class, Exception, StreamID, Stacktrace, Req, Opts]);
-report_error(data, [StreamID, IsFin, Data, State], Class, Exception, Stacktrace) ->
- error_logger:error_msg(
+ [Class, Exception, StreamID, Stacktrace, Req, Opts]};
+make_error_log(data, [StreamID, IsFin, Data, State], Class, Exception, Stacktrace) ->
+ {log, error,
"Unhandled exception ~p:~p in cowboy_stream:data(~p, ~p, Data, State)~n"
"Stacktrace: ~p~n"
"Data: ~p~n"
"State: ~p~n",
- [Class, Exception, StreamID, IsFin, Stacktrace, Data, State]);
-report_error(info, [StreamID, Msg, State], Class, Exception, Stacktrace) ->
- error_logger:error_msg(
+ [Class, Exception, StreamID, IsFin, Stacktrace, Data, State]};
+make_error_log(info, [StreamID, Msg, State], Class, Exception, Stacktrace) ->
+ {log, error,
"Unhandled exception ~p:~p in cowboy_stream:info(~p, Msg, State)~n"
"Stacktrace: ~p~n"
"Msg: ~p~n"
"State: ~p~n",
- [Class, Exception, StreamID, Stacktrace, Msg, State]);
-report_error(terminate, [StreamID, Reason, State], Class, Exception, Stacktrace) ->
- error_logger:error_msg(
+ [Class, Exception, StreamID, Stacktrace, Msg, State]};
+make_error_log(terminate, [StreamID, Reason, State], Class, Exception, Stacktrace) ->
+ {log, error,
"Unhandled exception ~p:~p in cowboy_stream:terminate(~p, Reason, State)~n"
"Stacktrace: ~p~n"
"Reason: ~p~n"
"State: ~p~n",
- [Class, Exception, StreamID, Stacktrace, Reason, State]);
-report_error(early_error, [StreamID, Reason, PartialReq, Resp, Opts], Class, Exception, Stacktrace) ->
- error_logger:error_msg(
+ [Class, Exception, StreamID, Stacktrace, Reason, State]};
+make_error_log(early_error, [StreamID, Reason, PartialReq, Resp, Opts],
+ Class, Exception, Stacktrace) ->
+ {log, error,
"Unhandled exception ~p:~p in cowboy_stream:early_error(~p, Reason, PartialReq, Resp, Opts)~n"
"Stacktrace: ~p~n"
"Reason: ~p~n"
"PartialReq: ~p~n"
"Resp: ~p~n"
"Opts: ~p~n",
- [Class, Exception, StreamID, Stacktrace, Reason, PartialReq, Resp, Opts]);
-report_error(Callback, _, Class, Reason, Stacktrace) ->
- error_logger:error_msg(
- "Exception occurred in unknown callback ~p~n"
- "Reason: ~p:~p~n"
- "Stacktrace: ~p~n",
- [Callback, Class, Reason, Stacktrace]).
+ [Class, Exception, StreamID, Stacktrace, Reason, PartialReq, Resp, Opts]}.