0 | ||| Structured message types for application logging.
 1 | module Log4Types.Message
 2 |
 3 | import Log4Types.Core
 4 | import Log4Types.Format
 5 |
 6 | %default total
 7 |
 8 | ----------------------------------------------------------------------
 9 | -- SimpleMsg
10 | ----------------------------------------------------------------------
11 |
12 | ||| A simple log message with severity and text, no structured fields.
13 | public export
14 | record SimpleMsg where
15 |   constructor MkSimpleMsg
16 |   severity : Severity
17 |   text     : String
18 |
19 | public export
20 | Show SimpleMsg where
21 |   show msg = fmtSeverity msg.severity ++ " " ++ msg.text
22 |
23 | public export
24 | Loggable SimpleMsg where
25 |   logFields renderer msg acc =
26 |     renderer.addField "severity" (StrVal $ show msg.severity) $
27 |     renderer.addField "message" (StrVal msg.text) acc
28 |   logShow = show
29 |
30 | ----------------------------------------------------------------------
31 | -- Msg
32 | ----------------------------------------------------------------------
33 |
34 | ||| A structured log message with severity, text, and key-value fields.
35 | public export
36 | record Msg where
37 |   constructor MkMsg
38 |   severity : Severity
39 |   text     : String
40 |   fields   : List (String, LogParamValue)
41 |
42 | ||| Render a Msg as a single line: the severity tag `fmtSev` produces, the
43 | ||| text, then any fields as `key=value` pairs. `show` uses `fmtSeverity`.
44 | public export
45 | fmtMsgWith : (fmtSev : Severity -> String) -> Msg -> String
46 | fmtMsgWith fmtSev msg =
47 |   let line = fmtSev msg.severity ++ " " ++ msg.text
48 |   in if null msg.fields then line else line ++ " " ++ fmtFields msg.fields
49 |
50 | public export
51 | Show Msg where
52 |   show = fmtMsgWith fmtSeverity
53 |
54 | public export
55 | Loggable Msg where
56 |   logFields renderer msg acc =
57 |     let acc' = renderer.addField "severity" (StrVal $ show msg.severity) $
58 |                renderer.addField "message" (StrVal msg.text) acc
59 |     in foldl (\a, (k, v) => renderer.addField k v a) acc' msg.fields
60 |   logShow = show
61 |
62 | ----------------------------------------------------------------------
63 | -- Convenience constructors
64 | ----------------------------------------------------------------------
65 |
66 | ||| Create a Debug message with no structured fields.
67 | public export
68 | mkDebug : String -> Msg
69 | mkDebug t = MkMsg Debug t []
70 |
71 | ||| Create an Info message with no structured fields.
72 | public export
73 | mkInfo : String -> Msg
74 | mkInfo t = MkMsg Info t []
75 |
76 | ||| Create a Warning message with no structured fields.
77 | public export
78 | mkWarning : String -> Msg
79 | mkWarning t = MkMsg Warning t []
80 |
81 | ||| Create an Error message with no structured fields.
82 | public export
83 | mkError : String -> Msg
84 | mkError t = MkMsg Error t []
85 |