Last active
January 13, 2018 10:03
-
-
Save tel/7d60be60f85dc45319ba to your computer and use it in GitHub Desktop.
Pipes-based logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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