packages feed

co-log-simple-1.2.0: src/lib/Colog/Simple.hs

{-|
This module is for creating 'LogAction' instances that use the logging and
formatting functions in this library.

A simple example:

@
import Colog.Monad (usingLoggerT)
import Colog.Simple (Severity (..), (%+), logger, logNotice, logTextStdout,
  msg, sevc, time)

usingLoggerT (logger (time %+ sevc %+ msg) logTextStdout Debug)
  $ logNotice "Take note of this!"
@

The 'LogAction' produced by 'logger' above is what you would put in your
ReaderT environment as well. See co-log documentation for more info on
integrating with transformers.

<https://github.com/co-log/co-log/blob/main/tutorials/custom/Custom.md>
-}
module Colog.Simple
  (
    -- * Constructing LogAction loggers
    logger
  , loggingDisabled

    -- * Utility functions
  , logTest

    -- * Re-exported from co-log-simple
  , module Colog.Simple.Message
  , module Colog.Simple.Severity

    -- * Re-exported from co-log
  , HasLog (..)
  , LogAction
  , logTextStderr, logTextStdout
  )
  where

import Colog (LogAction)
import Colog.Actions (logTextStderr, logTextStdout)
import Colog.Core.Action (cmapM)
import Colog.Message (defaultFieldMap, upgradeMessageAction)
import Colog.Monad (HasLog (..), WithLog)
import Control.Monad.Trans (MonadIO)
import Data.Text (Text)

import Colog.Simple.Message
import Colog.Simple.Severity


-- | Construct a logging action
logger :: MonadIO m
  => (RichMessage m -> m Text)  -- ^ Format function
  -> LogAction m Text   -- ^ Where log message output will go
  -> Severity           -- ^ The severity level to filter at or above
  -> LogAction m Message
logger fmtFs targetAction sev'
  = setSeverity sev'
  . upgradeMessageAction defaultFieldMap
  $ cmapM fmtFs targetAction


-- | Use this to completely disable logging
loggingDisabled :: Applicative m => LogAction m Message
loggingDisabled = mempty


-- | Test function to generate messages with every severity in this library
logTest :: WithLog env Message m => m ()
logTest = do
  logDebug "log test message Debug 1 of 5"
  logInfo "log test message Info 2 of 5"
  logNotice "log test message Notice 3 of 5"
  logWarning "log test message Warning 4 of 5"
  logError "log test message Error 5 of 5"