Skip to content

Pre/post processing in Effect handler #299

Answered by emlautarom1
emlautarom1 asked this question in Q&A
Discussion options

You must be logged in to vote

After some trial and error I ended up with:

runFileLogger :: (IOE :> es) => FilePath -> Eff (Logger e : es) a -> Eff es a
runFileLogger path logger = do
  counter <- liftIO $ newIORef (0 :: Int)
  withFile path WriteMode $ \handle -> do
    interpretWith logger $ \_ -> \case
      Log severity message -> liftIO $ do
        atomicModifyIORef' counter $ \c -> (c + 1, ())
        hPutStrLn handle message
        whenM ((== 10) <$> readIORef counter) $ do
          hFlush handle
          atomicModifyIORef' counter $ const (0, ())

This is pretty much what I was looking for but there are a couple of issues:

  1. This requires unliftio as a dependency to workaround the IO limitation of System.IO.w…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@arybczak
Comment options

@arybczak
Comment options

@emlautarom1
Comment options

@arybczak
Comment options

Answer selected by emlautarom1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #297 on February 03, 2025 19:28.