content: {-
     EMS - REST API
  
     This section will provide necessary information about the `CoinAPI EMS REST API` protocol. This API is also available in the Postman application: <a href=\"https://postman.coinapi.io/\" target=\"_blank\">https://postman.coinapi.io/</a>        Implemented Standards:    * [HTTP1.0](https://datatracker.ietf.org/doc/html/rfc1945)   * [HTTP1.1](https://datatracker.ietf.org/doc/html/rfc2616)   * [HTTP2.0](https://datatracker.ietf.org/doc/html/rfc7540)     ### Endpoints  <table>   <thead>     <tr>       <th>Deployment method</th>       <th>Environment</th>       <th>Url</th>     </tr>   </thead>   <tbody>     <tr>       <td>Managed Cloud</td>       <td>Production</td>       <td>Use <a href=\"#ems-docs-sh\">Managed Cloud REST API /v1/locations</a> to get specific endpoints to each server site where your deployments span</td>     </tr>     <tr>       <td>Self Hosted</td>       <td>Production</td>       <td>IP Address of the <code>ems-gateway</code> container/excecutable in the closest server site to the caller location</td>     </tr>   </tbody> </table>  ### Authentication If the software is deployed as `Self-Hosted` then API do not require authentication as inside your infrastructure, your company is responsible for the security and access controls.  If the software is deployed in our `Managed Cloud`, there are 2 methods for authenticating with us, you only need to use one:   1. Custom authorization header named `X-CoinAPI-Key` with the API Key  2. Query string parameter named `apikey` with the API Key  3. <a href=\"#certificate\">TLS Client Certificate</a> from the `Managed Cloud REST API` (/v1/certificate/pem endpoint) while establishing a TLS session with us.  #### Custom authorization header You can authorize by providing additional custom header named `X-CoinAPI-Key` and API key as its value. Assuming that your API key is `73034021-THIS-IS-SAMPLE-KEY`, then the authorization header you should send to us will look like: `X-CoinAPI-Key: 73034021-THIS-IS-SAMPLE-KEY` <aside class=\"success\">This method is recommended by us and you should use it in production environments.</aside> #### Query string authorization parameter You can authorize by providing an additional parameter named `apikey` with a value equal to your API key in the query string of your HTTP request. Assuming that your API key is `73034021-THIS-IS-SAMPLE-KEY` and that you want to request all balances, then your query string should look like this: `GET /v1/balances?apikey=73034021-THIS-IS-SAMPLE-KEY` <aside class=\"notice\">Query string method may be more practical for development activities.</aside> 
  
     OpenAPI Version: 3.0.0
     EMS - REST API API version: v1
     Contact: support@coinapi.io
     Generated by OpenAPI Generator (https://openapi-generator.tech)
  -}
  
  {-|
  Module : EMS-REST.LoggingKatip
  Katip Logging functions
  -}
  
  {-# LANGUAGE OverloadedStrings #-}
  {-# LANGUAGE RankNTypes #-}
  {-# LANGUAGE ScopedTypeVariables #-}
  
  module EMS-REST.LoggingKatip where
  
  import qualified Control.Exception.Safe as E
  import qualified Control.Monad.IO.Class as P
  import qualified Control.Monad.Trans.Reader as P
  import qualified Data.Text as T
  import qualified Lens.Micro as L
  import qualified System.IO as IO
  
  import Data.Text (Text)
  import GHC.Exts (IsString(..))
  
  import qualified Katip as LG
  
  -- * Type Aliases (for compatibility)
  
  -- | Runs a Katip logging block with the Log environment
  type LogExecWithContext = forall m a. P.MonadIO m =>
                                        LogContext -> LogExec m a
  
  -- | A Katip logging block
  type LogExec m a = LG.KatipT m a -> m a
  
  -- | A Katip Log environment
  type LogContext = LG.LogEnv
  
  -- | A Katip Log severity
  type LogLevel = LG.Severity
  
  -- * default logger
  
  -- | the default log environment
  initLogContext :: IO LogContext
  initLogContext = LG.initLogEnv "EMS-REST" "dev"
  
  -- | Runs a Katip logging block with the Log environment
  runDefaultLogExecWithContext :: LogExecWithContext
  runDefaultLogExecWithContext = LG.runKatipT
  
  -- * stdout logger
  
  -- | Runs a Katip logging block with the Log environment
  stdoutLoggingExec :: LogExecWithContext
  stdoutLoggingExec = runDefaultLogExecWithContext
  
  -- | A Katip Log environment which targets stdout
  stdoutLoggingContext :: LogContext -> IO LogContext
  stdoutLoggingContext cxt = do
      handleScribe <- LG.mkHandleScribe LG.ColorIfTerminal IO.stdout (LG.permitItem LG.InfoS) LG.V2
      LG.registerScribe "stdout" handleScribe LG.defaultScribeSettings cxt
  
  -- * stderr logger
  
  -- | Runs a Katip logging block with the Log environment
  stderrLoggingExec :: LogExecWithContext
  stderrLoggingExec = runDefaultLogExecWithContext
  
  -- | A Katip Log environment which targets stderr
  stderrLoggingContext :: LogContext -> IO LogContext
  stderrLoggingContext cxt = do
      handleScribe <- LG.mkHandleScribe LG.ColorIfTerminal IO.stderr (LG.permitItem LG.InfoS) LG.V2
      LG.registerScribe "stderr" handleScribe LG.defaultScribeSettings cxt
  
  -- * Null logger
  
  -- | Disables Katip logging
  runNullLogExec :: LogExecWithContext
  runNullLogExec le (LG.KatipT f) = P.runReaderT f (L.set LG.logEnvScribes mempty le)
  
  -- * Log Msg
  
  -- | Log a katip message
  _log :: (Applicative m, LG.Katip m) => Text -> LogLevel -> Text -> m ()
  _log src level msg = do
    LG.logMsg (fromString $ T.unpack src) level (LG.logStr msg)
  
  -- * Log Exceptions
  
  -- | re-throws exceptions after logging them
  logExceptions
    :: (LG.Katip m, E.MonadCatch m, Applicative m)
    => Text -> m a -> m a
  logExceptions src =
    E.handle
      (\(e :: E.SomeException) -> do
         _log src LG.ErrorS ((T.pack . show) e)
         E.throw e)
  
  -- * Log Level
  
  levelInfo :: LogLevel
  levelInfo = LG.InfoS
  
  levelError :: LogLevel
  levelError = LG.ErrorS
  
  levelDebug :: LogLevel
  levelDebug = LG.DebugS
  