0 | ||| Human-readable text formatting for log output.
 1 | module Log4Types.Format
 2 |
 3 | import Log4Types.Core
 4 |
 5 | %default total
 6 |
 7 | ||| A LogRenderer that builds key=value text output.
 8 | |||
 9 | ||| Fields are rendered as `key=value` pairs separated by spaces.
10 | ||| Nested objects are rendered as `key.subkey=value`.
11 | public export
12 | textRenderer : LogRenderer String
13 | textRenderer = MkLogRenderer
14 |   { addField  = \name, val, acc =>
15 |       let field = name ++ "=" ++ show val
16 |       in if acc == "" then field else acc ++ " " ++ field
17 |   , addNested = \name, build, acc =>
18 |       let nested = build ""
19 |           prefixed = name ++ "." ++ nested
20 |       in if acc == "" then prefixed else acc ++ " " ++ prefixed
21 |   , empty     = ""
22 |   , combine   = \a, b =>
23 |       if a == "" then b
24 |       else if b == "" then a
25 |       else a ++ " " ++ b
26 |   }
27 |
28 | ||| Format a severity level as a bracketed tag.
29 | |||
30 | ||| ```idris
31 | ||| fmtSeverity Info == "[INFO]"
32 | ||| ```
33 | public export
34 | fmtSeverity : Severity -> String
35 | fmtSeverity Debug   = "[DEBUG]"
36 | fmtSeverity Info    = "[INFO]"
37 | fmtSeverity Warning = "[WARNING]"
38 | fmtSeverity Error   = "[ERROR]"
39 |
40 | ||| Compose a formatting function with a string log action.
41 | |||
42 | ||| This is `cmap` specialised for the common pattern of
43 | ||| formatting a message then sending it to a string-based logger.
44 | public export
45 | formatWith : (msg -> String) -> LogAction m String -> LogAction m msg
46 | formatWith = cmap
47 |