module Log.Class (
UTCTime
, MonadTime(..)
, MonadLog(..)
, logAttention
, logInfo
, logTrace
, logAttention_
, logInfo_
, logTrace_
) where
import Control.Monad.Time
import Control.Monad.Trans
import Control.Monad.Trans.Control
import Data.Aeson
import Data.Aeson.Types
import Data.Time
import Prelude
import qualified Data.Text as T
import Log.Data
class MonadTime m => MonadLog m where
logMessage :: UTCTime
-> LogLevel
-> T.Text
-> Value
-> m ()
localData :: [Pair] -> m a -> m a
localDomain :: T.Text -> m a -> m a
instance (
MonadLog m
, Monad (t m)
, MonadTransControl t
) => MonadLog (t m) where
logMessage time level message = lift . logMessage time level message
localData data_ m = controlT $ \run -> localData data_ (run m)
localDomain domain m = controlT $ \run -> localDomain domain (run m)
controlT :: (MonadTransControl t, Monad (t m), Monad m)
=> (Run t -> m (StT t a)) -> t m a
controlT f = liftWith f >>= restoreT . return
logAttention :: (MonadLog m, ToJSON a) => T.Text -> a -> m ()
logAttention msg = logNow LogAttention msg . toJSON
logInfo :: (MonadLog m, ToJSON a) => T.Text -> a -> m ()
logInfo msg = logNow LogInfo msg . toJSON
logTrace :: (MonadLog m, ToJSON a) => T.Text -> a -> m ()
logTrace msg = logNow LogTrace msg . toJSON
logAttention_ :: MonadLog m => T.Text -> m ()
logAttention_ = (`logAttention` emptyObject)
logInfo_ :: MonadLog m => T.Text -> m ()
logInfo_ = (`logInfo` emptyObject)
logTrace_ :: MonadLog m => T.Text -> m ()
logTrace_ = (`logTrace` emptyObject)
logNow :: MonadLog m => LogLevel -> T.Text -> Value -> m ()
logNow level message data_ = do
time <- currentTime
logMessage time level message data_