0 | ||| Human-readable text formatting for log output.
 1 | module Log4Types.Format
 2 |
 3 | import Data.String
 4 | import Log4Types.Core
 5 |
 6 | %default total
 7 |
 8 | ||| A LogRenderer that flattens a value into a list of fields.
 9 | |||
10 | ||| Fields keep the order they are added in, and every field of a nested
11 | ||| object carries the object's name as a dotted prefix, as in
12 | ||| `customer.address.city`. The list has the shape of `Msg.fields`;
13 | ||| `fmtFields` renders it as text.
14 | public export
15 | fieldsRenderer : LogRenderer (List (String, LogParamValue))
16 | fieldsRenderer = MkLogRenderer
17 |   { addField  = \name, val, acc => acc ++ [(name, val)]
18 |   , addNested = \name, build, acc =>
19 |       acc ++ map (\(k, v) => (name ++ "." ++ k, v)) (build [])
20 |   , empty     = []
21 |   , combine   = (++)
22 |   }
23 |
24 | ||| Render fields as space-separated `key=value` pairs.
25 | |||
26 | ||| ```idris
27 | ||| fmtFields [("user", StrVal "alice"), ("attempt", IntVal 2)] == "user=\"alice\" attempt=2"
28 | ||| ```
29 | public export
30 | fmtFields : List (String, LogParamValue) -> String
31 | fmtFields = unwords . map (\(k, v) => k ++ "=" ++ show v)
32 |
33 | ||| Format a severity level as a bracketed tag.
34 | |||
35 | ||| ```idris
36 | ||| fmtSeverity Info == "[INFO]"
37 | ||| ```
38 | public export
39 | fmtSeverity : Severity -> String
40 | fmtSeverity Debug   = "[DEBUG]"
41 | fmtSeverity Info    = "[INFO]"
42 | fmtSeverity Warning = "[WARNING]"
43 | fmtSeverity Error   = "[ERROR]"
44 |
45 | ||| Compose a formatting function with a string log action.
46 | |||
47 | ||| This is `cmap` specialised for the common pattern of
48 | ||| formatting a message then sending it to a string-based logger.
49 | public export
50 | formatWith : (msg -> String) -> LogAction m String -> LogAction m msg
51 | formatWith = cmap
52 |