You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-- There are other helper functions list `logInfo`, `logNotice`, etc.
101
+
```
102
+
103
+
Now let's discuss `LogStr` type. It exists in order to efficiently generate message without
104
+
extra allocations that can be avoided. Basically it's just a [Text.Builder](http://hackage.haskell.org/package/text-1.2.4.0/docs/Data-Text-Lazy-Builder.html) though iternal
105
+
representation may evolve in the future. `LogStr` allows message concatenation without allocating
106
+
intermediate structures, so it allows efficient log building. For example line:
107
+
108
+
```haskell
109
+
logDebug context $"a"<>"b"<>"c"
110
+
```
111
+
112
+
will write all text string "a", "b", "c" directy to the buffer, and can actually do more optimizations.
113
+
So it's quite safe to use the librart even with a large amount of logs.
114
+
115
+
The 'LogStr' type implement [IsString](http://hackage.haskell.org/package/base-4.14.0.0/docs/Data-String.html#t:IsString) instance it means that you can just write string literals
116
+
and they will be treated as 'LogStr'. To concatenate two 'LogStr' you can use [`<>`](https://hackage.haskell.org/package/base-4.14.0.0/docs/Data-Semigroup.html#t:Semigroup) operation.
117
+
118
+
In addition you can convert any string lines that has [StringConv a T.Text](https://hackage.haskell.org/package/string-conv-0.1.2/docs/Data-String-Conv.html), i.e. can be converted
119
+
to LogStr using `ls :: StringConv a T.Text => a -> LogStr`.
120
+
Efficiency and safety of this convertion depends on the concrete instance implementation and is out
121
+
of control of the package.
122
+
123
+
```haskell
124
+
logDebug context $"foo is "<> ls ("ы"::ByteString)
125
+
```
126
+
127
+
In case your data structure does not implement `StringConv a` it's possible to use not efficient but
128
+
very general `showLS :: Show a => a -> LogStr` method that will work for any type that has Show instance.
129
+
130
+
## Adding context.
131
+
132
+
But just writing json messages is not very interesting, we want to control the context of the message.
133
+
In order to add user data to the context we can use `addContext :: PushContext -> LoggerEnv -> LoggerEnv`
134
+
method.
135
+
136
+
```haskell
137
+
138
+
let context1 = addContext (sl "user_id" (1::Int)) context
139
+
--^ ^ ^ ^ ^
140
+
--| | | | +--- old context
141
+
--| | | +------------ user data
142
+
--| | +---------------------- key for the user data
143
+
--| +---------------------------- function that generates an entry
144
+
-- +------------------------------------------------- new context that has data attached
0 commit comments