1
1
-- |
2
- -- Provide a code that allows to use structured logging. Here
3
- -- by structured logging we mean a way to add a additional structured
4
- -- data (context) to the log messages, and a tooling that allows keep
5
- -- track of the current context and attach it to all the messages.
2
+ -- Top-level module for structured logging support. Structured logging provides messages in
3
+ -- a machine-readable format and passing an additional user data to the messages (context)
4
+ -- to the log messages, and tooling that allows keep track of the current context and attach
5
+ -- it to all the messages.
6
6
--
7
7
-- Short example:
8
8
--
26
26
-- * @(5)@ extend initial context with a new user data
27
27
--
28
28
-- __NOTE__ You may notice a bit of an extra boilerplate code here. It can be removed
29
- -- by using any effect handling approach, like ReaderT, mtl , various effects
29
+ -- by using any effect handling approach, like ReaderT, MTL , various effects
30
30
-- system or service pattern. However this library does not commit to any of
31
- -- those approaches and provide simple @IO@ interface, so there can be a light
31
+ -- those approaches and provides simple @IO@ interface, so there can be a light
32
32
-- wrapper with the system of your choice. E.g. The library author prefer to use
33
33
-- @ImplicitParams@ extension, as you can see in cheops-logger package.
34
34
--
@@ -71,28 +71,28 @@ import qualified Data.Text as T
71
71
72
72
-- $logger-env
73
73
--
74
- -- For each message we may want to attach an additional information
74
+ -- For each message we attach additional information
75
75
--
76
76
-- * @thread id@ — it can be useful to group messages by the
77
- -- thread when debugging, espeacially in a case if thread
78
- -- can be associated with the request processing.
77
+ -- thread when debugging, especially in a case if a thread
78
+ -- can be associated with the request processing.
79
79
--
80
- -- * @namespace@ — '.' delimited text that describes the componet
81
- -- that the log was emited from. It allows simple logs
82
- -- filtering in external system, or in the logger action.
80
+ -- * @namespace@ — '.' delimited text that describes the component
81
+ -- that the log was emitted from. It allows simple logs
82
+ -- filtering in an external system, or in the logger action.
83
83
--
84
84
-- * @severity@ — information how urgent the message is.
85
85
--
86
- -- * @user_data@ - any user data in a key-value form, key is a
87
- -- text value, and value is an json -encoded value.
86
+ -- * @user_data@ - any user data in a key-value form, the key is a
87
+ -- text value, and the value is a JSON -encoded value.
88
88
--
89
- -- In order to keep track of that information we introduce 'LoggerEnv' handle,
90
- -- we can emit messages using that hadle, see writing logs section ,
91
- -- and modify current context, see adding context section.
89
+ -- In order to keep track of that information we introduce 'LoggerEnv' handle.
90
+ -- It can be used to emit messages with additional information ,
91
+ -- see writing logs section, and modify current context, see adding context section.
92
92
93
- -- | Logger environment, is used to keep information about
93
+ -- | Logger environment is used to keep information about
94
94
-- the current context and modify it. When any log message
95
- -- is emitted the current cotext is added to the message.
95
+ -- is emitted the current context is added to the message.
96
96
data LoggerEnv = LoggerEnv
97
97
{ action :: LogAction IO Message -- ^ Internal log action.
98
98
, context :: Seq. Seq Structured -- ^ Current context.
@@ -102,13 +102,23 @@ data LoggerEnv = LoggerEnv
102
102
emptyLogger :: LoggerEnv
103
103
emptyLogger = LoggerEnv (LogAction $ \ _ -> pure () ) Seq. empty
104
104
105
- -- | Covert ordinary co-log action into 'LoggerEnv' this way
105
+ -- | Covert ordinary colog action into 'LoggerEnv' this way
106
106
-- we can keep track of the current context and modify it.
107
+ --
108
+ -- @
109
+ -- let ctx = 'mkLogger' ('Colog.Json.Action.logToHandle' 'System.IO.stderr')
110
+ -- in 'logDebug' ctx "message"
111
+ -- @
112
+ --
107
113
mkLogger :: LogAction IO Message -> LoggerEnv
108
114
mkLogger action = LoggerEnv action Seq. empty
109
115
110
116
-- | Covert 'LoggerEnv' back into colog 'LogAction', so we can
111
117
-- combie it with the rest of the colog ecosystem.
118
+ --
119
+ -- @
120
+ -- 'Colog.Core.Action.cfilter' (\(sev, _) -> sev > 'Colog.Json.DebugS') 'unLogger' ctx
121
+ -- @
112
122
unLogger :: LoggerEnv -> LogAction IO (Severity , LogStr )
113
123
unLogger (LoggerEnv action st) = LogAction $ \ (lvl, msg) -> do
114
124
tid <- myThreadId
@@ -161,10 +171,11 @@ addNamespace ns LoggerEnv{..} = LoggerEnv{context=context Seq.|> Segment ns, ..}
161
171
-- log concatenation. Currently it uses 'Data.Text.Lazy.Builder' but it's an implementation
162
172
-- detail and may change in the future.
163
173
--
164
- -- 'LogStr' implements 'Data.String.IsString' class, so you can write constant strings
165
- -- without any boilerplate. For other types you should use 'ls' or 'showLS' names are
166
- -- taken from 'Katip' interface.
167
-
174
+ -- 'LogStr' can be created is several ways:
175
+ -- * From the string literal using 'Data.String.IsString' interface
176
+ -- * From the string like data that can be converted to text, using 'ls' function
177
+ -- * From the type that has a 'Show' instance using 'showLS'
178
+ --
168
179
169
180
-- $writing-helpers
170
181
-- Library provides helper for each 'Severity' level.
0 commit comments