Skip to content

[RFC] HoistLogAction with MVar #44

@yhuangsh

Description

@yhuangsh

Case use:

  • Have “global” log action that is mutable at run time. For example, the default log action is at a low verbosity level at normal time, a system admin could switch to a higher verbosity level logs without having to restart a running server
  • MVar allows another (admin) thread to change the default log action on the fly via the MVar
  • Do not use any of the performUnsafeIO hacks
module Colog.Core.Action (hoistLogActionMVar , hoistLogActionIOMVar) where

import Colog (LogAction(..), hoistLogAction)
import Control.Concurrent.MVar (MVar, readMVar)
import Control.Monad.IO.Class (MonadIO(..))

hoistLogActionMVar ::  MonadIO n => (forall a. m a -> n a) -> MVar (LogAction m msg) -> LogAction n msg
hoistLogActionMVar f v =
   LogAction
     \msg -> do
        m <- liftIO $ readMVar v
        unLogAction (hoistLogAction f m) msg

hoistLogActionIOMVar :: MonadIO m => MVar (LogAction IO msg) -> LogAction m msg
hoistLogActionIOMVar = hoistLogActionMVar liftIO

Every log action will involve additional IO (readMVar), performance impact not assessed.

Appreciate comments

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions