From 70eaa0f6e72c72447df71146233eb2fbe4cd0a5f Mon Sep 17 00:00:00 2001 From: Toralf Wittner Date: Mon, 28 Apr 2014 23:24:53 +0200 Subject: [PATCH] Rename `=:` to `.=` and add `~~`. `~~` is an alias of `.` with lower precedence to allow mixing of `.=` and `~~` without requiring parentheses. --- src/System/Logger.hs | 3 ++- src/System/Logger/Message.hs | 31 ++++++++++++++++++++++++------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/System/Logger.hs b/src/System/Logger.hs index 01ef70b..6b33989 100644 --- a/src/System/Logger.hs +++ b/src/System/Logger.hs @@ -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" diff --git a/src/System/Logger/Message.hs b/src/System/Logger/Message.hs index 72c0e3f..f3f8077 100644 --- a/src/System/Logger/Message.hs +++ b/src/System/Logger/Message.hs @@ -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. -- GitLab