@@ -14,23 +14,81 @@
14module Effect.Logging
14module Effect.Logging
16import Effects
16import Effects
17−import public Effect.Logging.Level
19%access public
18%access public
20+-- ---------------------------------------------------- [ Log Level Definition ]
22+||| Logging levels are natural numbers wrapped in a data type for
25+||| Several aliases have been defined to aide in semantic use of the
26+||| logging levels. These aliases have come from the Log4j/Python
27+||| family of loggers.
28+data LogLevel : Nat -> Type where
32+ ||| A fine-grained debug message, typically capturing the flow through
36+ ||| A general debugging event.
39+ ||| An event for informational purposes.
42+ ||| An event that might possible lead to an error.
45+ ||| A severe error that will prevent the application from continuing.
48+ ||| An error in the application, possibly recoverable.
51+ ||| All events should be logged.
54+ ||| User defined logging level.
55+ CUSTOM : (n : Nat) -> {auto prf : LTE n 70} -> LogLevel n
57+instance Cast (LogLevel n) Nat where
60+instance Show (LogLevel n) where
69+ show (CUSTOM n) = unwords ["CUSTOM", show n]
71+instance Eq (LogLevel n) where
74+ lvlEq : LogLevel a -> LogLevel b -> Bool
75+ lvlEq {a} {b} _ _ = a == b
77+cmpLevel : LogLevel a -> LogLevel b -> Ordering
78+cmpLevel {a} {b} _ _ = compare a b
80+||| The Logging details, this is the resource that the effect is
21record LogRes (a : Type) where
82record LogRes (a : Type) where
22 constructor MkLogRes
83 constructor MkLogRes
23− lvl : Nat
84+ lvl : LogLevel n
24 cats : List a
85 cats : List a
26instance (Show a) => Show (LogRes a) where
87instance (Show a) => Show (LogRes a) where
27 show (MkLogRes l cs) = unwords ["Log Settings:", show l, show cs]
88 show (MkLogRes l cs) = unwords ["Log Settings:", show l, show cs]
29−instance (Eq a) => Eq (LogRes a) where
30− (==) (MkLogRes x y) (MkLogRes x' y') = x' == x && y == y'
32instance Default (LogRes a) where
90instance Default (LogRes a) where
33− default = MkLogRes Z Nil
91+ default = MkLogRes OFF Nil
35||| A Logging effect to log levels and categories.
93||| A Logging effect to log levels and categories.
36data Logging : Effect where
94data Logging : Effect where
@@ -40,7 +98,7 @@ data Logging : Effect where
40 ||| @cats The categories it should appear under.
98 ||| @cats The categories it should appear under.
41 ||| @msg The message to log.
99 ||| @msg The message to log.
42 Log : (Show a, Eq a) =>
100 Log : (Show a, Eq a) =>
43− (lvl : Nat)
101+ (lvl : LogLevel n)
44 -> (cats : List a)
102 -> (cats : List a)
45 -> (msg : String)
103 -> (msg : String)
46 -> sig Logging () (LogRes a)
104 -> sig Logging () (LogRes a)
@@ -49,7 +107,7 @@ data Logging : Effect where
50 ||| @nlvl The new logging level
108 ||| @nlvl The new logging level
51 SetLogLvl : (Show a, Eq a) =>
109 SetLogLvl : (Show a, Eq a) =>
52− (nlvl : Nat) ->
110+ (nlvl : LogLevel n) ->
53 sig Logging () (LogRes a) (LogRes a)
111 sig Logging () (LogRes a) (LogRes a)
55 ||| Change the categories to show.
113 ||| Change the categories to show.
@@ -64,7 +122,7 @@ data Logging : Effect where
64 ||| @nlvl The new logging level.
122 ||| @nlvl The new logging level.
65 ||| @ncats The categories to show.
123 ||| @ncats The categories to show.
66 InitLogger : (Show a, Eq a) =>
124 InitLogger : (Show a, Eq a) =>
67− (nlvl : Nat) ->
125+ (nlvl : LogLevel n) ->
68 (ncats : List a) ->
126 (ncats : List a) ->
69 sig Logging () (LogRes a) (LogRes a)
127 sig Logging () (LogRes a) (LogRes a)
@@ -81,16 +139,16 @@ instance Handler Logging IO where
81 k () newSt
139 k () newSt
83 handle st (Log l cs' msg) k = do
141 handle st (Log l cs' msg) k = do
84− case l <= lvl st of
142+ case cmpLevel l (lvl st) of
85− False => k () st
143+ GT => k () st
86− True => do
144+ otherwise => do
87 let res = and $ map (\x => elem x cs') (cats st)
145 let res = and $ map (\x => elem x cs') (cats st)
88 let prompt = if isNil (cats st)
146 let prompt = if isNil (cats st)
89 then unwords [show l, ":"]
147 then unwords [show l, ":"]
90 else unwords [show l, ":", show (cats st)]
148 else unwords [show l, ":", show (cats st)]
91 if res || isNil (cats st)
149 if res || isNil (cats st)
93− printLn $ unwords [prompt, msg]
151+ putStrLn $ unwords [prompt, msg]
95 else k () st
153 else k () st
@@ -103,7 +161,7 @@ LOG cTy = MkEff (LogRes cTy) Logging
103||| Change the logging level.
161||| Change the logging level.
105||| @l The new logging level.
163||| @l The new logging level.
106−setLoglvl : (Eq a, Show a) => (l : Nat) -> Eff () [LOG a]
164+setLoglvl : (Eq a, Show a) => (l : LogLevel n) -> Eff () [LOG a]
107setLoglvl l = call $ SetLogLvl l
165setLoglvl l = call $ SetLogLvl l
109||| Change the categories to show.
167||| Change the categories to show.
@@ -116,7 +174,7 @@ setLogCats cs = call $ SetLogCats cs
117||| @l The logging level.
175||| @l The logging level.
118||| @cs The categories to show.
176||| @cs The categories to show.
119−initLogger : (Eq a, Show a) => (l : Nat)
177+initLogger : (Eq a, Show a) => (l : LogLevel n)
120 -> (cs : List a)
178 -> (cs : List a)
121 -> Eff () [LOG a]
179 -> Eff () [LOG a]
122initLogger l cs = call $ InitLogger l cs
180initLogger l cs = call $ InitLogger l cs
@@ -126,8 +184,37 @@ initLogger l cs = call $ InitLogger l cs
126||| @l The logging level.
184||| @l The logging level.
127||| @cs The logging categories.
185||| @cs The logging categories.
128||| @m THe message to be logged.
186||| @m THe message to be logged.
129−log : (Show a, Eq a) => (l : Nat)
187+log : (Show a, Eq a) => (l : Nat) -> {auto prf : LTE l 70}
130 -> (cs : List a) -> (m : String) -> Eff () [LOG a]
188 -> (cs : List a) -> (m : String) -> Eff () [LOG a]
131−log l cs msg = call $ Log l cs msg
189+log l cs msg = call $ Log (getProof lvl) cs msg
191+ lvl : (n ** LogLevel n)
192+ lvl = case cast {to=String} (cast {to=Int} l) of
194+ "10" => (_ ** TRACE)
195+ "20" => (_ ** DEBUG)
198+ "50" => (_ ** FATAL)
199+ "60" => (_ ** ERROR)
200+ _ => (_ ** CUSTOM l)
202+trace : (Show a, Eq a) => List a -> String -> Eff () [LOG a]
203+trace cs msg = call $ Log TRACE cs msg
205+debug : (Show a, Eq a) => List a -> String -> Eff () [LOG a]
206+debug cs msg = call $ Log DEBUG cs msg
208+info : (Show a, Eq a) => List a -> String -> Eff () [LOG a]
209+info cs msg = call $ Log INFO cs msg
211+warn : (Show a, Eq a) => List a -> String -> Eff () [LOG a]
212+warn cs msg = call $ Log WARN cs msg
214+fatal : (Show a, Eq a) => List a -> String -> Eff () [LOG a]
215+fatal cs msg = call $ Log FATAL cs msg
217+error : (Show a, Eq a) => List a -> String -> Eff () [LOG a]
218+error cs msg = call $ Log ERROR cs msg
133-- --------------------------------------------------------------------- [ EOF ]
220-- --------------------------------------------------------------------- [ EOF ]