on master help sign in
Use Builder for log message, remove blaze-builder dep
commit6307556471ff963c5e0c17d1dfae7a3b4499e0fc authorOzgun Ataman <ozgun.ataman@soostone.com> dateMon 2 Mar 2015, 19:25 (12 years ago) parent8f1cc08
4 files changed+57−15
katip/katip.cabal+1−1 katip/src/Katip.hs+5 katip/src/Katip/Core.hs+43−5 katip/src/Katip/Scribes/Handle.hs+8−9
katip/katip.cabal+1−1
@@ -32,9 +32,9 @@ library
3232 base >=4.6 && <4.7
3333 , aeson >=0.7 && <0.8
3434 , auto-update >= 0.1 && < 0.2
35− , blaze-builder >= 0.3 && < 0.4
3635 , bytestring >= 0.9 && < 0.11
3736 , containers >=0.5 && <0.6
37+ , exceptions >= 0.5 && < 0.7
3838 , hostname >=1.0 && <1.1
3939 , lens >=4.2 && <4.7
4040 , lens-aeson >= 1.0 && < 1.1
katip/src/Katip.hs+5
@@ -8,6 +8,8 @@ module Katip
88 , PayloadSelection (..)
99 , Scribe (..)
1010 , LogEnv (..)
11+ , KatipT (..)
12+ , runKatipT
1113
1214 -- * Initializing Loggers
1315 , initLogEnv
@@ -15,6 +17,9 @@ module Katip
1517 , unregisterScribe
1618
1719 -- * Logging Functions
20+ , LogStr (..)
21+ , logStr, ls
22+
1823 , logF
1924 , logM
2025 , logT
katip/src/Katip/Core.hs+43−5
@@ -14,6 +14,7 @@ import Control.Applicative
1414import Control.AutoUpdate
1515import Control.Concurrent
1616import Control.Lens
17+import Control.Monad.Catch
1718import Control.Monad.Reader
1819import Data.Aeson (ToJSON (..))
1920import qualified Data.Aeson as A
@@ -26,6 +27,7 @@ import Data.String
2627import Data.String.Conv
2728import Data.Text (Text)
2829import qualified Data.Text as T
30+import qualified Data.Text.Lazy.Builder as B
2931import Data.Time
3032import GHC.Generics hiding (to)
3133import Language.Haskell.TH
@@ -98,6 +100,29 @@ instance ToJSON Severity where
98100 toJSON s = A.String (renderSeverity s)
99101
100102
103+-------------------------------------------------------------------------------
104+-- | Log message with Builder unerneath; use '<>' to concat in O(1).
105+newtype LogStr = LogStr { unLogStr :: B.Builder }
106+ deriving (Generic)
107+
108+instance IsString LogStr where
109+ fromString = LogStr . B.fromString
110+
111+
112+-------------------------------------------------------------------------------
113+-- | Pack any string-like thing into a 'LogMsg'. This will
114+-- automatically work on 'String', 'ByteString, 'Text' and any of the
115+-- lazy variants.
116+logStr :: StringConv a Text => a -> LogStr
117+logStr t = LogStr (B.fromText $ toS t)
118+
119+
120+-------------------------------------------------------------------------------
121+-- | Shorthand for 'logMsg'
122+ls :: StringConv a Text => a -> LogStr
123+ls = logStr
124+
125+
101126-------------------------------------------------------------------------------
102127-- | This has everything each log message will contain.
103128data Item a = Item {
@@ -108,7 +133,7 @@ data Item a = Item {
108133 , itemHost :: HostName
109134 , itemProcess :: ProcessID
110135 , itemPayload :: a
111− , itemMessage :: Text
136+ , itemMessage :: LogStr
112137 , itemTime :: UTCTime
113138 , itemNamespace :: Namespace
114139 , itemLoc :: Maybe Loc
@@ -125,7 +150,7 @@ instance ToJSON a => ToJSON (Item a) where
125150 , "host" A..= itemHost
126151 , "pid" A..= A.String (toS (show itemProcess))
127152 , "data" A..= itemPayload
128− , "msg" A..= itemMessage
153+ , "msg" A..= (B.toLazyText $ unLogStr itemMessage)
129154 , "at" A..= itemTime
130155 , "ns" A..= itemNamespace
131156 , "loc" A..= fmap LocJs itemLoc
@@ -236,6 +261,19 @@ class Katip m where
236261 getLogEnv :: m LogEnv
237262
238263
264+-------------------------------------------------------------------------------
265+-- | A concrete monad you can use to run logging actions.
266+newtype KatipT m a = KatipT { unKatipT :: ReaderT LogEnv m a }
267+ deriving ( Monad, MonadIO, MonadMask, MonadCatch, MonadThrow
268+ , MonadReader LogEnv)
269+
270+
271+-------------------------------------------------------------------------------
272+-- | Execute 'KatipT' on a log env.
273+runKatipT :: LogEnv -> KatipT m a -> m a
274+runKatipT le (KatipT f) = runReaderT f le
275+
276+
239277-------------------------------------------------------------------------------
240278-- | Log with everything, including a source code location. This is
241279-- very low level and you typically can use 'logT' in its place.
@@ -245,7 +283,7 @@ logI
245283 -> Namespace
246284 -> Maybe Loc
247285 -> Severity
248− -> Text
286+ -> LogStr
249287 -> m ()
250288logI a ns loc sev msg = do
251289 LogEnv{..} <- getLogEnv
@@ -274,7 +312,7 @@ logF
274312 -- ^ Specific namespace of the message.
275313 -> Severity
276314 -- ^ Severity of the message
277− -> Text
315+ -> LogStr
278316 -- ^ The log message
279317 -> m ()
280318logF a ns sev msg = logI a ns Nothing sev msg
@@ -286,7 +324,7 @@ logM
286324 :: (Applicative m, MonadIO m, Katip m)
287325 => Namespace
288326 -> Severity
289− -> Text
327+ -> LogStr
290328 -> m ()
291329logM ns sev msg = logF () ns sev msg
292330
katip/src/Katip/Scribes/Handle.hs+8−9
@@ -6,17 +6,16 @@ module Katip.Scribes.Handle
66 ) where
77
88-------------------------------------------------------------------------------
9−import Blaze.ByteString.Builder
10−import Blaze.ByteString.Builder.Char.Utf8
119import Control.Lens
1210import Control.Monad
1311import Data.Aeson.Lens
14−import qualified Data.ByteString.Char8 as B
15−import qualified Data.HashMap.Strict as HM
12+import qualified Data.HashMap.Strict as HM
1613import Data.Monoid
14+import Data.Text.Lazy.Builder
15+import Data.Text.Lazy.IO as T
1716import Data.Time
1817import System.IO
19−import System.IO.Unsafe (unsafePerformIO)
18+import System.IO.Unsafe (unsafePerformIO)
2019import System.Locale
2120-------------------------------------------------------------------------------
2221import Katip.Core
@@ -25,7 +24,7 @@ import Katip.Core
2524
2625-------------------------------------------------------------------------------
2726brackets :: Builder -> Builder
28−brackets m = fromByteString "[" <> m <> fromByteString "]"
27+brackets m = fromText "[" <> m <> fromText "]"
2928
3029
3130-------------------------------------------------------------------------------
@@ -41,7 +40,7 @@ renderPrim :: Primitive -> Builder
4140renderPrim (StringPrim t) = fromText t
4241renderPrim (NumberPrim s) = fromString (show s)
4342renderPrim (BoolPrim b) = fromString (show b)
44−renderPrim NullPrim = fromByteString "null"
43+renderPrim NullPrim = fromText "null"
4544
4645
4746-------------------------------------------------------------------------------
@@ -61,7 +60,7 @@ mkHandleScribe cs h sev verb = do
6160 ColorLog b -> return b
6261 return $ Scribe $ \ i@Item{..} -> do
6362 when (itemSeverity >= sev) $
64− B.hPutStrLn h $ toByteString $ formatItem colorize verb i
63+ T.hPutStrLn h $ toLazyText $ formatItem colorize verb i
6564
6665
6766-------------------------------------------------------------------------------
@@ -75,7 +74,7 @@ formatItem withColor verb Item{..} =
7574 brackets (fromString (show itemThread)) <>
7675 mconcat ks <>
7776 maybe mempty (brackets . fromString . locationToString) itemLoc <>
78− fromText " " <> fromText itemMessage
77+ fromText " " <> (unLogStr itemMessage)
7978 where
8079 nowStr = fromString $ formatTime defaultTimeLocale "%Y-%m-%d %H:%M:%S" itemTime
8180 ks = map brackets $ getKeys verb itemPayload