Commit 70eaa0f6 authored by Toralf Wittner's avatar Toralf Wittner
Browse files

Rename `=:` to `.=` and add `~~`.

`~~` is an alias of `.` with lower precedence to allow
mixing of `.=` and `~~` without requiring parentheses.
parent 580d92cd
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@
{-# LANGUAGE OverloadedStrings #-}

-- | Small layer on top of @fast-logger@ which adds log-levels and
-- timestamp support and not much more.
-- timestamp support (using @date-cache@) and not much more.
module System.Logger
    ( Level    (..)
    , Output   (..)
@@ -86,6 +86,7 @@ newtype DateFormat = DateFormat
instance IsString DateFormat where
    fromString = DateFormat . pack

-- | ISO 8601 date-time format.
iso8601UTC :: DateFormat
iso8601UTC = "%Y-%0m-%0dT%0H:%0M:%0SZ"

+24 −7
Original line number Diff line number Diff line
@@ -5,13 +5,23 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}

-- | 'Msg' and 'ToBytes' assist in constructing log messages.
-- For example:
--
-- @
-- > g <- new defSettings { bufSize = 1, output = StdOut }
-- > info g $ msg "some text" ~~ "key" .= "value" ~~ "okay" .= True
-- 2014-04-28T21:18:20Z, I, some text, key=value, okay=True
-- >
-- @
module System.Logger.Message
    ( ToBytes (..)
    , Msg
    , msg
    , field
    , (=:)
    , (.=)
    , (+++)
    , (~~)
    , val
    , render
    ) where
@@ -62,22 +72,29 @@ instance ToBytes Bool where
-- | Type representing log messages.
newtype Msg = Msg { builders :: [Builder] }

-- | Log some value.
-- | Turn some value into a 'Msg'.
msg :: ToBytes a => a -> Msg -> Msg
msg p (Msg m) = Msg (bytes p : m)

-- | Log some field, i.e. a key-value pair delimited by \"=\".
field, (=:) :: ToBytes a => ByteString -> a -> Msg -> Msg
-- | Render some field, i.e. a key-value pair delimited by \"=\".
field :: ToBytes a => ByteString -> a -> Msg -> Msg
field k v (Msg m) = Msg $ bytes k <> B.byteString "=" <> bytes v : m

infixr 5 =:
(=:) = field
-- | Alias of 'field'.
(.=) :: ToBytes a => ByteString -> a -> Msg -> Msg
(.=) = field
infixr 5 .=

infixr 5 +++
-- | Alias of '.' with lowered precedence to allow combination with '.='
-- without requiring parentheses.
(~~) :: (b -> c) -> (a -> b) -> a -> c
(~~) = (.)
infixr 4 ~~

-- | Concatenate two 'ToBytes' values.
(+++) :: (ToBytes a, ToBytes b) => a -> b -> Builder
a +++ b = bytes a <> bytes b
infixr 5 +++

-- | Type restriction. Useful to disambiguate string literals when
-- using @OverloadedStrings@ pragma.