Created
April 27, 2012 14:38
-
-
Save capflam/2509769 to your computer and use it in GitHub Desktop.
simple logging module for yaws that uses lager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment