Commit 5cf41025 authored by Toralf Wittner's avatar Toralf Wittner
Browse files

Make `DateFormat` a function.

As discussed in #3, this exposes the `DateFormat` constructor to allow
clients complete control over turning `UnixTime` values into
`ByteStrings`.
parent b9aa0ea2
Loading
Loading
Loading
Loading
+3 −5
Original line number Diff line number Diff line
@@ -32,7 +32,7 @@ module System.Logger
    , Logger
    , Level      (..)
    , Output     (..)
    , DateFormat
    , DateFormat (..)
    , iso8601UTC

      -- * Core API
@@ -57,7 +57,6 @@ module System.Logger
    ) where

import Control.Applicative
import Control.AutoUpdate
import Control.Monad
import Control.Monad.IO.Class
import Data.Maybe (fromMaybe)
@@ -120,9 +119,8 @@ new s = liftIO $ do
    fn StdErr   = FL.newStderrLoggerSet
    fn (Path p) = flip FL.newFileLoggerSet p

    mkGetDate "" = return (return id)
    mkGetDate f  = mkAutoUpdate defaultUpdateSettings
        { updateAction = msg . formatUnixTimeGMT (template f) <$> getUnixTime }
    mkGetDate Nothing  = return (return id)
    mkGetDate (Just f) = return (msg . (display f) <$> getUnixTime)

    mergeWith m e = Map.fromList (readNote "Invalid LOG_LEVEL_MAP" e) `Map.union` m

+24 −15
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ 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

@@ -45,7 +46,7 @@ data Settings = Settings
    { _logLevel   :: !Level              -- ^ messages below this log level will be suppressed
    , _levelMap   :: !(Map Text Level)   -- ^ log level per named logger
    , _output     :: !Output             -- ^ log sink
    , _format     :: !DateFormat       -- ^ the timestamp format (use \"\" to disable timestamps)
    , _format     :: !(Maybe DateFormat) -- ^ the timestamp format
    , _delimiter  :: !ByteString         -- ^ text to intersperse between fields of a log line
    , _netstrings :: !Bool               -- ^ use <http://cr.yp.to/proto/netstrings.txt netstrings> encoding (fixes delimiter to \",\")
    , _bufSize    :: !Int                -- ^ how many bytes to buffer before commiting to sink
@@ -60,11 +61,11 @@ setOutput :: Output -> Settings -> Settings
setOutput x s = s { _output = x }

-- | The time and date format used for the timestamp part of a log line.
format :: Settings -> DateFormat
format :: Settings -> Maybe DateFormat
format = _format

setFormat :: DateFormat -> Settings -> Settings
setFormat x s = s { _format = x }
setFormat x s = s { _format = Just x }

bufSize :: Settings -> Int
bufSize = _bufSize
@@ -135,11 +136,11 @@ data Output
    deriving (Eq, Ord, Show)

newtype DateFormat = DateFormat
    { template :: ByteString
    } deriving (Eq, Ord, Show)
    { display :: UnixTime -> ByteString
    }

instance IsString DateFormat where
    fromString = DateFormat . pack
    fromString = DateFormat . formatUnixTimeGMT . pack

-- | ISO 8601 date-time format.
iso8601UTC :: DateFormat
@@ -162,5 +163,13 @@ iso8601UTC = "%Y-%0m-%0dT%0H:%0M:%0SZ"
--   * 'name'       = Nothing
--
defSettings :: Settings
defSettings = Settings Debug Map.empty StdOut iso8601UTC ", " False defaultBufSize Nothing id
defSettings = Settings
    Debug
    Map.empty
    StdOut
    (Just iso8601UTC)
    ", "
    False
    defaultBufSize
    Nothing
    id
+0 −1
Original line number Diff line number Diff line
@@ -37,7 +37,6 @@ library
    build-depends:
          base              >= 4.6    && < 5
        , bytestring        >= 0.10.4 && < 1.0
        , auto-update       >= 0.1    && < 0.2
        , containers        >= 0.5
        , double-conversion >= 0.2    && < 3.0
        , fast-logger       >= 2.1.4  && < 3.0