-
Notifications
You must be signed in to change notification settings - Fork 16
/
winston.node.txt
398 lines (325 loc) · 24.2 KB
/
winston.node.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
n
WINSTON
ALTERNATIVES ==> # - winston (prefer)
# - most high-profile
# - level filtering
# - custom filtering
# - transport abstraction
# - lots of transports
# - log formatting (using logform)
# - timers
# - log search
# - streaming
# - uncaught exception handling
# - graceful shutdown
# - pino (almost prefered):
# - browser/Node
# - fast
# - level filtering
# - transport abstraction
# - log formatting
# - value filtering
# - streaming (separate process) (ndjson)
# - uncaught exception handling
# - graceful shutdown
# - log (prefered with Deno if needs simple)
# - Deno std library
# - signale:
# - log formatting
# - streaming
# - CLI single line updating
# - timers
# - consola
# - level filtering
# - transport abstraction
# - tracer
# - level filtering
# - transport abstraction
# - log formatting
# - log4js
# - level filtering
VERSION ==> #3.13.0
GOAL ==> #Logger:
# - level filtering
# - log message formatting|filtering
# - log storage abstraction
#Also convenient methods to log:
# - timers
# - uncaught exceptions
TRIPLE-BEAM #Module with winston constants
#Version 1.4.1
TRIPLE-BEAM.configs.* #Same as WINSTON.config.*
/=+===============================+=\
/ : : \
)==: INIT :==(
\ :_______________________________: /
\=+===============================+=/
createLogger([OPTS])->LOGGER #LOGGER is a IOSTREAM in objectMode (but should use LOGGER.*() methods)
LOGGER.configure([OPTS]) #Reinitialize LOGGER
Logger #LOGGER class
WINSTON.* #LOGGER singleton (with default OPTS) available directly on WINSTON.*
LOGGER.close() #Close LOGGER's and its TRANSPORT's streams
LOGGER.child([LOGINFO])->LOGGER2 #Creates a child LOGGER2, i.e. forward writes to LOGGER but merges LOGINFO first
#(with low priority).
/=+===============================+=\
/ : : \
)==: CONTAINERS :==(
\ :_______________________________: /
\=+===============================+=/
new Container([OPTS]) #Set of LOGGERs
#Each ID can have several LOGGERs
CONTAINER.add(ID[, OPTS]) #
CONTAINER.get(ID)->LOGGER #
CONTAINER.has(ID)->BOOL #
CONTAINER.close(ID) #Call all LOGGER.close()
loggers #CONTAINER singleton
/=+===============================+=\
/ : : \
)==: LOG :==(
\ :_______________________________: /
\=+===============================+=/
LOGGER.log([LEVEL, ]LOGINFO)
LOGGER.log
(LEVEL, 'MESSAGE'[, LOGINFO]) #
LOGGER.LEVEL(...) #Same as LOGGER.log(LEVEL, ...)
LOGINFO #OBJ:
# - level LEVEL
# - [TRIPLE-BEAM.LEVEL]:
# - same as LOGINFO.level
# - Winston internal code uses [TRIPLE-BEAM.LEVEL] but most transports use LOGINFO.level
# - message 'MESSAGE'
# - [TRIPLE-BEAM.MESSAGE] STR
# - as opposed to LOGINFO.message, it is serialization of full LOGINFO.*
# - set by FORMAT.json|prettyPrint|simple|printf|cli|logstash()
# - any other property
#Those are directly mutated on log argument if it is an OBJ
LOGINFO CONSUMPTION ==> #LOGINFO is consumed by TRANSPORTs in different ways because of transports based on older
#Winston transports:
# - latest version either:
# - log a string, i.e. print LOGINFO[TRIPLE-BEAM.MESSAGE] only:
# - transports: stream (non-ObjectMode), console, file, daily-rotate-file, syslog, fast-file-rotate, newrelic
# - log an object, i.e. print LOGINFO.*:
# - transports: stream (ObjectMode), http
# - older version:
# - print LOGINFO.level|message
# - sometimes also print LOGINFO.meta|splat|[TRIPLE-BEAM.SPLAT]|stack or rest of LOGINFO.* (as metadata)
# - note that latest Winston only sets LOGINFO[TRIPLE-BEAM.SPLAT] not LOGINFO.splat
# - transports: winston-cloudwatch|azuretable|logsene|mail|mongodb|winston-aws-cloudwatch|elasticsearch|redis|logzio|@google-cloud/logging-winston
# - oldest version:
# - use TRANSPORT.log(LEVEL, MESSAGE, META) instead of TRANSPORT.log(LOGINFO)
# - transports: airbrake|dynamodb|firehose|sns|cassandra|spark|cloudant|graylog2|papertrail|pusher|simpledb|sumologic|koa-sse|winlog2|couchdb|loggly|riak
/=+===============================+=\
/ : : \
)==: LEVELS :==(
\ :_______________________________: /
\=+===============================+=/
LOGGER|OPTS|TOPTS.silent #BOOL (def: false)
TRANSPORT.level #LEVEL. Filters logs with lower LEVEL
LOGGER|OPTS|TOPTS.level #STR (def: 'info')
#Default TRANSPORT.level
LOGGER.isLevelEnabled(LEVEL)->BOOL
LOGGER.isLEVELEnabled()->BOOL #Whether some TRANSPORT.level >= LEVEL
LOGGER|OPTS.levels #All valid LEVELs as { LEVEL: NUM, ... }
#Can use either custom one or:
# - config.npm.levels (def): 'error|warn|info|http|verbose|debug|silly'
# - config.cli.levels: 'error|warn|help|data|info|debug|prompt|verbose|input|silly'
# - config.syslog.levels: 'emerg|alert|crit|error|warning|notice|info|debug'
/=+===============================+=\
/ : : \
)==: FORMAT :==(
\ :_______________________________: /
\=+===============================+=/
format.* #Reference to module 'logform' (2.6.0)
LOGGER|OPTS|TOPTS.format #FORMAT
#Def: format.json()
FORMAT #LOGINFO reducer performed on every log. Usually:
# - transform LOGINFO.message|level (padding, colors, prefix, templating, etc.)
# - set some LOGINFO.* (ms|timestamp|splat)
# - serialize LOGINFO.* to LOGINFO[TRIPLE-BEAM.MESSAGE] (meant to be last)
#If reduce to false, silent log.
FORMAT.transform(LOGINFO, FOPTS)
->LOGINFO|false #Method to implement
format
(FUNC(LOGINFO, FOPTS)->LOGINFO)
->FUNC2([FOPTS])->FORMAT #Turn any reducer FUNC into a FORMAT generator
format.combine(FORMAT...)->FORMAT #Turn several FORMAT into one, running them serially
format.json([FOPTS])->FORMAT #LOGINFO[TRIPLE-BEAM.MESSAGE] = JSON.stringify(LOGINFO)
#Uses SAFE-STABLE-STRINGIFY (see its doc)
#FOPTS:
# - replacer|space (arguments to JSON.stringify())
#Default replacer allow stringifying BUFFER (using BUFFER.toString('base64'))
format.prettyPrint([FOPTS]) #LOGINFO[TRIPLE-BEAM.MESSAGE] = UTIL.inspect(LOGINFO, FOPTS)
->FORMAT #Can only use FOPTS.depth|colorize
format.simple()->FORMAT #LOGINFO[TRIPLE-BEAM.MESSAGE] = 'LEVEL: LOGINFO.message JSON_LOGINFO'
# - JSON_LOGINFO is JSON.stringify(LOGINFO) without LOGINFO.level|message
format.cli([FOPTS])->FORMAT #Combines format.simple|colorize|padLevels()
format.logstash()->FORMAT #LOGINFO[TRIPLE-BEAM.MESSAGE] = { @fields LOGINFO, @message LOGINFO.message, @timestamp LOGINFO.timestamp }
format.printf
(FUNC(LOGINFO)->'MESSAG')->FORMAT#LOGINFO[TRIPLE-BEAM.MESSAGE] = 'MESSAGE'
format.errors([FOPTS])->FORMAT #When LOGINFO.message is ERROR (noop otherwise):
# - LOGINFO[TRIPLE-BEAM.MESSAGE]|message = ERROR.message
# - LOGINFO.stack = ERROR.stack (if OPTS.stack true (def: false))
# - LOGINFO.cause = ERROR.cause (if OPTS.cause true (def: false))
format.colorize([FOPTS])->FORMAT #Colorize LOGINFO.level|message
#Color is according to LOGINFO.level
#FOPTS:
# - level BOOL (def: true unless FOPTS.message true): colorize LOGINFO.level
# - message BOOL (def: false): colorize LOGINFO.message
# - all BOOL (def: false): colorize both LOGINFO.level|message
config.LEVEL_TYPE.colors.LEVEL #Default colors
[format.]addColors
({ LEVEL: 'COLOR ...', ... }) #Change default colors. Uses COLORS.js library
format.uncolorize([FOPTS])->FORMAT#Remove colors from LOGINFO.level|message
#FOPTS: level|message BOOL (def: true)
LOGGER.log|LEVEL #Extra SPLAT_ARGS is set to LOGINFO[TRIPLE-BEAM.SPLAT]
(..., ...SPLAT_ARGS) #'MESSAGE' must be present.
format.splat()->FORMAT #LOGINFO.message = UTIL.format(LOGINFO.message, ...SPLAT_ARGS)
#LOGINFO.meta = VAL[_ARR]
# - extra SPLAT_ARGS with no %placeholder Object.assign()'d to LOGINFO.*
# - VAL_ARR if several
# - default to TOPS.defaultMeta
format.align()->FORMAT #Prepend '\t' to LOGINFO.message
format.padLevels([FOPTS])->FORMAT #Left pads LOGINFO.message according to LOGINFO.level width
#FOPTS:
# - levels (def: config.npm.levels)
# - filler STR (def: ' ')
format.timestamp([FOPTS])->FORMAT #LOGINFO.timestamp = 'DATE'
#FOPTS:
# - alias 'PROP': also set LOGINFO.PROP
# - format STR (def: ISO 8601) (passed to FECHA library) or format()->'DATE'
format.ms()->FORMAT #LOGINFO.ms = '+NUM ms|s|m|h' with the time spent since last log (0 if was first log)
format.label([FOPTS])->FORMAT #If FOPTS.message false (def), LOGINFO.label = FOPTS.label
#Otherwise, prepends '[FOPTS.label] ' to LOGINFO.message
format.metadata([FOPTS])->FORMAT #Move LOGINFO.* -> LOGINFO.metadata.* (for backward compatibility with older Winston)
#FOPTS:
# - metadataKey 'PROP' (def: 'metadata')
# - fillExcept 'PROP'_ARR (def: ['level', 'message']): do not move those keys
# - fillWith 'PROP'_ARR: only move those keys
/=+===============================+=\
/ : : \
)==: TIMERS :==(
\ :_______________________________: /
\=+===============================+=/
LOGGER.startTimer()->TIMER #
TIMER.done([LOGINFO]) #Timer (using Date.now())
#Logs LOGINFO with LOGINFO.durationMs NUM
#LOGINFO.level defaults to 'info'
LOGGER.profile(TIMER_ID[,LOGINFO])#Same except:
# - same method for starting and stopping the timer
# - LOGINFO.message defaults to TIMER_ID
/=+===============================+=\
/ : : \
)==: UNCAUGHT EXCEPTIONS :==(
\ :_______________________________: /
\=+===============================+=/
OPTS.exception|rejectionHandlers #Three ways to specify TRANSPORTs performed on
TRANSPORT[_ARR] #process.on('uncaughtException|unhandledRejection', FUNC(ERROR))
#Must perform logging within 3 secs
LOGGER.exceptions|rejections. #LOGINFO:
handle(TRANSPORT) # - error ERROR
TOPTS.handleExceptions|Rejections # - level 'error'
true # - message 'uncaughtException|unhandledRejection: ERROR.message\nERROR.stack'
# - stack ERROR.stack
# - exception true
# - date 'DATE'
# - process OBJ:
# - pid|execPath|version|argv PROCESS.pid|execPath|version|argv
# - uid|gid PROCESS.getuid|getgid()
# - cwd PROCESS.cwd()
# - memoryUsage PROCESS.memoryUsage()
# - os OBJ:
# - loadavg|uptime: OS.loadavg|uptime()
# - trace OBJ_ARR:
# - column|file|function|line|method|native
# - first parse ERROR.stack with NODE-STACK-TRACE (see its doc)
# - then CALL.getColumnNumber|FileName|FunctionName|LineNumber|MethodName|isNative()
LOGGER.exceptions|rejections.
unhandle() #Removes 'uncaughtException|unhandledRejection' handler
LOGGER|OPTS.exitOnError #BOOL[(ERROR)]
#If true (def), calls process.exit(1) on 'uncaughtException|unhandledRejection' ERROR
/=+===============================+=\
/ : : \
)==: TRANSPORT CORE :==(
\ :_______________________________: /
\=+===============================+=/
LOGGER|OPTS.transports #TRANSPORT[_ARR]
#Calls LOGGER.add(TRANSPORT)
LOGGER.add(TRANSPORT) #Add TRANSPORT, i.e. pipe LOGGER's stream to TRANSPORT
LOGGER.remove(TRANSPORT) #
LOGGER.clear() #Remove all TRANSPORTs
LOGGER.on('error|warn',
FUNC(ERROR, TRANSPORT)) #Forwarded events from TRANSPORT
LOGGER.stream([SOPTS])->STREAM #Aggregate all TRANSPORTs into single IOSTREAM with events:
# - log(LOGINFO): with LOGINFO.transports 'TRANSPORT'_ARR
# - error(ERROR): with ERROR.transports 'TRANSPORT'_ARR
#Only for TRANSPORTs that implement following method
TRANSPORT.stream([SOPTS])->STREAM #
LOGGER.query #Retrieve logs.
([QOPTS, ]FUNC(ERROR, OBJ)) #Only for transports that support the following methods, which are called in series.
#OBJ result is { ['TRANSPORT']: ARR, ... }
TRANSPORT.formatQuery
(QOPTS.query)->QOPTS.query #Def QOPTS.query: {}
TRANSPORT.query
(QOPTS, FUNC(ERROR, ARR)) #
TRANSPORT.formatResults
(ARR, QOPTS.format)->ARR #
TOPTS.highWaterMark #Passed to OSTREAM
/=+===============================+=\
/ : : \
)==: CUSTOM TRANSPORT :==(
\ :_______________________________: /
\=+===============================+=/
Transport #Base class from 'winston-transport' (4.7.0) to inherit from
#OSTREAM in objectMode
#Following methods should be defined by TRANSPORT, but not called directly by users
TRANSPORT.name #'TRANSPORT'
TRANSPORT.log #Called after writing LOGINFO on OSTREAM.
(LOGINFO, FUNC([ERROR])) #Performed after level filtering and FORMAT applied
TRANSPORT.logv
(LOGINFO_ARR, FUNC([ERROR])) #Same but for several LOGINFO at once, and is optional
TRANSPORT.close() #Called when TRANSPORT is removed
TOPTS #TRANSPORT constructor options. Some are common to all transports
/=+===============================+=\
/ : : \
)==: TRANSPORTS :==(
\ :_______________________________: /
\=+===============================+=/
new transports.Stream([TOPTS]) #TRANSPORT that pipes to OPTS.stream OSTREAM
#If OSTREAM is not in objectMode, pipes LOGINFO.message instead of LOGINFO
new transports.Console([TOPTS]) #TRANSPORT that calls console.log(LOGINFO.message)
#TOPTS:
# - stderrLevels|consoleWarnLevels LEVEL_ARR: call console.error|warn() instead for those levels
# - eol (def: OS.EOL)
new transports.Http([TOPTS]) #TRANSPORT that make a POST HTTP request (using core HTTP module) with JSON payload LOGINFO
#TOPTS:
# - ssl BOOL (def: false)
# - host STR (def: 'localhost')
# - port NUM (def: 80|443)
# - auth.username|password STR (can also use LOGINFO.auth)
# - path STR (def: '') (can also use LOGINFO.path)
# - headers OBJ
# - agent AGENT
# - batch BOOL (def: false): batch requests at regular intervals or after a max count
# - batchInterval NUM (def: 5000, in ms)
# - batchCount NUM (def: 10)
# - replacer FUNC: used with JSON.stringify(VAL, FUNC)
# - maximumDepth NUM (def: none): max OBJ|ARR depth (replaced with '[Object]|[Array]' otherwise)
#Emit 'warn' event if issue or if status code not 200
#Has TRANSPORT.query|stream()
# - payload is then { method: 'query|stream', params LOGINFO }
OTHERS ==> #Only document them when needed:
# - File, DailyRotateFile, FastFileRotate
# - Mail
# - Slack, Telegram
# - SSE (Koa), TCP
# - Syslog, Windows events
# - ElasticSearch
# - Logsene, logz.io
# - MongoDB, CouchDB, Redis, Riak, InfluxDB, Cassandra, Kafka, IBM Cloudant, AWS SimpleDB, PostgreSQL, SQLite3
# - Airbrake, Graylog, New Relic, Sumo Logic, Loggly, Datadog, Sentry, Rollbar, Humio, logDNA, papertrail
# - Pusher
# - AWS Cloudwatch, AWS kinesis firehose, AWS SNS, Google Stackdriver logging Azure Table, AWS DynamoDB
# - Google BigQuery
# - others