Skip to content

Commit

Permalink
Use Logger. Upgrade zotonic_stdlib. (#30)
Browse files Browse the repository at this point in the history
* Use Logger. Upgrade zotonic_stdlib.

* zotonic-stdlib 1.6

* Add node to the error log metadata
  • Loading branch information
mworrell authored Feb 2, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 290d5f2 commit 816d130
Showing 4 changed files with 41 additions and 33 deletions.
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
{plugins, [rebar3_hex]}.

{deps, [
{zotonic_stdlib, "1.5.9"},
{zotonic_stdlib, "~> 1.6"},
{cowboy, "2.9.0"}
]}.

14 changes: 8 additions & 6 deletions rebar.lock
Original file line number Diff line number Diff line change
@@ -3,21 +3,23 @@
{<<"cowlib">>,{pkg,<<"cowlib">>,<<"2.11.0">>},1},
{<<"ranch">>,{pkg,<<"ranch">>,<<"1.8.0">>},1},
{<<"ssl_verify_fun">>,{pkg,<<"ssl_verify_fun">>,<<"1.1.6">>},2},
{<<"tls_certificate_check">>,{pkg,<<"tls_certificate_check">>,<<"1.8.0">>},1},
{<<"zotonic_stdlib">>,{pkg,<<"zotonic_stdlib">>,<<"1.5.9">>},0}]}.
{<<"tls_certificate_check">>,
{pkg,<<"tls_certificate_check">>,<<"1.11.0">>},
1},
{<<"zotonic_stdlib">>,{pkg,<<"zotonic_stdlib">>,<<"1.6.0">>},0}]}.
[
{pkg_hash,[
{<<"cowboy">>, <<"865DD8B6607E14CF03282E10E934023A1BD8BE6F6BACF921A7E2A96D800CD452">>},
{<<"cowlib">>, <<"0B9FF9C346629256C42EBE1EEB769A83C6CB771A6EE5960BD110AB0B9B872063">>},
{<<"ranch">>, <<"8C7A100A139FD57F17327B6413E4167AC559FBC04CA7448E9BE9057311597A1D">>},
{<<"ssl_verify_fun">>, <<"CF344F5692C82D2CD7554F5EC8FD961548D4FD09E7D22F5B62482E5AEAEBD4B0">>},
{<<"tls_certificate_check">>, <<"8A41A435C8055CB11D1D0AC96B34F48377D49FEA2782D70EABCF8946539946A3">>},
{<<"zotonic_stdlib">>, <<"64BFCBBF42511E22BBFF254609C68E19D3458924EE7E357E03190C5CC29A204B">>}]},
{<<"tls_certificate_check">>, <<"609DCD503F31170F0799DAC380EB0E086388CF918FC769AAA60DDD6BBF575218">>},
{<<"zotonic_stdlib">>, <<"872E85BD2DC8A49A4DA255E4DF6C1B4DDC993E59024D02303BD6A5DF0868859B">>}]},
{pkg_hash_ext,[
{<<"cowboy">>, <<"2C729F934B4E1AA149AFF882F57C6372C15399A20D54F65C8D67BEF583021BDE">>},
{<<"cowlib">>, <<"2B3E9DA0B21C4565751A6D4901C20D1B4CC25CBB7FD50D91D2AB6DD287BC86A9">>},
{<<"ranch">>, <<"49FBCFD3682FAB1F5D109351B61257676DA1A2FDBE295904176D5E521A2DDFE5">>},
{<<"ssl_verify_fun">>, <<"BDB0D2471F453C88FF3908E7686F86F9BE327D065CC1EC16FA4540197EA04680">>},
{<<"tls_certificate_check">>, <<"5211FD87B4FEFAA77F2DE1F03479CBC4EE8312738A21B9B905253674468928F2">>},
{<<"zotonic_stdlib">>, <<"AEBFC5C41989FE76458C3739269021D144454B94915F18B9E7A6249CA2F7CF0F">>}]}
{<<"tls_certificate_check">>, <<"4AB962212EF7C87482619CB298E1FE06E047B57F0BD58CC417B3B299EB8D036E">>},
{<<"zotonic_stdlib">>, <<"9139167866615F226C3915B6082B317892E276F3A9C78C5A99CD497FA589444E">>}]}
].
2 changes: 1 addition & 1 deletion src/cowmachine.app.src
Original file line number Diff line number Diff line change
@@ -13,6 +13,6 @@
{use_sendfile, erlang}
]},
{maintainers, ["Zotonic Team"]},
{licenses, ["Apache 2.0"]},
{licenses, ["Apache-2.0"]},
{links, [{"GitHub", "https://github.com/zotonic/cowmachine"}]}
]}.
56 changes: 31 additions & 25 deletions src/cowmachine.erl
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@
%% Internal logging interface
-export([log/1, log/2]).

-include_lib("kernel/include/logger.hrl").
-include("cowmachine_state.hrl").
-include("cowmachine_log.hrl").

