kazu-yamamoto / logger

A fast logging system for Haskell
159 stars 68 forks source link

[monad-logger] runFileLoggingT does not log to file #130

Closed epsilonhalbe closed 7 years ago

epsilonhalbe commented 7 years ago

I have a few forked processes that log in a channel and another one that uses unChanLoggingT + runFileLoggingT to put all of them in one file - but this does not work, neither a file is created nor any lines are added to the (non-existent) file.

I think this is an issue with buffering/flushing - looking at the source code Logger.hs#L661

runFileLoggingT :: MonadBaseControl IO m => FilePath -> LoggingT m a -> m a
runFileLoggingT fp log = bracket
    (liftBase $ openFile fp AppendMode)
    (liftBase . hClose)
    $ \h -> (runLoggingT log) (defaultOutput h)

should be (with the additional import System.IO (BufferMode(LineBuffering), hSetBuffering))

runFileLoggingT :: MonadBaseControl IO m => FilePath -> LoggingT m a -> m a
runFileLoggingT fp log = bracket
    (liftBase $ openFile fp AppendMode)
    (liftBase . hClose)
    $ \h -> hSetBuffering LineBuffering h >> (runLoggingT log) (defaultOutput h)

or configurable -

runFileLoggingT :: MonadBaseControl IO m => FilePath -> BufferMode -> LoggingT m a -> m a
runFileLoggingT fp bm log = bracket
    (liftBase $ openFile fp AppendMode)
    (liftBase . hClose)
    $ \h -> hSetBuffering bm h >> (runLoggingT log) (defaultOutput h)
epsilonhalbe commented 7 years ago

I can provide a pull request from https://github.com/epsilonhalbe/logger/commit/364ea83e6c12d763228300e53ec9c24bf9b37e11

snoyberg commented 7 years ago

A PR would be great, thanks.