{-# LANGUAGE OverloadedStrings, TypeSynonymInstances, FlexibleInstances, ExistentialQuantification, TypeFamilies, GeneralizedNewtypeDeriving, StandaloneDeriving, MultiParamTypeClasses, UndecidableInstances, AllowAmbiguousTypes, ScopedTypeVariables, FunctionalDependencies, FlexibleContexts, ConstraintKinds #-}
module System.Log.Heavy.Level
(
Level (..),
levelToLogLevel, logLevelToLevel,
parseLevel,
trace_level, debug_level, info_level, warn_level, error_level, fatal_level,
disable_logging
) where
import Control.Monad.Logger (LogLevel (..))
import qualified Data.Text as T
import qualified System.Posix.Syslog as Syslog
data Level = Level {
levelName :: T.Text
, levelInt :: Int
, levelToPriority :: Syslog.Priority
} deriving (Eq)
instance Show Level where
show l = T.unpack (levelName l)
instance Ord Level where
compare l1 l2 = compare (levelInt l1) (levelInt l2)
trace_level :: Level
trace_level = Level "TRACE" 600 Syslog.Debug
debug_level :: Level
debug_level = Level "DEBUG" 500 Syslog.Debug
info_level :: Level
info_level = Level "INFO" 400 Syslog.Info
warn_level :: Level
warn_level = Level "WARN" 300 Syslog.Warning
error_level :: Level
error_level = Level "ERROR" 200 Syslog.Error
fatal_level :: Level
fatal_level = Level "FATAL" 100 Syslog.Emergency
disable_logging :: Level
disable_logging = Level "DISABLED" 0 Syslog.Emergency
levelToLogLevel :: Level -> LogLevel
levelToLogLevel l =
case levelName l of
"DEBUG" -> LevelDebug
"INFO" -> LevelInfo
"WARN" -> LevelWarn
"ERROR" -> LevelError
name -> LevelOther name
logLevelToLevel :: LogLevel -> Level
logLevelToLevel LevelDebug = debug_level
logLevelToLevel LevelInfo = info_level
logLevelToLevel LevelWarn = warn_level
logLevelToLevel LevelError = error_level
logLevelToLevel (LevelOther name) = Level name 210 Syslog.Alert
parseLevel :: [Level]
-> T.Text
-> Maybe Level
parseLevel knownLevels str = go knownLevels
where
go [] = Nothing
go (l:ls)
| T.toCaseFold (levelName l) == needle = Just l
| otherwise = go ls
needle = T.toCaseFold str