Skip to content

Instantly share code, notes, and snippets.

@tel
Last active January 13, 2018 10:03
Show Gist options
  • Save tel/7d60be60f85dc45319ba to your computer and use it in GitHub Desktop.
Save tel/7d60be60f85dc45319ba to your computer and use it in GitHub Desktop.
Pipes-based logger
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module PipeLog where
import Control.Applicative
import Data.Functor.Identity
import Pipes
import qualified Pipes.Prelude as P
newtype LogT l m a =
LogT { runLog :: Producer l m a }
deriving ( Functor, Applicative, Monad, MonadTrans, MonadIO, MFunctor )
type Log l a = LogT l Identity a
say :: Monad m => l -> LogT l m ()
say = LogT . yield
-- Not the recommended way to perform logging.
produceLog :: Monad m => LogT l m () -> m [l]
produceLog = P.toListM . runLog
runLog :: Monad m
=> (l -> m ()) -- the logging function (consider m ~ IO)
-> LogT l m a
-> m a
runLog log (Log p) = runEffect (for p (lift . log))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment