0 | ||| Scoped structured context for enriching log messages.
 1 | |||
 2 | ||| A `Context` is a list of named fields that can be attached to every
 3 | ||| log message within a scope. Contexts are pure values that compose
 4 | ||| by appending.
 5 | module Log4Types.Context
 6 |
 7 | import Log4Types.Core
 8 | import Log4Types.Message
 9 |
10 | %default total
11 |
12 | ----------------------------------------------------------------------
13 | -- Context type
14 | ----------------------------------------------------------------------
15 |
16 | ||| A structured context: a list of named fields.
17 | public export
18 | Context : Type
19 | Context = List (String, LogParamValue)
20 |
21 | ||| The empty context.
22 | public export
23 | emptyContext : Context
24 | emptyContext = []
25 |
26 | ----------------------------------------------------------------------
27 | -- Building context
28 | ----------------------------------------------------------------------
29 |
30 | ||| Add a primitive field to a context.
31 | public export
32 | addField : String -> LogParamValue -> Context -> Context
33 | addField name val ctx = ctx ++ [(name, val)]
34 |
35 | ||| Add a string field to a context.
36 | public export
37 | addStr : String -> String -> Context -> Context
38 | addStr name val = addField name (StrVal val)
39 |
40 | ||| Add an integer field to a context.
41 | public export
42 | addInt : String -> Integer -> Context -> Context
43 | addInt name val = addField name (IntVal val)
44 |
45 | ||| Add a boolean field to a context.
46 | public export
47 | addBool : String -> Bool -> Context -> Context
48 | addBool name val = addField name (BoolVal val)
49 |
50 | ||| Add a namespace segment to a context.
51 | |||
52 | ||| Namespaces are stored as string fields with the key "namespace".
53 | public export
54 | addNamespace : String -> Context -> Context
55 | addNamespace ns = addField "namespace" (StrVal ns)
56 |
57 | ----------------------------------------------------------------------
58 | -- Enriching log actions
59 | ----------------------------------------------------------------------
60 |
61 | ||| Enrich a Msg log action by prepending context fields to every message.
62 | public export
63 | withContext : Context -> LogAction m Msg -> LogAction m Msg
64 | withContext ctx = cmap (\msg => { fields $= (ctx ++) } msg)
65 |