Skip to content

Commit 816d130

Browse files
authored
Use Logger. Upgrade zotonic_stdlib. (#30)
* Use Logger. Upgrade zotonic_stdlib. * zotonic-stdlib 1.6 * Add node to the error log metadata
1 parent 290d5f2 commit 816d130

File tree

4 files changed

+41
-33
lines changed

4 files changed

+41
-33
lines changed

rebar.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{plugins, [rebar3_hex]}.
66

77
{deps, [
8-
{zotonic_stdlib, "1.5.9"},
8+
{zotonic_stdlib, "~> 1.6"},
99
{cowboy, "2.9.0"}
1010
]}.
1111

rebar.lock

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@
33
{<<"cowlib">>,{pkg,<<"cowlib">>,<<"2.11.0">>},1},
44
{<<"ranch">>,{pkg,<<"ranch">>,<<"1.8.0">>},1},
55
{<<"ssl_verify_fun">>,{pkg,<<"ssl_verify_fun">>,<<"1.1.6">>},2},
6-
{<<"tls_certificate_check">>,{pkg,<<"tls_certificate_check">>,<<"1.8.0">>},1},
7-
{<<"zotonic_stdlib">>,{pkg,<<"zotonic_stdlib">>,<<"1.5.9">>},0}]}.
6+
{<<"tls_certificate_check">>,
7+
{pkg,<<"tls_certificate_check">>,<<"1.11.0">>},
8+
1},
9+
{<<"zotonic_stdlib">>,{pkg,<<"zotonic_stdlib">>,<<"1.6.0">>},0}]}.
810
[
911
{pkg_hash,[
1012
{<<"cowboy">>, <<"865DD8B6607E14CF03282E10E934023A1BD8BE6F6BACF921A7E2A96D800CD452">>},
1113
{<<"cowlib">>, <<"0B9FF9C346629256C42EBE1EEB769A83C6CB771A6EE5960BD110AB0B9B872063">>},
1214
{<<"ranch">>, <<"8C7A100A139FD57F17327B6413E4167AC559FBC04CA7448E9BE9057311597A1D">>},
1315
{<<"ssl_verify_fun">>, <<"CF344F5692C82D2CD7554F5EC8FD961548D4FD09E7D22F5B62482E5AEAEBD4B0">>},
14-
{<<"tls_certificate_check">>, <<"8A41A435C8055CB11D1D0AC96B34F48377D49FEA2782D70EABCF8946539946A3">>},
15-
{<<"zotonic_stdlib">>, <<"64BFCBBF42511E22BBFF254609C68E19D3458924EE7E357E03190C5CC29A204B">>}]},
16+
{<<"tls_certificate_check">>, <<"609DCD503F31170F0799DAC380EB0E086388CF918FC769AAA60DDD6BBF575218">>},
17+
{<<"zotonic_stdlib">>, <<"872E85BD2DC8A49A4DA255E4DF6C1B4DDC993E59024D02303BD6A5DF0868859B">>}]},
1618
{pkg_hash_ext,[
1719
{<<"cowboy">>, <<"2C729F934B4E1AA149AFF882F57C6372C15399A20D54F65C8D67BEF583021BDE">>},
1820
{<<"cowlib">>, <<"2B3E9DA0B21C4565751A6D4901C20D1B4CC25CBB7FD50D91D2AB6DD287BC86A9">>},
1921
{<<"ranch">>, <<"49FBCFD3682FAB1F5D109351B61257676DA1A2FDBE295904176D5E521A2DDFE5">>},
2022
{<<"ssl_verify_fun">>, <<"BDB0D2471F453C88FF3908E7686F86F9BE327D065CC1EC16FA4540197EA04680">>},
21-
{<<"tls_certificate_check">>, <<"5211FD87B4FEFAA77F2DE1F03479CBC4EE8312738A21B9B905253674468928F2">>},
22-
{<<"zotonic_stdlib">>, <<"AEBFC5C41989FE76458C3739269021D144454B94915F18B9E7A6249CA2F7CF0F">>}]}
23+
{<<"tls_certificate_check">>, <<"4AB962212EF7C87482619CB298E1FE06E047B57F0BD58CC417B3B299EB8D036E">>},
24+
{<<"zotonic_stdlib">>, <<"9139167866615F226C3915B6082B317892E276F3A9C78C5A99CD497FA589444E">>}]}
2325
].

src/cowmachine.app.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
{use_sendfile, erlang}
1414
]},
1515
{maintainers, ["Zotonic Team"]},
16-
{licenses, ["Apache 2.0"]},
16+
{licenses, ["Apache-2.0"]},
1717
{links, [{"GitHub", "https://github.com/zotonic/cowmachine"}]}
1818
]}.

src/cowmachine.erl

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
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

157158
log(#{ level := Level } = Report) ->
158-
log_report(Level, Report#{in => cowmachine}).
159+
log_report(Level, Report#{
160+
in => cowmachine,
161+
node => node()
162+
}).
159163

160164
log(#{ 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

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

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

0 commit comments

Comments
 (0)