@@ -1,5 +1,6 @@
11{-# LANGUAGE DeriveGeneric #-}
22{-# LANGUAGE FlexibleContexts #-}
3+{-# LANGUAGE FlexibleInstances #-}
34{-# LANGUAGE GeneralizedNewtypeDeriving #-}
45{-# LANGUAGE MultiParamTypeClasses #-}
56{-# LANGUAGE OverloadedStrings #-}
@@ -14,8 +15,15 @@ import Control.Applicative
1415import Control.AutoUpdate
1516import Control.Concurrent
1617import Control.Lens
18+import Control.Monad
1719import Control.Monad.Catch
18−import Control.Monad.Reader
20+import Control.Monad.IO.Class
21+import Control.Monad.Trans.Class
22+import Control.Monad.Trans.Either
23+import Control.Monad.Trans.Maybe
24+import Control.Monad.Trans.Reader
25+import Control.Monad.Trans.State
26+import Control.Monad.Trans.Writer
1927import Data.Aeson (ToJSON (..))
2028import qualified Data.Aeson as A
2129import Data.Aeson.Lens
@@ -59,14 +67,14 @@ newtype Environment = Environment { getEnvironment :: Text }
5967
6068-------------------------------------------------------------------------------
6169data Severity
62− = Debug -- ^ Debug messages
63− | Info -- ^ Information
64− | Notice -- ^ Normal runtime Conditions
65− | Warning -- ^ General Warnings
66− | Error -- ^ General Errors
67− | Critical -- ^ Severe situations
68− | Alert -- ^ Take immediate action
69− | Emergency -- ^ System is unusable
70+ = DebugS -- ^ Debug messages
71+ | InfoS -- ^ Information
72+ | NoticeS -- ^ Normal runtime Conditions
73+ | WarningS -- ^ General Warnings
74+ | ErrorS -- ^ General Errors
75+ | CriticalS -- ^ Severe situations
76+ | AlertS -- ^ Take immediate action
77+ | EmergencyS -- ^ System is unusable
7078 deriving (Eq, Ord, Show, Read, Generic)
7179
7280
@@ -85,14 +93,14 @@ data Verbosity = V0 | V1 | V2 | V3
8593-------------------------------------------------------------------------------
8694renderSeverity :: Severity -> Text
8795renderSeverity s = case s of
88− Debug -> "Debug"
89− Info -> "Info"
90− Notice -> "Notice"
91− Warning -> "Warning"
92− Error -> "Error"
93− Critical -> "Critical"
94− Alert -> "Alert"
95− Emergency -> "Emergency"
96+ DebugS -> "Debug"
97+ InfoS -> "Info"
98+ NoticeS -> "Notice"
99+ WarningS -> "Warning"
100+ ErrorS -> "Error"
101+ CriticalS -> "Critical"
102+ AlertS -> "Alert"
103+ EmergencyS -> "Emergency"
96104
97105
98106-------------------------------------------------------------------------------
@@ -108,6 +116,9 @@ newtype LogStr = LogStr { unLogStr :: B.Builder }
108116instance IsString LogStr where
109117 fromString = LogStr . B.fromString
110118
119+instance Monoid LogStr where
120+ mappend (LogStr a) (LogStr b) = LogStr (a `mappend` b)
121+ mempty = LogStr mempty
111122
112123-------------------------------------------------------------------------------
113124-- | Pack any string-like thing into a 'LogMsg'. This will
@@ -123,6 +134,11 @@ ls :: StringConv a Text => a -> LogStr
123134ls = logStr
124135
125136
137+-------------------------------------------------------------------------------
138+showLS :: Show a => a -> LogStr
139+showLS = ls . show
140+
141+
126142-------------------------------------------------------------------------------
127143-- | This has everything each log message will contain.
128144data Item a = Item {
@@ -263,11 +279,35 @@ class MonadIO m => Katip m where
263279 getLogEnv :: m LogEnv
264280
265281
282+instance Katip m => Katip (ReaderT s m) where
283+ getLogEnv = lift getLogEnv
284+
285+
286+instance Katip m => Katip (EitherT s m) where
287+ getLogEnv = lift getLogEnv
288+
289+
290+instance Katip m => Katip (MaybeT m) where
291+ getLogEnv = lift getLogEnv
292+
293+
294+instance Katip m => Katip (StateT s m) where
295+ getLogEnv = lift getLogEnv
296+
297+
298+instance (Katip m, Monoid s) => Katip (WriterT s m) where
299+ getLogEnv = lift getLogEnv
300+
301+
266302-------------------------------------------------------------------------------
267303-- | A concrete monad you can use to run logging actions.
268304newtype KatipT m a = KatipT { unKatipT :: ReaderT LogEnv m a }
269− deriving ( Monad, MonadIO, MonadMask, MonadCatch, MonadThrow
270− , MonadReader LogEnv)
305+ deriving ( Functor, Applicative, Monad, MonadIO
306+ , MonadMask, MonadCatch, MonadThrow )
307+
308+
309+instance MonadIO m => Katip (KatipT m) where
310+ getLogEnv = KatipT ask
271311
272312
273313-------------------------------------------------------------------------------
@@ -345,14 +385,14 @@ instance TH.Lift Verbosity where
345385
346386
347387instance TH.Lift Severity where
348− lift Debug = [| Debug |]
349− lift Info = [| Info |]
350− lift Notice = [| Notice |]
351− lift Warning = [| Warning |]
352− lift Error = [| Error |]
353− lift Critical = [| Critical |]
354− lift Alert = [| Alert |]
355− lift Emergency = [| Emergency |]
388+ lift DebugS = [| DebugS |]
389+ lift InfoS = [| InfoS |]
390+ lift NoticeS = [| NoticeS |]
391+ lift WarningS = [| WarningS |]
392+ lift ErrorS = [| ErrorS |]
393+ lift CriticalS = [| CriticalS |]
394+ lift AlertS = [| AlertS |]
395+ lift EmergencyS = [| EmergencyS |]
356396
357397
358398-- | Lift a location into an Exp.