vikunja-api-0.24.6.0: lib/Vikunja/LoggingKatip.hs
{-
Vikunja API
# Pagination Every endpoint capable of pagination will return two headers: * `x-pagination-total-pages`: The total number of available pages for this request * `x-pagination-result-count`: The number of items returned for this request. # Rights All endpoints which return a single item (project, task, etc.) - no array - will also return a `x-max-right` header with the max right the user has on this item as an int where `0` is `Read Only`, `1` is `Read & Write` and `2` is `Admin`. This can be used to show or hide ui elements based on the rights the user has. # Errors All errors have an error code and a human-readable error message in addition to the http status code. You should always check for the status code in the response, not only the http status code. Due to limitations in the swagger library we're using for this document, only one error per http status code is documented here. Make sure to check the [error docs](https://vikunja.io/docs/errors/) in Vikunja's documentation for a full list of available error codes. # Authorization **JWT-Auth:** Main authorization method, used for most of the requests. Needs `Authorization: Bearer <jwt-token>`-header to authenticate successfully. **API Token:** You can create scoped API tokens for your user and use the token to make authenticated requests in the context of that user. The token must be provided via an `Authorization: Bearer <token>` header, similar to jwt auth. See the documentation for the `api` group to manage token creation and revocation. **BasicAuth:** Only used when requesting tasks via CalDAV. <!-- ReDoc-Inject: <security-definitions> -->
OpenAPI Version: 3.0.1
Vikunja API API version: 0.24.6
Contact: hello@vikunja.io
Generated by OpenAPI Generator (https://openapi-generator.tech)
-}
{-|
Module : Vikunja.LoggingKatip
Katip Logging functions
-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Vikunja.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 "Vikunja" "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