# `TimeLog.TimePlug`

This plug stops the time a request takes. Also, this module delivers additional useful functionality
The plug needs to be called as early in the `endpoint.ex` as possible, ideally right after the socked received a request.
Additionally the get_duration function can be called from everywhere in order to know how long the request took so far.
Allows calls to get_ip from other apps too.

# `call`

```elixir
@spec call(Plug.Conn, Map) :: Plug.Conn
```

Call to TimePlug that initiates the duration measurement and schedules Response logging to take place before sending the response.

# `check_sanitize_fields`

# `check_sanitize_list`

# `create_http_map`

```elixir
@spec create_http_map(Plug.Conn) :: Map
```

create http map for logging

# `from_config`

# `from_config`

# `get_correlation_id`

```elixir
@spec get_correlation_id(Plug.Conn) :: String
```

Get the correlation_id.

# `get_duration`

```elixir
@spec get_duration(DateTime) :: Integer
@spec get_duration(Plug.Conn) :: Integer
```

Returns the time in ms the request took so far.
May be called with either a DateTime struct `DateTime` or with the current conn `Plug.Conn`.

## Examples
```
iex> TimeLog.TimePlug.get_duration(conn) # Request the duration with a `Plug.Conn` struct
65

iex> start_time = DateTime.utc_now
...
...
iex> TimeLog.TimePlug.get_duration(start_time) # Request the duration with a `DateTime` struct
65
```

# `get_ip`

```elixir
@spec get_ip(Plug.Conn) :: String
```

Returns the IP address in a Format fit for logging.
If set in config the IP addresses retourned are anonymized by replacing the last digits with 'XXX'.
Requests that use internal (e.g. company) Ip addresses can be filtered if set in config(`config :time_log, internal_ips: ["127.0.0.1"]`).

# `log_response`

```elixir
@spec log_response(Plug.Conn) :: Plug.Conn
```

Log the response including the IP address, correlation_id and headers.

# `sanitize`

# `sanitize_body`

# `sanitize_check`

# `sanitize_query_string`

# `sanitize_recursiv`

# `sanitize_value`

# `set_controller_body`

```elixir
@spec set_controller_body(Plug.Conn, Map) :: Plug.Conn
```

Sets the body to be logged in the conn in the private part for the key `:controller_body`.
The body will be included in the response logs if present.

## Example
In a REST Api function that is called to send the result JSON. The output paramter may be any `Map` or `Struct`.

```
defp send_response(conn, output) do
  conn
  |> TimeLog.TimePlug.set_controller_body(output)
  |> json(output)
end
```

---

*Consult [api-reference.md](api-reference.md) for complete listing*
