| Safe Haskell | None | 
|---|---|
| Language | Haskell2010 | 
System.Wlog.CanLog
Contents
Description
Type class that add ability to log messages. Supports pure and IO logging.
Synopsis
- class Monad m => CanLog m where
- dispatchMessage :: LoggerName -> Severity -> Text -> m ()
 
 - type WithLogger m = (CanLog m, HasLoggerName m)
 - type WithLoggerIO m = (MonadIO m, WithLogger m)
 - logDebug :: WithLogger m => Text -> m ()
 - logError :: WithLogger m => Text -> m ()
 - logInfo :: WithLogger m => Text -> m ()
 - logNotice :: WithLogger m => Text -> m ()
 - logWarning :: WithLogger m => Text -> m ()
 - logMessage :: WithLogger m => Severity -> Text -> m ()
 - liftLogIO :: WithLoggerIO m => (IO a -> IO b) -> LoggerNameBox IO a -> m b
 
Documentation
class Monad m => CanLog m where Source #
Instances of this class should explain how they add messages to their log.
Minimal complete definition
Nothing
Methods
dispatchMessage :: LoggerName -> Severity -> Text -> m () Source #
dispatchMessage :: (MonadTrans t, t n ~ m, CanLog n) => LoggerName -> Severity -> Text -> m () Source #
Instances
type WithLogger m = (CanLog m, HasLoggerName m) Source #
Type alias for constraints CanLog and HasLoggerName.
 We need two different type classes to support more flexible interface
 but in practice we usually use them both.
type WithLoggerIO m = (MonadIO m, WithLogger m) Source #
Type alias for constraints WithLogger and MonadIO.
 It is a very common situation to use both of them together.
Logging functions
logDebug :: WithLogger m => Text -> m () Source #
Shortcut for logMessage to use according severity.
logError :: WithLogger m => Text -> m () Source #
Shortcut for logMessage to use according severity.
logInfo :: WithLogger m => Text -> m () Source #
Shortcut for logMessage to use according severity.
logNotice :: WithLogger m => Text -> m () Source #
Shortcut for logMessage to use according severity.
logWarning :: WithLogger m => Text -> m () Source #
Shortcut for logMessage to use according severity.
logMessage :: WithLogger m => Severity -> Text -> m () Source #
Logs message with specified severity using logger name in context.
liftLogIO :: WithLoggerIO m => (IO a -> IO b) -> LoggerNameBox IO a -> m b Source #
It's very common situation when we need to write log messages
inside functions that work in IO.
To do so we need to configure logger name each time inside work of this function.
liftLogIO is the easiest way to deal with such kind of situations.
Usage Example
We have some function
clientStart :: HostName -> .. -> IO ClientComponentMap
clientStart hostName .. = do
    ...
    forkIO $ routeIncoming endPoint msgs
    ...
We need to log Error messages in routeIncoming function.
To do that we firstly need clientStart to work in MonadIO
clientStart :: (MonadIO m) => HostName -> .. -> m ClientComponentMap
clientStart hostName .. = do
    ...
    liftIO $ forkIO $ routeIncoming endPoint msgs
    ...
After we added some logError into routeIncoming clientStart should
now work in WithLogger. Thus we can use WithLoggerIO which is combination of MonadIO and WithLogger.
Taking into consideration all above
we get:
clientStart :: WithLoggerIO m => HostName -> .. -> m ClientComponentMap
clientStart hostName .. = do
    ...
    logName <- askLoggerName
    liftIO $ forkIO $ usingLoggerName logName $ routeIncoming endPoint msgs
    ...
So, here we see how useful this function can be.
clientStart :: WithLoggerIO m => HostName -> .. -> m ClientComponentMap
clientStart hostName .. = do
    ...
    liftLogIO forkIO $ routeIncoming endPoint msgs
    ...