module Network.Wai.Log.Options (
Options(..)
, ResponseTime(..)
, defaultOptions
, defaultLogRequest
, defaultLogResponse
, logRequestUUID
, requestUUID
) where
import Data.Aeson.Types (Pair, Value)
import Data.ByteString.Builder (Builder)
import Data.String.Conversions (ConvertibleStrings, StrictText, cs)
import Data.Text (Text)
import Data.Time.Clock (NominalDiffTime)
import Data.UUID (UUID)
import Log
import Network.HTTP.Types.Header (ResponseHeaders)
import Network.HTTP.Types.Status (Status, statusCode, statusMessage)
import Network.Wai
data Options = Options {
Options -> LogLevel
logLevel :: LogLevel
, Options -> UUID -> Request -> [Pair]
logRequest :: UUID -> Request -> [Pair]
, Options
-> UUID -> Request -> Response -> Value -> ResponseTime -> [Pair]
logResponse :: UUID -> Request -> Response -> Value -> ResponseTime -> [Pair]
, Options
-> Maybe
(Request -> Status -> ResponseHeaders -> Maybe (Builder -> Value))
logBody :: Maybe (Request -> Status -> ResponseHeaders -> Maybe (Builder -> Value))
}
data ResponseTime = ResponseTime {
ResponseTime -> NominalDiffTime
processing :: NominalDiffTime
, ResponseTime -> NominalDiffTime
full :: NominalDiffTime
}
defaultOptions :: Options
defaultOptions :: Options
defaultOptions = Options :: LogLevel
-> (UUID -> Request -> [Pair])
-> (UUID -> Request -> Response -> Value -> ResponseTime -> [Pair])
-> Maybe
(Request -> Status -> ResponseHeaders -> Maybe (Builder -> Value))
-> Options
Options
{ logLevel :: LogLevel
logLevel = LogLevel
LogInfo
, logRequest :: UUID -> Request -> [Pair]
logRequest = UUID -> Request -> [Pair]
defaultLogRequest
, logResponse :: UUID -> Request -> Response -> Value -> ResponseTime -> [Pair]
logResponse = UUID -> Request -> Response -> Value -> ResponseTime -> [Pair]
defaultLogResponse
, logBody :: Maybe
(Request -> Status -> ResponseHeaders -> Maybe (Builder -> Value))
logBody = Maybe
(Request -> Status -> ResponseHeaders -> Maybe (Builder -> Value))
forall a. Maybe a
Nothing
}
defaultLogRequest :: UUID -> Request -> [Pair]
defaultLogRequest :: UUID -> Request -> [Pair]
defaultLogRequest UUID
uuid Request
req =
[ Text
"request_uuid" Text -> UUID -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= UUID
uuid
, Text
"method" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Method -> Text
forall a. ConvertibleStrings a Text => a -> Text
ts (Request -> Method
requestMethod Request
req)
, Text
"url" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Method -> Text
forall a. ConvertibleStrings a Text => a -> Text
ts (Request -> Method
rawPathInfo Request
req)
, Text
"remote_host" Text -> String -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= SockAddr -> String
forall a. Show a => a -> String
show (Request -> SockAddr
remoteHost Request
req)
, Text
"user_agent" Text -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= (Method -> Text) -> Maybe Method -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Method -> Text
forall a. ConvertibleStrings a Text => a -> Text
ts (Request -> Maybe Method
requestHeaderUserAgent Request
req)
, Text
"body_length" Text -> String -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= RequestBodyLength -> String
forall a. Show a => a -> String
show (Request -> RequestBodyLength
requestBodyLength Request
req)
]
defaultLogResponse :: UUID -> Request -> Response -> Value -> ResponseTime -> [Pair]
defaultLogResponse :: UUID -> Request -> Response -> Value -> ResponseTime -> [Pair]
defaultLogResponse UUID
uuid Request
req Response
resp Value
responseBody ResponseTime
time =
[ Text
"request_uuid" Text -> UUID -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= UUID
uuid
, Text
"method" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Method -> Text
forall a. ConvertibleStrings a Text => a -> Text
ts (Request -> Method
requestMethod Request
req)
, Text
"url" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Method -> Text
forall a. ConvertibleStrings a Text => a -> Text
ts (Request -> Method
rawPathInfo Request
req)
, Text
"response_body" Text -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Value
responseBody
, Text
"status" Text -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= [Pair] -> Value
object [ Text
"code" Text -> Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Status -> Int
statusCode (Response -> Status
responseStatus Response
resp)
, Text
"message" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Method -> Text
forall a. ConvertibleStrings a Text => a -> Text
ts (Status -> Method
statusMessage (Response -> Status
responseStatus Response
resp))
]
, Text
"time" Text -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= [Pair] -> Value
object [ Text
"full" Text -> NominalDiffTime -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= ResponseTime -> NominalDiffTime
full ResponseTime
time
, Text
"process" Text -> NominalDiffTime -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= ResponseTime -> NominalDiffTime
processing ResponseTime
time
]
]
logRequestUUID :: MonadLog m => UUID -> m a -> m a
logRequestUUID :: UUID -> m a -> m a
logRequestUUID = [Pair] -> m a -> m a
forall (m :: * -> *) a. MonadLog m => [Pair] -> m a -> m a
localData ([Pair] -> m a -> m a) -> (UUID -> [Pair]) -> UUID -> m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UUID -> [Pair]
requestUUID
requestUUID :: UUID -> [Pair]
requestUUID :: UUID -> [Pair]
requestUUID UUID
uuid =
[ Text
"request_uuid" Text -> UUID -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= UUID
uuid ]
ts :: ConvertibleStrings a StrictText => a -> Text
ts :: a -> Text
ts = a -> Text
forall a b. ConvertibleStrings a b => a -> b
cs