Skip to content

Commit

Permalink
Delay for 0.1s when flushing
Browse files Browse the repository at this point in the history
fast-logger has a bug[^1] where flushing immediately after pushing a log
message sometimes does not find anything to flush. Typically, users will
flush the log because they intend to print non-logging output or as part
of program exit. This bug causes output to be interleaved incorrectly in
the former case and log messages to be lost entirely in the latter.

To work around this, we delay 0.1s before flushing. There's no guarantee
this works, and I don't know if the delay could be shorter or should be
longer, but it has made things work reliably in my testing so far.

[^1]: kazu-yamamoto/logger#213
  • Loading branch information
pbrisbin committed May 3, 2024
1 parent 9607f20 commit 57ad67e
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Blammo/Logging/Logger.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import Blammo.Logging.LogSettings
import Blammo.Logging.Terminal
import Blammo.Logging.Test hiding (getLoggedMessages)
import qualified Blammo.Logging.Test as LoggedMessages
import Control.Concurrent (threadDelay)
import Control.Lens (view)
import Control.Monad (unless)
import Control.Monad.IO.Class (MonadIO (..))
Expand Down Expand Up @@ -82,7 +83,11 @@ pushLogStrLn logger str = case lLoggedMessages logger of

flushLogStr :: MonadIO m => Logger -> m ()
flushLogStr logger = case lLoggedMessages logger of
Nothing -> liftIO $ FastLogger.flushLogStr loggerSet
Nothing -> liftIO $ do
-- Delay for 0.1s before flushing to work around
-- https://github.com/kazu-yamamoto/logger/issues/213
threadDelay 100000
FastLogger.flushLogStr loggerSet
Just _ -> pure ()
where
loggerSet = getLoggerLoggerSet logger
Expand Down

0 comments on commit 57ad67e

Please sign in to comment.