public
Created

simple logging module for yaws that uses lager

  • Download Gist
yaws_lager_log.erl
Erlang
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
-module(yaws_lager_log).
-compile([{parse_transform, lager_transform}]).
 
-behaviour(yaws_logger).
 
-include_lib("yaws/include/yaws.hrl").
-include_lib("yaws/include/yaws_api.hrl").
 
-export([open_log/3, close_log/3, wrap_log/4, write_log/4]).
 
%% ===================================================================
open_log(_ServerName, _Type, _Dir) ->
%% Do nothing return an empty state
{true, []}.
 
close_log(_ServerName, _Type, _State) ->
%% Nothing to do here
ok.
 
wrap_log(_ServerName, _Type, _Data, _LogWrapSize) ->
%% lager_file_backend already handle log rotation
ok.
 
write_log(_ServerName, auth, _State, _Info) ->
%% Skip auth log
ok;
write_log(ServerName, access, _State, {Ip, Req, InH, OutH, Time}) ->
Status = case OutH#outh.status of
undefined -> "-";
S -> integer_to_list(S)
end,
Len = case Req#http_request.method of
'HEAD' ->
"-";
_ ->
case OutH#outh.contlen of
undefined ->
case OutH#outh.act_contlen of
undefined -> "-";
L -> integer_to_list(L)
end;
L ->
integer_to_list(L)
end
end,
Ver = case Req#http_request.version of
{1,0} -> "HTTP/1.0";
{1,1} -> "HTTP/1.1";
{0,9} -> "HTTP/0.9"
end,
Path = yaws_server:safe_decode_path(Req#http_request.path),
Meth = yaws:to_list(Req#http_request.method),
Referer = optional_header(InH#headers.referer),
UserAgent = optional_header(InH#headers.user_agent),
User = case InH#headers.authorization of
{U, _P, _OStr} -> U;
_ -> "-"
end,
 
lager:notice("~s - ~s \"~s\" ~s ~s \"~s\" \"~s\" ~p ~s",
[fmt_ip(Ip), User, no_ctl([Meth, $\s, Path, $\s, Ver]), Status,
Len, Referer, UserAgent, (Time div 1000), ServerName]),
ok.
 
 
%% ===================================================================
optional_header(Item) ->
case Item of
undefined -> "-";
Item -> Item
end.
 
 
no_ctl([H|T]) when H < 32 ->
no_ctl(T);
no_ctl([H|T]) ->
[H|no_ctl(T)];
no_ctl([]) ->
[].
 
fmt_ip(IP) when is_tuple(IP) ->
inet_parse:ntoa(IP);
fmt_ip(undefined) ->
"0.0.0.0";
fmt_ip(HostName) ->
HostName.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.