module System.Logger.Settings
( Settings
, Level (..)
, Output (..)
, DateFormat (..)
, defSettings
, output
, setOutput
, format
, setFormat
, bufSize
, setBufSize
, delimiter
, setDelimiter
, netstrings
, setNetStrings
, logLevel
, logLevelMap
, logLevelOf
, setLogLevel
, setLogLevelMap
, setLogLevelOf
, name
, setName
, nameMsg
, iso8601UTC
) where
import Data.String
import Data.ByteString (ByteString)
import Data.ByteString.Char8 (pack)
import Data.Map.Strict as Map
import Data.Text (Text)
import Data.UnixTime
import System.Log.FastLogger (defaultBufSize)
import System.Logger.Message
data Settings = Settings
{ _logLevel :: !Level
, _levelMap :: !(Map Text Level)
, _output :: !Output
, _format :: !(Maybe DateFormat)
, _delimiter :: !ByteString
, _netstrings :: !Bool
, _bufSize :: !Int
, _name :: !(Maybe Text)
, _nameMsg :: !(Msg -> Msg)
}
output :: Settings -> Output
output = _output
setOutput :: Output -> Settings -> Settings
setOutput x s = s { _output = x }
format :: Settings -> Maybe DateFormat
format = _format
setFormat :: Maybe DateFormat -> Settings -> Settings
setFormat x s = s { _format = x }
bufSize :: Settings -> Int
bufSize = _bufSize
setBufSize :: Int -> Settings -> Settings
setBufSize x s = s { _bufSize = max 1 x }
delimiter :: Settings -> ByteString
delimiter = _delimiter
setDelimiter :: ByteString -> Settings -> Settings
setDelimiter x s = s { _delimiter = x }
netstrings :: Settings -> Bool
netstrings = _netstrings
setNetStrings :: Bool -> Settings -> Settings
setNetStrings x s = s { _netstrings = x }
logLevel :: Settings -> Level
logLevel = _logLevel
setLogLevel :: Level -> Settings -> Settings
setLogLevel x s = s { _logLevel = x }
logLevelOf :: Text -> Settings -> Maybe Level
logLevelOf x s = Map.lookup x (_levelMap s)
logLevelMap :: Settings -> Map Text Level
logLevelMap = _levelMap
setLogLevelOf :: Text -> Level -> Settings -> Settings
setLogLevelOf n x s = s { _levelMap = Map.insert n x (_levelMap s) }
setLogLevelMap :: Map Text Level -> Settings -> Settings
setLogLevelMap x s = s { _levelMap = x }
name :: Settings -> Maybe Text
name = _name
setName :: Maybe Text -> Settings -> Settings
setName Nothing s = s { _name = Nothing, _nameMsg = id }
setName (Just xs) s = s { _name = Just xs, _nameMsg = "logger" .= xs }
nameMsg :: Settings -> (Msg -> Msg)
nameMsg = _nameMsg
data Level
= Trace
| Debug
| Info
| Warn
| Error
| Fatal
deriving (Eq, Ord, Read, Show)
data Output
= StdOut
| StdErr
| Path FilePath
deriving (Eq, Ord, Show)
newtype DateFormat = DateFormat
{ display :: UnixTime -> ByteString
}
instance IsString DateFormat where
fromString = DateFormat . formatUnixTimeGMT . pack
iso8601UTC :: DateFormat
iso8601UTC = "%Y-%0m-%0dT%0H:%0M:%0SZ"
defSettings :: Settings
defSettings = Settings
Debug
Map.empty
StdOut
(Just iso8601UTC)
", "
False
defaultBufSize
Nothing
id