react-hook-eslint 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTING.md +30 -0
- package/LICENSE +21 -0
- package/README.md +85 -0
- package/SECURITY.md +68 -0
- package/docs/api.md +1487 -0
- package/docs/asynchronous.md +40 -0
- package/docs/benchmarks.md +55 -0
- package/docs/browser.md +227 -0
- package/docs/bundling.md +40 -0
- package/docs/child-loggers.md +95 -0
- package/docs/ecosystem.md +84 -0
- package/docs/help.md +345 -0
- package/docs/lts.md +64 -0
- package/docs/pretty.md +35 -0
- package/docs/redaction.md +135 -0
- package/docs/transports.md +1238 -0
- package/docs/web.md +269 -0
- package/docsify/sidebar.md +26 -0
- package/favicon-16x16.png +0 -0
- package/favicon-32x32.png +0 -0
- package/favicon.ico +0 -0
- package/file.js +12 -0
- package/index.html +55 -0
- package/lib/caller.js +30 -0
- package/lib/constants.js +28 -0
- package/lib/deprecations.js +8 -0
- package/lib/levels.js +241 -0
- package/lib/meta.js +3 -0
- package/lib/multistream.js +188 -0
- package/lib/proto.js +234 -0
- package/lib/redaction.js +118 -0
- package/lib/symbols.js +74 -0
- package/lib/time.js +11 -0
- package/lib/tools.js +394 -0
- package/lib/transport-stream.js +56 -0
- package/lib/transport.js +167 -0
- package/lib/worker.js +194 -0
- package/lib/writer.js +44 -0
- package/package.json +38 -0
- package/pino-banner.png +0 -0
- package/pino-logo-hire.png +0 -0
- package/pino-tree.png +0 -0
- package/pino.d.ts +889 -0
- package/pino.js +38 -0
- package/pretty-demo.png +0 -0
- package/tsconfig.json +14 -0
package/docs/web.md
ADDED
@@ -0,0 +1,269 @@
|
|
1
|
+
# Web Frameworks
|
2
|
+
|
3
|
+
Since HTTP logging is a primary use case, Pino has first-class support for the Node.js
|
4
|
+
web framework ecosystem.
|
5
|
+
|
6
|
+
- [Web Frameworks](#web-frameworks)
|
7
|
+
- [Pino with Fastify](#pino-with-fastify)
|
8
|
+
- [Pino with Express](#pino-with-express)
|
9
|
+
- [Pino with Hapi](#pino-with-hapi)
|
10
|
+
- [Pino with Restify](#pino-with-restify)
|
11
|
+
- [Pino with Koa](#pino-with-koa)
|
12
|
+
- [Pino with Node core `http`](#pino-with-node-core-http)
|
13
|
+
- [Pino with Nest](#pino-with-nest)
|
14
|
+
- [Pino with H3](#pino-with-h3)
|
15
|
+
|
16
|
+
<a id="fastify"></a>
|
17
|
+
## Pino with Fastify
|
18
|
+
|
19
|
+
The Fastify web framework comes bundled with Pino by default, simply set Fastify's
|
20
|
+
`logger` option to `true` and use `request.log` or `reply.log` for log messages that correspond
|
21
|
+
to each request:
|
22
|
+
|
23
|
+
```js
|
24
|
+
const fastify = require('fastify')({
|
25
|
+
logger: true
|
26
|
+
})
|
27
|
+
|
28
|
+
fastify.get('/', async (request, reply) => {
|
29
|
+
request.log.info('something')
|
30
|
+
return { hello: 'world' }
|
31
|
+
})
|
32
|
+
|
33
|
+
fastify.listen({ port: 3000 }, (err) => {
|
34
|
+
if (err) {
|
35
|
+
fastify.log.error(err)
|
36
|
+
process.exit(1)
|
37
|
+
}
|
38
|
+
})
|
39
|
+
```
|
40
|
+
|
41
|
+
The `logger` option can also be set to an object, which will be passed through directly
|
42
|
+
as the [`pino` options object](/docs/api.md#options-object).
|
43
|
+
|
44
|
+
See the [fastify documentation](https://www.fastify.io/docs/latest/Reference/Logging/) for more information.
|
45
|
+
|
46
|
+
<a id="express"></a>
|
47
|
+
## Pino with Express
|
48
|
+
|
49
|
+
```sh
|
50
|
+
npm install pino-http
|
51
|
+
```
|
52
|
+
|
53
|
+
```js
|
54
|
+
const app = require('express')()
|
55
|
+
const pino = require('pino-http')()
|
56
|
+
|
57
|
+
app.use(pino)
|
58
|
+
|
59
|
+
app.get('/', function (req, res) {
|
60
|
+
req.log.info('something')
|
61
|
+
res.send('hello world')
|
62
|
+
})
|
63
|
+
|
64
|
+
app.listen(3000)
|
65
|
+
```
|
66
|
+
|
67
|
+
See the [pino-http README](https://npm.im/pino-http) for more info.
|
68
|
+
|
69
|
+
<a id="hapi"></a>
|
70
|
+
## Pino with Hapi
|
71
|
+
|
72
|
+
```sh
|
73
|
+
npm install hapi-pino
|
74
|
+
```
|
75
|
+
|
76
|
+
```js
|
77
|
+
'use strict'
|
78
|
+
|
79
|
+
const Hapi = require('@hapi/hapi')
|
80
|
+
const Pino = require('hapi-pino');
|
81
|
+
|
82
|
+
async function start () {
|
83
|
+
// Create a server with a host and port
|
84
|
+
const server = Hapi.server({
|
85
|
+
host: 'localhost',
|
86
|
+
port: 3000
|
87
|
+
})
|
88
|
+
|
89
|
+
// Add the route
|
90
|
+
server.route({
|
91
|
+
method: 'GET',
|
92
|
+
path: '/',
|
93
|
+
handler: async function (request, h) {
|
94
|
+
// request.log is HAPI's standard way of logging
|
95
|
+
request.log(['a', 'b'], 'Request into hello world')
|
96
|
+
|
97
|
+
// a pino instance can also be used, which will be faster
|
98
|
+
request.logger.info('In handler %s', request.path)
|
99
|
+
|
100
|
+
return 'hello world'
|
101
|
+
}
|
102
|
+
})
|
103
|
+
|
104
|
+
await server.register(Pino)
|
105
|
+
|
106
|
+
// also as a decorated API
|
107
|
+
server.logger.info('another way for accessing it')
|
108
|
+
|
109
|
+
// and through Hapi standard logging system
|
110
|
+
server.log(['subsystem'], 'third way for accessing it')
|
111
|
+
|
112
|
+
await server.start()
|
113
|
+
|
114
|
+
return server
|
115
|
+
}
|
116
|
+
|
117
|
+
start().catch((err) => {
|
118
|
+
console.log(err)
|
119
|
+
process.exit(1)
|
120
|
+
})
|
121
|
+
```
|
122
|
+
|
123
|
+
See the [hapi-pino README](https://npm.im/hapi-pino) for more info.
|
124
|
+
|
125
|
+
<a id="restify"></a>
|
126
|
+
## Pino with Restify
|
127
|
+
|
128
|
+
```sh
|
129
|
+
npm install restify-pino-logger
|
130
|
+
```
|
131
|
+
|
132
|
+
```js
|
133
|
+
const server = require('restify').createServer({name: 'server'})
|
134
|
+
const pino = require('restify-pino-logger')()
|
135
|
+
|
136
|
+
server.use(pino)
|
137
|
+
|
138
|
+
server.get('/', function (req, res) {
|
139
|
+
req.log.info('something')
|
140
|
+
res.send('hello world')
|
141
|
+
})
|
142
|
+
|
143
|
+
server.listen(3000)
|
144
|
+
```
|
145
|
+
|
146
|
+
See the [restify-pino-logger README](https://npm.im/restify-pino-logger) for more info.
|
147
|
+
|
148
|
+
<a id="koa"></a>
|
149
|
+
## Pino with Koa
|
150
|
+
|
151
|
+
```sh
|
152
|
+
npm install koa-pino-logger
|
153
|
+
```
|
154
|
+
|
155
|
+
```js
|
156
|
+
const Koa = require('koa')
|
157
|
+
const app = new Koa()
|
158
|
+
const pino = require('koa-pino-logger')()
|
159
|
+
|
160
|
+
app.use(pino)
|
161
|
+
|
162
|
+
app.use((ctx) => {
|
163
|
+
ctx.log.info('something else')
|
164
|
+
ctx.body = 'hello world'
|
165
|
+
})
|
166
|
+
|
167
|
+
app.listen(3000)
|
168
|
+
```
|
169
|
+
|
170
|
+
See the [koa-pino-logger README](https://github.com/pinojs/koa-pino-logger) for more info.
|
171
|
+
|
172
|
+
<a id="http"></a>
|
173
|
+
## Pino with Node core `http`
|
174
|
+
|
175
|
+
```sh
|
176
|
+
npm install pino-http
|
177
|
+
```
|
178
|
+
|
179
|
+
```js
|
180
|
+
const http = require('http')
|
181
|
+
const server = http.createServer(handle)
|
182
|
+
const logger = require('pino-http')()
|
183
|
+
|
184
|
+
function handle (req, res) {
|
185
|
+
logger(req, res)
|
186
|
+
req.log.info('something else')
|
187
|
+
res.end('hello world')
|
188
|
+
}
|
189
|
+
|
190
|
+
server.listen(3000)
|
191
|
+
```
|
192
|
+
|
193
|
+
See the [pino-http README](https://npm.im/pino-http) for more info.
|
194
|
+
|
195
|
+
|
196
|
+
<a id="nest"></a>
|
197
|
+
## Pino with Nest
|
198
|
+
|
199
|
+
```sh
|
200
|
+
npm install nestjs-pino
|
201
|
+
```
|
202
|
+
|
203
|
+
```ts
|
204
|
+
import { NestFactory } from '@nestjs/core'
|
205
|
+
import { Controller, Get, Module } from '@nestjs/common'
|
206
|
+
import { LoggerModule, Logger } from 'nestjs-pino'
|
207
|
+
|
208
|
+
@Controller()
|
209
|
+
export class AppController {
|
210
|
+
constructor(private readonly logger: Logger) {}
|
211
|
+
|
212
|
+
@Get()
|
213
|
+
getHello() {
|
214
|
+
this.logger.log('something')
|
215
|
+
return `Hello world`
|
216
|
+
}
|
217
|
+
}
|
218
|
+
|
219
|
+
@Module({
|
220
|
+
controllers: [AppController],
|
221
|
+
imports: [LoggerModule.forRoot()]
|
222
|
+
})
|
223
|
+
class MyModule {}
|
224
|
+
|
225
|
+
async function bootstrap() {
|
226
|
+
const app = await NestFactory.create(MyModule)
|
227
|
+
await app.listen(3000)
|
228
|
+
}
|
229
|
+
bootstrap()
|
230
|
+
```
|
231
|
+
|
232
|
+
See the [nestjs-pino README](https://npm.im/nestjs-pino) for more info.
|
233
|
+
|
234
|
+
|
235
|
+
<a id="h3"></a>
|
236
|
+
## Pino with H3
|
237
|
+
|
238
|
+
```sh
|
239
|
+
npm install pino-http h3
|
240
|
+
```
|
241
|
+
|
242
|
+
Save as `server.mjs`:
|
243
|
+
|
244
|
+
```js
|
245
|
+
import { createApp, createRouter, eventHandler, fromNodeMiddleware } from "h3";
|
246
|
+
import pino from 'pino-http'
|
247
|
+
|
248
|
+
export const app = createApp();
|
249
|
+
|
250
|
+
const router = createRouter();
|
251
|
+
app.use(router);
|
252
|
+
app.use(fromNodeMiddleware(pino()))
|
253
|
+
|
254
|
+
app.use(eventHandler((event) => {
|
255
|
+
event.node.req.log.info('something')
|
256
|
+
return 'hello world'
|
257
|
+
}))
|
258
|
+
|
259
|
+
router.get(
|
260
|
+
"/",
|
261
|
+
eventHandler((event) => {
|
262
|
+
return { path: event.path, message: "Hello World!" };
|
263
|
+
}),
|
264
|
+
);
|
265
|
+
```
|
266
|
+
|
267
|
+
Execute `npx --yes listhen -w --open ./server.mjs`.
|
268
|
+
|
269
|
+
See the [pino-http README](https://npm.im/pino-http) for more info.
|
@@ -0,0 +1,26 @@
|
|
1
|
+
* [Readme](/)
|
2
|
+
* [API](/docs/api.md)
|
3
|
+
* [Browser API](/docs/browser.md)
|
4
|
+
* [Redaction](/docs/redaction.md)
|
5
|
+
* [Child Loggers](/docs/child-loggers.md)
|
6
|
+
* [Transports](/docs/transports.md)
|
7
|
+
* [Web Frameworks](/docs/web.md)
|
8
|
+
* [Pretty Printing](/docs/pretty.md)
|
9
|
+
* [Asynchronous Logging](/docs/asynchronous.md)
|
10
|
+
* [Ecosystem](/docs/ecosystem.md)
|
11
|
+
* [Benchmarks](/docs/benchmarks.md)
|
12
|
+
* [Long Term Support](/docs/lts.md)
|
13
|
+
* [Help](/docs/help.md)
|
14
|
+
* [Log rotation](/docs/help.md#rotate)
|
15
|
+
* [Reopening log files](/docs/help.md#reopening)
|
16
|
+
* [Saving to multiple files](/docs/help.md#multiple)
|
17
|
+
* [Log filtering](/docs/help.md#filter-logs)
|
18
|
+
* [Transports and systemd](/docs/help.md#transport-systemd)
|
19
|
+
* [Duplicate keys](/docs/help.md#dupe-keys)
|
20
|
+
* [Log levels as labels instead of numbers](/docs/help.md#level-string)
|
21
|
+
* [Pino with `debug`](/docs/help.md#debug)
|
22
|
+
* [Unicode and Windows terminal](/docs/help.md#windows)
|
23
|
+
* [Mapping Pino Log Levels to Google Cloud Logging (Stackdriver) Severity Levels](/docs/help.md#stackdriver)
|
24
|
+
* [Avoid Message Conflict](/docs/help.md#avoid-message-conflict)
|
25
|
+
* [Best performance for logging to `stdout`](/docs/help.md#best-performance-for-stdout)
|
26
|
+
* [Testing](/docs/help.md#testing)
|
Binary file
|
Binary file
|
package/favicon.ico
ADDED
Binary file
|
package/file.js
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
'use strict'
|
2
|
+
|
3
|
+
const pino = require('./pino')
|
4
|
+
const { once } = require('node:events')
|
5
|
+
|
6
|
+
module.exports = async function (opts = {}) {
|
7
|
+
const destOpts = Object.assign({}, opts, { dest: opts.destination || 1, sync: false })
|
8
|
+
delete destOpts.destination
|
9
|
+
const destination = pino.destination(destOpts)
|
10
|
+
await once(destination, 'ready')
|
11
|
+
return destination
|
12
|
+
}
|
package/index.html
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html lang="en">
|
3
|
+
<head>
|
4
|
+
<meta charset="UTF-8">
|
5
|
+
<title>Pino - Super fast, all natural JSON logger for Node.js</title>
|
6
|
+
<meta name="description" content="Super fast, all natural JSON logger for Node.js">
|
7
|
+
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
|
8
|
+
<link rel="stylesheet" href="//unpkg.com/docsify-themeable/dist/css/theme-simple.css">
|
9
|
+
<style>
|
10
|
+
:root {
|
11
|
+
--base-font-size: 16px;
|
12
|
+
--theme-color: rgb(104, 118, 52);
|
13
|
+
--link-color: rgb(104, 118, 52);
|
14
|
+
--link-color--hover: rgb(137, 152, 100);
|
15
|
+
--sidebar-name-margin: 0;
|
16
|
+
--sidebar-name-padding: 0;
|
17
|
+
--code-font-size: .9em;
|
18
|
+
}
|
19
|
+
.sidebar > h1 {
|
20
|
+
margin-bottom: -.75em;
|
21
|
+
margin-top: .75em;
|
22
|
+
}
|
23
|
+
.sidebar > h1 img {
|
24
|
+
height: 4em;
|
25
|
+
}
|
26
|
+
.markdown-section a code {
|
27
|
+
color: var(--link-color)!important;
|
28
|
+
}
|
29
|
+
.markdown-section code:not([class*="lang-"]):not([class*="language-"]) {
|
30
|
+
white-space: unset
|
31
|
+
}
|
32
|
+
</style>
|
33
|
+
<link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png">
|
34
|
+
<link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.png">
|
35
|
+
</head>
|
36
|
+
<body>
|
37
|
+
<div id="app"></div>
|
38
|
+
</body>
|
39
|
+
<script>
|
40
|
+
window.$docsify = {
|
41
|
+
name: 'pino',
|
42
|
+
logo: './pino-tree.png',
|
43
|
+
loadSidebar: 'docsify/sidebar.md',
|
44
|
+
repo: 'https://github.com/pinojs/pino',
|
45
|
+
auto2top: true,
|
46
|
+
ga: 'UA-103155139-1'
|
47
|
+
}
|
48
|
+
</script>
|
49
|
+
<script src="//unpkg.com/docsify/lib/docsify.min.js"></script>
|
50
|
+
<script src="//unpkg.com/docsify/lib/plugins/search.min.js"></script>
|
51
|
+
<script src="//unpkg.com/docsify/lib/plugins/ga.min.js"></script>
|
52
|
+
<!-- To enable syntax highlighting on TypeScript codes: -->
|
53
|
+
<script src="//cdn.jsdelivr.net/npm/prismjs@1/components/prism-typescript.min.js"></script>
|
54
|
+
|
55
|
+
</html>
|
package/lib/caller.js
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
'use strict'
|
2
|
+
|
3
|
+
function noOpPrepareStackTrace (_, stack) {
|
4
|
+
return stack
|
5
|
+
}
|
6
|
+
|
7
|
+
module.exports = function getCallers () {
|
8
|
+
const originalPrepare = Error.prepareStackTrace
|
9
|
+
Error.prepareStackTrace = noOpPrepareStackTrace
|
10
|
+
const stack = new Error().stack
|
11
|
+
Error.prepareStackTrace = originalPrepare
|
12
|
+
|
13
|
+
if (!Array.isArray(stack)) {
|
14
|
+
return undefined
|
15
|
+
}
|
16
|
+
|
17
|
+
const entries = stack.slice(2)
|
18
|
+
|
19
|
+
const fileNames = []
|
20
|
+
|
21
|
+
for (const entry of entries) {
|
22
|
+
if (!entry) {
|
23
|
+
continue
|
24
|
+
}
|
25
|
+
|
26
|
+
fileNames.push(entry.getFileName())
|
27
|
+
}
|
28
|
+
|
29
|
+
return fileNames
|
30
|
+
}
|
package/lib/constants.js
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
/**
|
2
|
+
* Represents default log level values
|
3
|
+
*
|
4
|
+
* @enum {number}
|
5
|
+
*/
|
6
|
+
const DEFAULT_LEVELS = {
|
7
|
+
trace: 10,
|
8
|
+
debug: 20,
|
9
|
+
info: 30,
|
10
|
+
warn: 40,
|
11
|
+
error: 50,
|
12
|
+
fatal: 60
|
13
|
+
}
|
14
|
+
|
15
|
+
/**
|
16
|
+
* Represents sort order direction: `ascending` or `descending`
|
17
|
+
*
|
18
|
+
* @enum {string}
|
19
|
+
*/
|
20
|
+
const SORTING_ORDER = {
|
21
|
+
ASC: 'ASC',
|
22
|
+
DESC: 'DESC'
|
23
|
+
}
|
24
|
+
|
25
|
+
module.exports = {
|
26
|
+
DEFAULT_LEVELS,
|
27
|
+
SORTING_ORDER
|
28
|
+
}
|
package/lib/levels.js
ADDED
@@ -0,0 +1,241 @@
|
|
1
|
+
'use strict'
|
2
|
+
/* eslint no-prototype-builtins: 0 */
|
3
|
+
const {
|
4
|
+
lsCacheSym,
|
5
|
+
levelValSym,
|
6
|
+
useOnlyCustomLevelsSym,
|
7
|
+
streamSym,
|
8
|
+
formattersSym,
|
9
|
+
hooksSym,
|
10
|
+
levelCompSym
|
11
|
+
} = require('./symbols')
|
12
|
+
const { noop, genLog } = require('./tools')
|
13
|
+
const { DEFAULT_LEVELS, SORTING_ORDER } = require('./constants')
|
14
|
+
|
15
|
+
const levelMethods = {
|
16
|
+
fatal: (hook) => {
|
17
|
+
const logFatal = genLog(DEFAULT_LEVELS.fatal, hook)
|
18
|
+
return function (...args) {
|
19
|
+
const stream = this[streamSym]
|
20
|
+
logFatal.call(this, ...args)
|
21
|
+
if (typeof stream.flushSync === 'function') {
|
22
|
+
try {
|
23
|
+
stream.flushSync()
|
24
|
+
} catch (e) {
|
25
|
+
// https://github.com/pinojs/pino/pull/740#discussion_r346788313
|
26
|
+
}
|
27
|
+
}
|
28
|
+
}
|
29
|
+
},
|
30
|
+
error: (hook) => genLog(DEFAULT_LEVELS.error, hook),
|
31
|
+
warn: (hook) => genLog(DEFAULT_LEVELS.warn, hook),
|
32
|
+
info: (hook) => genLog(DEFAULT_LEVELS.info, hook),
|
33
|
+
debug: (hook) => genLog(DEFAULT_LEVELS.debug, hook),
|
34
|
+
trace: (hook) => genLog(DEFAULT_LEVELS.trace, hook)
|
35
|
+
}
|
36
|
+
|
37
|
+
const nums = Object.keys(DEFAULT_LEVELS).reduce((o, k) => {
|
38
|
+
o[DEFAULT_LEVELS[k]] = k
|
39
|
+
return o
|
40
|
+
}, {})
|
41
|
+
|
42
|
+
const initialLsCache = Object.keys(nums).reduce((o, k) => {
|
43
|
+
o[k] = '{"level":' + Number(k)
|
44
|
+
return o
|
45
|
+
}, {})
|
46
|
+
|
47
|
+
function genLsCache (instance) {
|
48
|
+
const formatter = instance[formattersSym].level
|
49
|
+
const { labels } = instance.levels
|
50
|
+
const cache = {}
|
51
|
+
for (const label in labels) {
|
52
|
+
const level = formatter(labels[label], Number(label))
|
53
|
+
cache[label] = JSON.stringify(level).slice(0, -1)
|
54
|
+
}
|
55
|
+
instance[lsCacheSym] = cache
|
56
|
+
return instance
|
57
|
+
}
|
58
|
+
|
59
|
+
function isStandardLevel (level, useOnlyCustomLevels) {
|
60
|
+
if (useOnlyCustomLevels) {
|
61
|
+
return false
|
62
|
+
}
|
63
|
+
|
64
|
+
switch (level) {
|
65
|
+
case 'fatal':
|
66
|
+
case 'error':
|
67
|
+
case 'warn':
|
68
|
+
case 'info':
|
69
|
+
case 'debug':
|
70
|
+
case 'trace':
|
71
|
+
return true
|
72
|
+
default:
|
73
|
+
return false
|
74
|
+
}
|
75
|
+
}
|
76
|
+
|
77
|
+
function setLevel (level) {
|
78
|
+
const { labels, values } = this.levels
|
79
|
+
if (typeof level === 'number') {
|
80
|
+
if (labels[level] === undefined) throw Error('unknown level value' + level)
|
81
|
+
level = labels[level]
|
82
|
+
}
|
83
|
+
if (values[level] === undefined) throw Error('unknown level ' + level)
|
84
|
+
const preLevelVal = this[levelValSym]
|
85
|
+
const levelVal = this[levelValSym] = values[level]
|
86
|
+
const useOnlyCustomLevelsVal = this[useOnlyCustomLevelsSym]
|
87
|
+
const levelComparison = this[levelCompSym]
|
88
|
+
const hook = this[hooksSym].logMethod
|
89
|
+
|
90
|
+
for (const key in values) {
|
91
|
+
if (levelComparison(values[key], levelVal) === false) {
|
92
|
+
this[key] = noop
|
93
|
+
continue
|
94
|
+
}
|
95
|
+
this[key] = isStandardLevel(key, useOnlyCustomLevelsVal) ? levelMethods[key](hook) : genLog(values[key], hook)
|
96
|
+
}
|
97
|
+
|
98
|
+
this.emit(
|
99
|
+
'level-change',
|
100
|
+
level,
|
101
|
+
levelVal,
|
102
|
+
labels[preLevelVal],
|
103
|
+
preLevelVal,
|
104
|
+
this
|
105
|
+
)
|
106
|
+
}
|
107
|
+
|
108
|
+
function getLevel (level) {
|
109
|
+
const { levels, levelVal } = this
|
110
|
+
// protection against potential loss of Pino scope from serializers (edge case with circular refs - https://github.com/pinojs/pino/issues/833)
|
111
|
+
return (levels && levels.labels) ? levels.labels[levelVal] : ''
|
112
|
+
}
|
113
|
+
|
114
|
+
function isLevelEnabled (logLevel) {
|
115
|
+
const { values } = this.levels
|
116
|
+
const logLevelVal = values[logLevel]
|
117
|
+
return logLevelVal !== undefined && this[levelCompSym](logLevelVal, this[levelValSym])
|
118
|
+
}
|
119
|
+
|
120
|
+
/**
|
121
|
+
* Determine if the given `current` level is enabled by comparing it
|
122
|
+
* against the current threshold (`expected`).
|
123
|
+
*
|
124
|
+
* @param {SORTING_ORDER} direction comparison direction "ASC" or "DESC"
|
125
|
+
* @param {number} current current log level number representation
|
126
|
+
* @param {number} expected threshold value to compare with
|
127
|
+
* @returns {boolean}
|
128
|
+
*/
|
129
|
+
function compareLevel (direction, current, expected) {
|
130
|
+
if (direction === SORTING_ORDER.DESC) {
|
131
|
+
return current <= expected
|
132
|
+
}
|
133
|
+
|
134
|
+
return current >= expected
|
135
|
+
}
|
136
|
+
|
137
|
+
/**
|
138
|
+
* Create a level comparison function based on `levelComparison`
|
139
|
+
* it could a default function which compares levels either in "ascending" or "descending" order or custom comparison function
|
140
|
+
*
|
141
|
+
* @param {SORTING_ORDER | Function} levelComparison sort levels order direction or custom comparison function
|
142
|
+
* @returns Function
|
143
|
+
*/
|
144
|
+
function genLevelComparison (levelComparison) {
|
145
|
+
if (typeof levelComparison === 'string') {
|
146
|
+
return compareLevel.bind(null, levelComparison)
|
147
|
+
}
|
148
|
+
|
149
|
+
return levelComparison
|
150
|
+
}
|
151
|
+
|
152
|
+
function mappings (customLevels = null, useOnlyCustomLevels = false) {
|
153
|
+
const customNums = customLevels
|
154
|
+
/* eslint-disable */
|
155
|
+
? Object.keys(customLevels).reduce((o, k) => {
|
156
|
+
o[customLevels[k]] = k
|
157
|
+
return o
|
158
|
+
}, {})
|
159
|
+
: null
|
160
|
+
/* eslint-enable */
|
161
|
+
|
162
|
+
const labels = Object.assign(
|
163
|
+
Object.create(Object.prototype, { Infinity: { value: 'silent' } }),
|
164
|
+
useOnlyCustomLevels ? null : nums,
|
165
|
+
customNums
|
166
|
+
)
|
167
|
+
const values = Object.assign(
|
168
|
+
Object.create(Object.prototype, { silent: { value: Infinity } }),
|
169
|
+
useOnlyCustomLevels ? null : DEFAULT_LEVELS,
|
170
|
+
customLevels
|
171
|
+
)
|
172
|
+
return { labels, values }
|
173
|
+
}
|
174
|
+
|
175
|
+
function assertDefaultLevelFound (defaultLevel, customLevels, useOnlyCustomLevels) {
|
176
|
+
if (typeof defaultLevel === 'number') {
|
177
|
+
const values = [].concat(
|
178
|
+
Object.keys(customLevels || {}).map(key => customLevels[key]),
|
179
|
+
useOnlyCustomLevels ? [] : Object.keys(nums).map(level => +level),
|
180
|
+
Infinity
|
181
|
+
)
|
182
|
+
if (!values.includes(defaultLevel)) {
|
183
|
+
throw Error(`default level:${defaultLevel} must be included in custom levels`)
|
184
|
+
}
|
185
|
+
return
|
186
|
+
}
|
187
|
+
|
188
|
+
const labels = Object.assign(
|
189
|
+
Object.create(Object.prototype, { silent: { value: Infinity } }),
|
190
|
+
useOnlyCustomLevels ? null : DEFAULT_LEVELS,
|
191
|
+
customLevels
|
192
|
+
)
|
193
|
+
if (!(defaultLevel in labels)) {
|
194
|
+
throw Error(`default level:${defaultLevel} must be included in custom levels`)
|
195
|
+
}
|
196
|
+
}
|
197
|
+
|
198
|
+
function assertNoLevelCollisions (levels, customLevels) {
|
199
|
+
const { labels, values } = levels
|
200
|
+
for (const k in customLevels) {
|
201
|
+
if (k in values) {
|
202
|
+
throw Error('levels cannot be overridden')
|
203
|
+
}
|
204
|
+
if (customLevels[k] in labels) {
|
205
|
+
throw Error('pre-existing level values cannot be used for new levels')
|
206
|
+
}
|
207
|
+
}
|
208
|
+
}
|
209
|
+
|
210
|
+
/**
|
211
|
+
* Validates whether `levelComparison` is correct
|
212
|
+
*
|
213
|
+
* @throws Error
|
214
|
+
* @param {SORTING_ORDER | Function} levelComparison - value to validate
|
215
|
+
* @returns
|
216
|
+
*/
|
217
|
+
function assertLevelComparison (levelComparison) {
|
218
|
+
if (typeof levelComparison === 'function') {
|
219
|
+
return
|
220
|
+
}
|
221
|
+
|
222
|
+
if (typeof levelComparison === 'string' && Object.values(SORTING_ORDER).includes(levelComparison)) {
|
223
|
+
return
|
224
|
+
}
|
225
|
+
|
226
|
+
throw new Error('Levels comparison should be one of "ASC", "DESC" or "function" type')
|
227
|
+
}
|
228
|
+
|
229
|
+
module.exports = {
|
230
|
+
initialLsCache,
|
231
|
+
genLsCache,
|
232
|
+
levelMethods,
|
233
|
+
getLevel,
|
234
|
+
setLevel,
|
235
|
+
isLevelEnabled,
|
236
|
+
mappings,
|
237
|
+
assertNoLevelCollisions,
|
238
|
+
assertDefaultLevelFound,
|
239
|
+
genLevelComparison,
|
240
|
+
assertLevelComparison
|
241
|
+
}
|
package/lib/meta.js
ADDED