module System.Logger
( withConsoleLogger
, withFileLogger
, module System.Logger.Types
, module System.Logger.Logger
, module System.Logger.Backend.Handle
, LogConfig(..)
, logConfigLogger
, logConfigBackend
, defaultLogConfig
, validateLogConfig
, pLogConfig
, pLogConfig_
) where
import Configuration.Utils hiding (Lens')
import Control.Lens hiding ((.=))
import Control.Monad.IO.Class
import Control.Monad.Trans.Control
import qualified Data.Text as T
import Data.Typeable
import GHC.Generics
import Prelude.Unicode
import System.Logger.Backend.ColorOption
import System.Logger.Backend.Handle
import System.Logger.Logger
import System.Logger.Types
withConsoleLogger
∷ (MonadIO m, MonadBaseControl IO m)
⇒ LogLevel
→ LoggerT T.Text m α
→ m α
withConsoleLogger level inner =
withHandleBackend (config ^. logConfigBackend) $ \backend →
withLogger (config ^. logConfigLogger) backend $ runLoggerT inner
where
config = defaultLogConfig
& logConfigLogger ∘ loggerConfigThreshold .~ level
withFileLogger
∷ (MonadIO m, MonadBaseControl IO m)
⇒ FilePath
→ LogLevel
→ LoggerT T.Text m α
→ m α
withFileLogger f level inner =
withHandleBackend (config ^. logConfigBackend) $ \backend →
withLogger (config ^. logConfigLogger) backend $ runLoggerT inner
where
config = defaultLogConfig
& logConfigLogger ∘ loggerConfigThreshold .~ level
& logConfigBackend ∘ handleBackendConfigColor .~ ColorFalse
& logConfigBackend ∘ handleBackendConfigHandle .~ FileHandle f
data LogConfig = LogConfig
{ _logConfigLogger ∷ !LoggerConfig
, _logConfigBackend ∷ !HandleBackendConfig
}
deriving (Show, Read, Eq, Ord, Typeable, Generic)
logConfigLogger ∷ Lens' LogConfig LoggerConfig
logConfigLogger = lens _logConfigLogger $ \a b → a { _logConfigLogger = b }
logConfigBackend ∷ Lens' LogConfig HandleBackendConfig
logConfigBackend = lens _logConfigBackend $ \a b → a { _logConfigBackend = b }
defaultLogConfig ∷ LogConfig
defaultLogConfig = LogConfig
{ _logConfigLogger = defaultLoggerConfig
, _logConfigBackend = defaultHandleBackendConfig
}
validateLogConfig ∷ ConfigValidation LogConfig []
validateLogConfig LogConfig{..} = do
validateLoggerConfig _logConfigLogger
validateHandleBackendConfig _logConfigBackend
instance ToJSON LogConfig where
toJSON LogConfig{..} = object
[ "logger" .= _logConfigLogger
, "backend" .= _logConfigBackend
]
instance FromJSON (LogConfig → LogConfig) where
parseJSON = withObject "LogConfig" $ \o → id
<$< logConfigLogger %.: "logger" × o
<*< logConfigBackend %.: "backend" × o
pLogConfig ∷ MParser LogConfig
pLogConfig = pLogConfig_ ""
pLogConfig_
∷ T.Text
→ MParser LogConfig
pLogConfig_ prefix = id
<$< logConfigLogger %:: pLoggerConfig_ prefix
<*< logConfigBackend %:: pHandleBackendConfig_ prefix