0 | ||| Log severity levels and severity-based filtering.
 1 | module Log4Types.Core.Severity
 2 |
 3 | import Log4Types.Core.Action
 4 |
 5 | %default total
 6 |
 7 | ----------------------------------------------------------------------
 8 | -- Severity
 9 | ----------------------------------------------------------------------
10 |
11 | ||| Standard severity levels for log messages.
12 | |||
13 | ||| Ordered from least to most severe: Debug < Info < Warning < Error.
14 | public export
15 | data Severity = Debug | Info | Warning | Error
16 |
17 | public export
18 | Eq Severity where
19 |   Debug   == Debug   = True
20 |   Info    == Info    = True
21 |   Warning == Warning = True
22 |   Error   == Error   = True
23 |   _       == _       = False
24 |
25 | public export
26 | Ord Severity where
27 |   compare x y = compare (toOrd x) (toOrd y)
28 |     where
29 |       toOrd : Severity -> Integer
30 |       toOrd Debug   = 0
31 |       toOrd Info    = 1
32 |       toOrd Warning = 2
33 |       toOrd Error   = 3
34 |
35 | public export
36 | Show Severity where
37 |   show Debug   = "Debug"
38 |   show Info    = "Info"
39 |   show Warning = "Warning"
40 |   show Error   = "Error"
41 |
42 | ----------------------------------------------------------------------
43 | -- Filtering
44 | ----------------------------------------------------------------------
45 |
46 | ||| Only log messages with severity at or above the given threshold.
47 | public export
48 | filterBySeverity : Applicative m
49 |                 => Severity
50 |                 -> (a -> Severity)
51 |                 -> LogAction m a
52 |                 -> LogAction m a
53 | filterBySeverity threshold getSev = cfilter (\a => getSev a >= threshold)
54 |