mirror of
https://github.com/valitydev/cowboy_access_log.git
synced 2024-11-06 00:05:22 +00:00
Apache-style access log for your cowboy application
.github/workflows | ||
src | ||
.gitignore | ||
elvis.config | ||
LICENSE | ||
Makefile | ||
README.md | ||
rebar.config | ||
rebar.lock |
Cowboy access log
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 coderemote_addr
- IP address of request origin if it was forwardedpeer_addr
- IP address of request originrequest_method
- Request methodrequest_path
- Request pathrequest_length
- Request lengthresponse_length
- Responce lengthrequest_time
- Time taken by requset proccessing in µshttp_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).