Apache-style access log for your cowboy application
Go to file
Sergey Yelin 584196154b
Fix access log crash on requiest without body (#14)
* Fix access log crash on requiest without  body

* Add comment with issue
2019-11-21 16:17:49 +03:00
src Fix access log crash on requiest without body (#14) 2019-11-21 16:17:49 +03:00
.gitignore MSPF-320: Introduce access lib (#1) 2017-12-05 17:59:06 +03:00
elvis.config OTP 21 Logger (#8) 2019-04-25 18:25:24 +03:00
LICENSE Prepare for opensource (#9) 2019-07-03 12:28:34 +03:00
Makefile Prepare for opensource (#9) 2019-07-03 12:28:34 +03:00
README.md Prepare for opensource (#9) 2019-07-03 12:28:34 +03:00
rebar.config Prepare for opensource (#9) 2019-07-03 12:28:34 +03:00
rebar.lock OTP 21 Logger (#8) 2019-04-25 18:25:24 +03:00
wercker.yml Force use cache for dialyzer (#13) 2019-08-06 18:37:56 +03:00

Cowboy access log

wercker status

Description

This app implements apache-styled access logging for cowboy >= 2.0.
It works as a stream handler and logs every request received by server via Logger.
Following information is being logged:

  • status - HTTP status code
  • remote_addr - IP address of request origin if it was forwarded
  • peer_addr - IP address of request origin
  • request_method - Request method
  • request_path - Request path
  • request_length - Request length
  • response_length - Responce length
  • request_time - Time taken by requset proccessing in µs
  • http_x-request-id - Unique request id

cowboy_access_log also supports custom meta, that can be added by calling set_extra_info_fun/2 on your Cowboy options.
extra_info_fun must take Req as it's only argument and return #{atom() => term()}.

Usage

To use this stream handler just put it in stream handler chain when configuring cowboy server. All log events, created by the handler are being taged with cowboy_access_log domain, so they can be easily filtered with built-in functions. Check examples for better understanding.

Examples

Add handler

cowboy:start_clear(http, [{port, 8080}], #{
    stream_handlers => [cowboy_access_log, cowboy_stream_h],
    env => #{dispatch => Dispatch}
}).

Filter only cowboy_access_log events

add_handler_filter(
    HandlerId,
    FilterId,
    {fun logger_filters:domain/2, {stop, not_equal, [cowboy_access_log]}}
).

Add function to tag each log event with random Id

CowboyOpts0 = #{
    stream_handlers => [cowboy_access_log, cowboy_stream_h],
    env => #{dispatch => Dispatch}
},
CowboyOpts = cowboy_access_log_h:set_extra_info_fun(fun(_Req) -> #{my_random_id => base64:encode(crypto:strong_rand_bytes(20))} end, CowboyOpts0),
cowboy:start_clear(http, [{port, 8080}], CowboyOpts).