-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplugin.js
124 lines (106 loc) · 3.13 KB
/
plugin.js
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
'use strict'
const fp = require('fastify-plugin')
/**
* Every request creates a new child logger based on:
* - the unique main logger
* - the route's configuration/context
*/
async function fastifyLogController (fastify, opts) {
const {
optionKey = 'logCtrl',
exposeGet = false,
routeConfig
} = opts
const pocket = new Map()
// Inspired by https://github.com/Eomm/fastify-explorer
fastify.addHook('onRegister', function fastifyExplorerTracker (instance, opts) {
const pluginId = opts?.[optionKey]?.name
if (pluginId) {
if (pocket.has(pluginId)) {
throw new Error(`The instance named ${pluginId} has been already registered`)
}
const defaultLogLevel = opts.logLevel || instance.log.level
pocket.set(pluginId, defaultLogLevel) // save the initial log level
// todo manage the case where the route has a specific log level
instance.addHook('onRequest', logLevelHook.bind(instance, pluginId))
}
})
const logLevels = Object.keys(fastify.log.levels.values)
if (exposeGet) {
fastify.get('/log-level/levels', {
...routeConfig,
handler: logLevelControllerLevels,
schema: {
response: {
200: {
type: 'array',
items: {
type: 'string'
}
}
}
}
})
fastify.get('/log-level', {
...routeConfig,
handler: logLevelControllerReader,
schema: {
response: {
200: {
type: 'array',
items: {
type: 'object',
required: ['level', 'contextName'],
properties: {
contextName: { type: 'string', minLength: 1, maxLength: 500 },
level: { type: 'string', enum: logLevels }
}
}
}
}
}
})
}
fastify.post('/log-level', {
...routeConfig,
handler: logLevelControllerHandler,
schema: {
body: {
type: 'object',
required: ['level', 'contextName'],
properties: {
level: { type: 'string', enum: logLevels },
contextName: { type: 'string', minLength: 1, maxLength: 500 }
}
}
}
})
function logLevelControllerLevels (request, reply) {
reply.code(200).send(logLevels)
}
function logLevelControllerReader (request, reply) {
reply.code(200).send(Array.from(pocket, e => ({ contextName: e[0], level: e[1] })))
}
function logLevelHook (pluginId, request, reply, done) {
if (!request.routeOptions.logLevel &&
request.log.level !== pocket.get(pluginId)) {
request.log.level = pocket.get(pluginId)
}
done()
}
function logLevelControllerHandler (request, reply) {
const instance = pocket.get(request.body.contextName)
if (!instance) {
reply.code(404).send(new Error('Context not found'))
return
}
pocket.set(request.body.contextName, request.body.level)
reply.code(204).send()
}
}
module.exports = fp(fastifyLogController, {
name: 'fastify-log-controller',
fastify: '^5'
})
module.exports.default = fastifyLogController
module.exports.fastifyLogController = fastifyLogController