Write your own logger

To illustrate how to write a module that handles auth and access logging in Yaws, we will get a concrete example: How to log the original client IP behind a HTTP reverse proxy.

When Yaws is running behind a HTTP reverse proxy, the source IP of a connection is always the proxy's IP address. It is also this IP address that you will find in your log files. In this situation, it may be desirable to log the real client IP address by extracting it from the "X-Forwarded-For" header.

It can easily be done by writting a simple module to handle access and auth logging on top of yaws_log. yaws_log is the default logger of Yaws and it can be overridden by using the directive "logger_mod" in yaws.conf:

<server www.hyber.org>
        port       = 80
        listen     = 0.0.0.0
        logger_mod = simple_logger
        ...
</server>

     

In this example, "simple_logger" will be used in place of yaws_log. This module must implement the behaviour yaws_logger by exporting the following functions:

Module:open_log(ServerName, Type, LogDir) -> {true, State} | false.
    ServerName :: string()
    Type       :: access | auth
    LogDir     :: string()
    State      :: term()


Module:close_log(ServerName, Type, State) -> ok.
    ServerName :: string()
    Type       :: access | auth
    State      :: term()


Module:wrap_log(ServerName, Type, State, LogWrapSize) -> NewState.
    ServerName      :: string()
    Type            :: access | auth
    State, NewState :: term()


Module:write_log(ServerName, Type, State, Infos) -> ok.
    ServerName :: string()
    Type       :: access | auth
    State      :: term()
    Infos      :: {Ip,Req,InHdrs,OutHdrs,Time} | %% when type =:= access
                  {Ip,Path,Item}                 %% when type =:= auth

        Ip      :: inet:ip_address() | inet:hostname() | unknown
        Req     :: #http_request{}                 %% defined in yaws_api.hrl
        InHdrs  :: #headers{}                      %% defined in yaws_api.hrl
        OutHdrs :: #outh{}                         %% defined in yaws.hrl
        Time    :: non_neg_integer()               %% The time taken to serve the request, in microseconds
        Path    :: string()                        %% the URI path of the request
        Item    :: {ok, User} | 403 | {401, Realm} %% the result of an auth request

     

Back to our example. The only thing we want to do is read the "X-Forwarded-For" header to extract the real client IP address. The logging will be delegated to yaws_log.

If the "X-Forwarded-For" header is defined, then the originating IP address of a client is the left-most IP address. But, because this header can be falsified, we will define a list of trustworthy proxies. For simplicity, we will use a macro. Here is our logger:

-module(yaws_revproxy_logger).

-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]).

-define(REVPROXY_WHITELIST, [{192,168,0,1}, {192,168,0,2}]).

%% ===================================================================
open_log(ServerName, Type, Dir) ->
    yaws_log:open_log(ServerName, Type, Dir).


close_log(ServerName, Type, State) ->
    yaws_log:close_log(ServerName, Type, State).


wrap_log(ServerName, Type, Data, LogWrapSize) ->
    yaws_log:wrap_log(ServerName, Type, Data, LogWrapSize).


write_log(ServerName, auth, State, {Ip, Path, Item}) ->
    yaws_log:write_log(ServerName, auth, State, {Ip, Path, Item});
write_log(ServerName, access, State, {Ip, Req, InH, OutH, Time}) ->
    RealIp = real_client_ip(Ip, ?REVPROXY_WHITELIST, InH),
    yaws_log:write_log(ServerName, access, State, {RealIp, Req, InH, OutH, Time}).


real_client_ip(Ip, ProxyWhitelist, Hdrs) ->
    case lists:member(Ip, ProxyWhitelist) of
        true ->
            FwdFor = Hdrs#headers.x_forwarded_for,
            case yaws:split_sep(FwdFor, $,) of
                [FirstIp|_Proxies] ->
                    %% We might check if the last proxy is the remote
                    %% address of the request, i.e hd(_Proxies) =:= Ip.
                    case inet_parse:address(FirstIp) of
                        {error, _}     -> unknown;
                        {ok, ClientIp} -> ClientIp
                    end;
                [] ->
                    Ip
            end;
        false ->
            Ip
    end.

     

Now, we just need to compile this module and update yaws.conf accordingly.

Note: In previous version of Yaws, we could use "x_forwarded_for_log_proxy_whitelist" to do the same thing. This parameter is now deprecated.

Valid XHTML 1.0!