HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulekatip-0.8.8.0Haskell2010

Katip.Monadic

Provides support for treating payloads and namespaces as composable contexts. The common pattern would be to provide a KatipContext instance for your base monad.

  • 5 types
  • 1 class
  • 10 values
  • Packagekatip-0.8.8.0
  • Exports16
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceMonadic.hs

Monadic variants of logging functions from Katip.Core

5 declarations
valuelogFM
  1. :: (Applicative m, KatipContext m)
  2. => Severity

    Severity of the message

  3. -> LogStr

    The log message

  4. -> m ()
#

Log with full context, but without any code location. Automatically supplies payload and namespace.

valuelogTM :: ExpQ
#

Loc-tagged logging when using template-haskell. Automatically supplies payload and namespace.

$(logTM) InfoS "Hello world"

Loc-tagged logging when using getCallStack implicit-callstacks>. Automatically supplies payload and namespace.

Same consideration as logLoc applies.

By default, location will be logged from the module that invokes logLocM. If you want to use logLocM in a helper, wrap the entire helper in withFrozenCallStack to retain the callsite of the helper in the logs.

This function does not require template-haskell. Using GHC <= 7.8 will result in the emission of a log line without any location information. Users using GHC <= 7.8 may want to use the template-haskell function logTM for maximum compatibility.

logLocM InfoS "Hello world"

Machinery for merging typed log payloads/contexts

4 declarations
classclass Katip m => KatipContext (m :: Type -> Type) where
#

A monadic context that has an inherant way to get logging context and namespace. Examples include a web application monad or database monad. The local variants are just like local from Reader and indeed you can easily implement them with local if you happen to be using a Reader in your monad. These give us katipAddNamespace and katipAddContext that works with *any* KatipContext, as opposed to making users have to implement these functions on their own in each app.

Methods

Instances14KatipContext, …
datadata AnyLogContext where
#

A wrapper around a log context that erases type information so that contexts from multiple layers can be combined intelligently.

newtypenewtype LogContexts
#

Heterogeneous list of log contexts that provides a smart LogContext instance for combining multiple payload policies. This is critical for log contexts deep down in a stack to be able to inject their own context without worrying about other context that has already been set. Also note that contexts are treated as a sequence and <> will be appended to the right hand side of the sequence. If there are conflicting keys in the contexts, the /right side will take precedence/, which is counter to how monoid works for Map and HashMap, so bear that in mind. The reasoning is that if the user is sequentially adding contexts to the right side of the sequence, on conflict the intent is to overwrite with the newer value (i.e. the rightmost value).

Additional note: you should not mappend LogContexts in any sort of infinite loop, as it retains all data, so that would be a memory leak.

Instances5Semigroup, Monoid, ToJSON, LogItem, ToObject
valueliftPayload :: LogItem a => a -> LogContexts
#

Lift a log context into the generic wrapper so that it can combine with the existing log context.

KatipContextT - Utility transformer that provides Katip and KatipContext instances

7 declarations
newtypenewtype KatipContextT (m :: Type -> Type) a
#

Provides a simple transformer that defines a KatipContext instance for a fixed namespace and context. Just like KatipT, you should use this if you prefer an explicit transformer stack and don't want to (or cannot) define KatipContext for your monad . This is the slightly more powerful version of KatipT in that it provides KatipContext instead of just Katip. For instance:

  threadWithLogging = do
    le <- getLogEnv
    ctx <- getKatipContext
    ns <- getKatipNamespace
    forkIO $ runKatipContextT le ctx ns $ do
      $(logTM) InfoS "Look, I can log in IO and retain context!"
      doOtherStuff
Instances25MonadTrans, MonadTransControl, MonadError, MonadReader, MonadState, MonadWriter, …
valuekatipAddNamespace :: KatipContext m => Namespace -> m a -> m a
#

Append a namespace segment to the current namespace for the given monadic action, then restore the previous state afterwards. Works with anything implementing KatipContext.

valuekatipAddContext :: (LogItem i, KatipContext m) => i -> m a -> m a
#

Append some context to the current context for the given monadic action, then restore the previous state afterwards. Important note: be careful using this in a loop. If you're using something like forever or replicateM_ that does explicit sharing to avoid a memory leak, youll be fine as it will *sequence* calls to katipAddNamespace, so each loop will get the same context added. If you instead roll your own recursion and you're recursing in the action you provide, you'll instead accumulate tons of redundant contexts and even if they all merge on log, they are stored in a sequence and will leak memory. Works with anything implementing KatipContext.

newtypenewtype NoLoggingT (m :: Type -> Type) a
#

Constructors

Instances23MonadTrans, MonadTransControl, MonadError, MonadReader, MonadState, MonadWriter, …