Skip to content

Commit

Permalink
add straightforward logging hook to cowboy
Browse files Browse the repository at this point in the history
  • Loading branch information
stolen committed Jun 28, 2014
1 parent e71870c commit f82c5f3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/erdico.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ start_cowboy() ->
Host = {'_', [DefPath]}, % No virtualhosts
Dispatch = cowboy_router:compile([Host]),
Env = [{env, [{dispatch, Dispatch}]}],
cowboy:start_http(?MODULE, 10, [{port, conf(port, 2080)}], Env).
Hooks = [{onresponse, fun erdico_log:access_log_hook/4}],
cowboy:start_http(?MODULE, 10, [{port, conf(port, 2080)}], Env ++ Hooks).

conf(Key, Default) ->
application:get_env(?MODULE, Key, Default).
11 changes: 11 additions & 0 deletions src/erdico_log.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-module(erdico_log).
-export([access_log_hook/4]).

access_log_hook(Status, _Headers, _Body, Req) ->
{{PeerAddr, _}, Req1} = cowboy_req:peer(Req),
{Method, Req2} = cowboy_req:method(Req1),
{Url, Req3} = cowboy_req:url(Req2),
PeerStr = inet_parse:ntoa(PeerAddr),
lager:info([{tag, access}, {peer, PeerStr}, {method, Method}, {url, Url}, {status, Status}], ""),
Req3.

0 comments on commit f82c5f3

Please sign in to comment.