@@ -84,7 +85,7 @@ request_1(Controller, Req, Env, Options, Context) ->
catch
throw:{stop_request, 500, {Reason, Stacktrace}} when is_list(Stacktrace) ->
log(#{ at => ?AT, level => error, code => 500, text => "Stop request",
reason => Reason, stacktrace => Stacktrace}, Req),
reason => Reason, stack => Stacktrace}, Req),
handle_stop_request(500, Site, undefined, Req, Env, State, Context);
throw:{stop_request, 500, Reason} ->
log(#{ at => ?AT, level => error, code => 500, text => "Stop request", reason => Reason}, Req),
@@ -93,7 +94,7 @@ request_1(Controller, Req, Env, Options, Context) ->
handle_stop_request(ResponseCode, Site, {throw, Reason}, Req, Env, State, Context);
throw:{stop_request, 500}:Stacktrace ->
log(#{ at => ?AT, level => error, code => 500, text => "Stop request",
stacktrace => Stacktrace}, Req),
stack => Stacktrace}, Req),
handle_stop_request(500, Site, undefined, Req, Env, State, Context);
throw:{stop_request, ResponseCode} when is_integer(ResponseCode), ResponseCode >= 400, ResponseCode < 500 ->
handle_stop_request(ResponseCode, Site, undefined, Req, Env, State, Context);
@@ -108,12 +109,12 @@ request_1(Controller, Req, Env, Options, Context) ->
throw:Reason:Stacktrace ->
log(#{ at => ?AT, level => error, code => 500, text => "Unexpected throw",
class => throw, reason => Reason,
stacktrace => Stacktrace}, Req),
stack => Stacktrace}, Req),
handle_stop_request(500, Site, {throw, {Reason, Stacktrace}}, Req, Env, State, Context);
Class:Reason:Stacktrace ->
log(#{ at => ?AT, level => error, code => 500, text => "Unexpected exception",
class => Class, reason => Reason,
stacktrace => Stacktrace}, Req),
stack => Stacktrace}, Req),
{stop, cowboy_req:reply(500, Req)}
end.

@@ -132,19 +133,19 @@ handle_stop_request(ResponseCode, _Site, Reason, Req, Env, State, Context) ->
ContextRespCode = cowmachine_req:set_response_code(ResponseCode, ContextResult),
cowmachine_response:send_response(ContextRespCode)
catch
throw:{stop_request, Code, Reason} ->
throw:{stop_request, Code, CReason} ->
log(#{ at => ?AT, level => warning,
text => "Stop request",
code => Code,
reason => Reason }, Req),
reason => CReason }, Req),
{stop, cowboy_req:reply(Code, Req)};
Class:Reason:Stacktrace->
Class:CReason:Stacktrace->
log(#{ at => ?AT, level => warning,
text => "Unexpected exception",
code => 500,
class => Class,
reason => Reason,
stacktrace => Stacktrace
reason => CReason,
stack => Stacktrace
}, Req),
{stop, cowboy_req:reply(500, Req)}
end.
@@ -155,7 +156,10 @@ handle_stop_request(ResponseCode, _Site, Reason, Req, Env, State, Context) ->
%%

log(#{ level := Level } = Report) ->
log_report(Level, Report#{in => cowmachine}).
log_report(Level, Report#{
in => cowmachine,
node => node()
}).

log(#{ level := Level } = Report, Req) when is_map(Req) ->
Report1 = lists:foldl(fun({Key, Fun}, Acc) ->
@@ -165,23 +169,25 @@ log(#{ level := Level } = Report, Req) when is_map(Req) ->
end
end, Report, [{src, fun src/1},
{dst, fun dst/1},
{path, fun path/1}]),
log_report(Level, Report1#{in => cowmachine}).

log_report(debug, _Report) ->
%% Ignore for now - re-enable for logger
ok;
log_report(Level, Report) when is_map(Report) ->
%% @todo also implement error logging for erlang 21 and higher.
Function = case Level of
error -> error_report;
warning -> warning_report;
info -> info_report
end,
error_logger:Function(maps:to_list(Report)).
{path, fun path/1}]),
log_report(Level, Report1#{
in => cowmachine,
node => node()
}).

log_report(debug, Report) when is_map(Report) ->
?LOG_DEBUG(Report);
log_report(info, Report) when is_map(Report) ->
?LOG_INFO(Report);
log_report(notice, Report) when is_map(Report) ->
?LOG_NOTICE(Report);
log_report(warning, Report) when is_map(Report) ->
?LOG_WARNING(Report);
log_report(error, Report) when is_map(Report) ->
?LOG_ERROR(Report).

src(#{ peer := {IP, Port} }) -> {ok, ip_info(IP, Port)};
src(_) -> undefined.
src(_) -> undefined.

dst(#{ sock := {IP, Port} } ) -> {ok, ip_info(IP, Port)};
dst(#{ port := Port }) -> {ok, #{ port => Port }};

0 comments on commit 816d130

Please sign in to comment.