24
24
module Cheops.Logger
25
25
( -- * Initialization
26
26
withLogger
27
- , emptyLogger
28
27
, defCapacity
29
28
, LoggerConfig (.. )
30
- , LoggerEnv
31
- -- * High level
32
- -- ** Writing logs
33
- -- $writing-logs
34
- , ls
35
- , showLS
36
- , LogStr (.. )
37
- -- $writing-helpers
38
- , logDebug
39
- , logNotice
40
- , logInfo
41
- , logErr
42
- , logWarn
43
- , logAlert
44
- , logCrit
45
- , logEmergency
46
- , Severity (.. )
47
- -- ** Adding context
48
- -- $adding-context
49
- , addContext
50
- , addNamespace
51
- , sl
52
- -- * Low level
53
- , PushContext
54
- , logSay
29
+ , module Cheops.Logger.Structured
55
30
-- * Metrics support
56
31
-- $metrics-support
57
32
-- $setup
58
33
) where
59
34
60
- import Cheops.Logger.Internal. Metrics (flushLog , submitLog )
35
+ import Cheops.Logger.Metrics (flushLog , submitLog )
61
36
import Cheops.Logger.Internal.Structured
62
37
import Cheops.Logger.Internal.Writer
38
+ import Cheops.Logger.Structured
63
39
import Colog.Concurrent
64
40
import Colog.Concurrent.Internal
65
41
import Colog.Core hiding (Severity , Info )
66
- import Control.Concurrent
67
42
import Data.Aeson
68
43
import System.IO
69
- import qualified Data.Sequence as Seq
70
- import qualified Data.Text as T
71
44
72
45
-- | Logger configuration
73
46
data LoggerConfig = LoggerConfig
@@ -87,13 +60,6 @@ instance FromJSON LoggerConfig where
87
60
n <- o .:? " disabled"
88
61
pure $ LoggerConfig defCapacity n
89
62
90
- -- | Logger environment, keeps internal Katip environment,
91
- -- current contexts (metadata), namespaces and minimal interesting
92
- -- severity and verbosity.
93
- data LoggerEnv = LoggerEnv
94
- { action :: LogAction IO Message
95
- , context :: Seq. Seq Structured
96
- }
97
63
98
64
-- | Create logger and initialize it with defaults.
99
65
withLogger
@@ -108,90 +74,11 @@ withLogger LoggerConfig{..} f
108
74
(LogAction $ flushLog . feed)
109
75
(hFlush stdout)
110
76
$ \ (LogAction log_action) ->
111
- f $ LoggerEnv
112
- (LogAction $ submitLog . log_action)
113
- Seq. Empty
77
+ f $ mkLogger (LogAction $ submitLog . log_action)
114
78
where
115
79
LogAction feed = feedHandle stdout
116
80
117
- -- | Internal logger function.
118
- logSay :: LoggerEnv -- ^ Logger handle.
119
- -> Severity -- ^ Message severity.
120
- -> LogStr -- ^ Message itself.
121
- -> IO ()
122
- logSay (LoggerEnv action context) lvl msg = do
123
- tid <- myThreadId
124
- unLogAction action $ Message lvl (mkThreadId tid) context msg
125
-
126
- -- | Logger that does nothing. Useful for the testing purpose.
127
- emptyLogger :: LoggerEnv
128
- emptyLogger = LoggerEnv (LogAction $ \ _ -> pure () ) Seq. empty
129
-
130
- -- $adding-context
131
- --
132
- -- Messages in the library forms a stack, and you can attach 2 kinds of data to it:
133
- --
134
- -- 1. @namespace@ - a list of locations, that allows to tell that the component
135
- -- is it.
136
- -- 2. @context@ - context is a list of key-value pairs, where key is a text and
137
- -- a value is any JSON value.
138
- --
139
- -- When you attach that information to the 'LoggerContext' it will be added to
140
- -- each message that is written in that context. Allowing to analyze data in the
141
- -- external systems.
142
- --
143
-
144
- -- | Helper to update context, by appending another item to the log.
145
- --
146
- -- @
147
- -- local ('addContext' ('sl' "key" "value")) $ do
148
- -- ...
149
- -- @
150
- addContext
151
- :: PushContext -- ^ New data to store
152
- -> LoggerEnv -- ^ Old context.
153
- -> LoggerEnv
154
- addContext (PushContext f) LoggerEnv {.. } = LoggerEnv {context = f context, .. }
155
-
156
- -- | Helper to extend current namespace by appending sub-namespace.
157
- --
158
- -- @
159
- -- local ('addNamespace' "subcomponent") $ do
160
- -- ...
161
- -- @
162
- addNamespace :: T. Text -> LoggerEnv -> LoggerEnv
163
- addNamespace ns LoggerEnv {.. } = LoggerEnv {context= context Seq. |> Segment ns, .. }
164
-
165
- -- $writing-logs
166
- -- Logs can we written using one of the following helpers. The general pattern is
167
- --
168
- -- @
169
- -- 'logDebug' logger "message"
170
- -- @
171
- --
172
- -- Messages has type 'LogStr'. This is an abstraction over a data-type for efficient
173
- -- log concatenation. Currently it uses 'Data.Text.Lazy.Builder' but it's an implementation
174
- -- detail and may change in the future.
175
- --
176
- -- 'LogStr' implements 'Data.String.IsString' class, so you can write constant strings
177
- -- without any boilerplate. For other types you should use 'ls' or 'showLS' names are
178
- -- taken from 'Katip' interface.
179
-
180
-
181
- -- $writing-helpers
182
- -- Library provides helper for each 'Severity' level.
183
81
184
- logDebug, logNotice, logInfo, logWarn,
185
- logErr, logAlert, logCrit, logEmergency
186
- :: LoggerEnv -> LogStr -> IO ()
187
- logDebug x = logSay x DebugS
188
- logNotice x = logSay x InfoS
189
- logInfo x = logSay x InfoS
190
- logWarn x = logSay x WarningS
191
- logErr x = logSay x ErrorS
192
- logCrit x = logSay x CriticalS
193
- logAlert x = logSay x AlertS
194
- logEmergency x = logSay x EmergencyS
195
82
196
83
197
84
-- $metrics-support
0 commit comments