@react-foundry/fastify-dev-logger 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (C) 2019-2025 Crown Copyright
4
+ Copyright (C) 2019-2026 Daniel A.C. Martin
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
7
+ this software and associated documentation files (the "Software"), to deal in
8
+ the Software without restriction, including without limitation the rights to
9
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10
+ of the Software, and to permit persons to whom the Software is furnished to do
11
+ so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,67 @@
1
+ React Foundry - Fastify Dev Logger
2
+ ==================================
3
+
4
+ A Pino transport for pretty printing [Fastify] logs in **dev** environments.
5
+ Built on [pino-pretty].
6
+
7
+ The output looks like this (but with colours):
8
+
9
+ ```
10
+ [16:56:26.501] info | Server listening at http://[::1]:5173
11
+ [16:56:34.020] info (01) | GET / - incoming request
12
+ [16:56:48.565] info (01) | GET / - request completed; 200 OK (64ms)
13
+ ```
14
+
15
+ **Note:** It should be possible to use this in [pino] without Fastify, but we
16
+ provide extra features for handling Fastify's (request) objects.
17
+
18
+
19
+ Using this package
20
+ ------------------
21
+
22
+ First install the package into your project:
23
+
24
+ ```shell
25
+ npm install -D @react-foundry/fastify-dev-logger
26
+ ```
27
+
28
+ Then use it in your code as follows:
29
+
30
+ ```js
31
+ import fastifyDevLogger from '@react-foundry/fastify-dev-logger';
32
+ import Fastify from 'fastify';
33
+
34
+ const httpd = Fastify({
35
+ logger: {
36
+ transport: {
37
+ target: '@react-foundry/fastify-dev-logger'
38
+ }
39
+ }
40
+ });
41
+
42
+ [...]
43
+ ```
44
+
45
+
46
+ Working on this package
47
+ -----------------------
48
+
49
+ Before working on this package you must install its dependencies using
50
+ the following command:
51
+
52
+ ```shell
53
+ pnpm install
54
+ ```
55
+
56
+ ### Testing
57
+
58
+ Test the package by running the unit tests.
59
+
60
+ ```shell
61
+ npm test
62
+ ```
63
+
64
+
65
+ [Fastify]: https://fastify.dev/
66
+ [pino]: https://getpino.io/
67
+ [pino-pretty]: https://www.npmjs.com/package/pino-pretty
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ const baseConfig = require('../../jest.config.base');
4
+
5
+ const config = {
6
+ ...baseConfig,
7
+ collectCoverageFrom: [
8
+ '<rootDir>/src/**.js',
9
+ ],
10
+ testMatch: [
11
+ '<rootDir>/spec/**.js'
12
+ ]
13
+ };
14
+
15
+ module.exports = config;
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@react-foundry/fastify-dev-logger",
3
+ "version": "0.1.0",
4
+ "description": "A Pino transport for pretty printing Fastify logs in dev environments.",
5
+ "type": "module",
6
+ "main": "src/index.js",
7
+ "typings": "src/index.d.ts",
8
+ "author": "Daniel A.C. Martin <npm@daniel-martin.co.uk> (http://daniel-martin.co.uk/)",
9
+ "license": "MIT",
10
+ "engines": {
11
+ "node": ">=18.0.0"
12
+ },
13
+ "dependencies": {
14
+ "pino-pretty": "^13.1.3"
15
+ },
16
+ "devDependencies": {
17
+ "jest": "30.2.0",
18
+ "jest-environment-jsdom": "30.2.0",
19
+ "pino": "10.3.0",
20
+ "ts-jest": "29.4.6"
21
+ },
22
+ "scripts": {
23
+ "test": "NODE_OPTIONS=--experimental-vm-modules jest",
24
+ "build": "true",
25
+ "clean": "true"
26
+ }
27
+ }
package/spec/index.js ADDED
@@ -0,0 +1,115 @@
1
+ import pino from 'pino';
2
+ import { Transform, Writable } from 'node:stream';
3
+ import { fastifyDevLogger } from '../src/index.js';
4
+
5
+ // See: https://github.com/chalk/ansi-regex/blob/94983fc6ba00e1e9657f72c07eb7b9c75e4011a2/index.js
6
+ const controlCodesRegex = /(?:\u001B\][\s\S]*?(?:\u0007|\u001B\u005C|\u009C))|[\u001B\u009B][[\]()#;?]*(?:\d{1,4}(?:[;:]\d{0,4})*)?[\dA-PR-TZcf-nq-uy=><~]/g;
7
+ const stripColours = (str) => str.replace(controlCodesRegex, '');
8
+
9
+ describe('fastifyDevLogger', () => {
10
+ it('is a function', () => expect(fastifyDevLogger).toBeInstanceOf(Function));
11
+ it('that takes one parameter', () => expect(fastifyDevLogger).toHaveLength(1));
12
+
13
+ describe('when given minimal options', () => {
14
+ let output = [];
15
+ const captureStream = new Writable({
16
+ write(chunk, _enc, cb) {
17
+ output.push(
18
+ stripColours(chunk.toString())
19
+ .replace(/\n$/, '')
20
+ );
21
+ cb();
22
+ },
23
+ });
24
+ const options = {
25
+ sync: true,
26
+ destination: captureStream
27
+ };
28
+ const result = fastifyDevLogger(options);
29
+
30
+ it('returns a Transform object', () => expect(result).toBeInstanceOf(Transform));
31
+
32
+ describe('the Transform', () => {
33
+ describe('when run in pino', () => {
34
+ const pinoOptions = {
35
+ level: 'trace'
36
+ };
37
+ const logger = pino(pinoOptions, result);
38
+ const timePattern = '\\[[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}\\]';
39
+ const timeRegex = new RegExp(timePattern);
40
+
41
+ describe('when logging a string', () => {
42
+ beforeAll(() => {
43
+ output = [];
44
+ logger.warn('My message');
45
+ });
46
+
47
+ it('writes only once', () => expect(output.length).toEqual(1));
48
+ it('outputs the message', () => expect(output[0]).toContain('My message'));
49
+ it('outputs the log level', () => expect(output[0]).toContain('warn'));
50
+ it('outputs the time', () => expect(output[0]).toMatch(timeRegex));
51
+ it('outputs in the correct format', () => expect(output[0]).toMatch(new RegExp(
52
+ `^${timePattern} warn \\| My message$`
53
+ )));
54
+ });
55
+
56
+ describe('when logging an error', () => {
57
+ beforeAll(() => {
58
+ output = [];
59
+ logger.error(new Error('My error message'));
60
+ });
61
+
62
+ it('writes only once', () => expect(output.length).toEqual(1));
63
+ it('outputs the message', () => expect(output[0]).toContain('My error message'));
64
+ it('outputs log level', () => expect(output[0]).toContain('error'));
65
+ it('outputs the time', () => expect(output[0]).toMatch(timeRegex));
66
+ it('outputs in the correct format', () => expect(output[0]).toMatch(new RegExp(
67
+ `^${timePattern} error \\| Error: My error message\n\\s*at `,
68
+ 'ms'
69
+ )));
70
+ });
71
+
72
+ describe('when logging an object', () => {
73
+ beforeAll(() => {
74
+ output = [];
75
+ logger.debug({ my: 'object' });
76
+ });
77
+
78
+ it('writes only once', () => expect(output.length).toEqual(1));
79
+ it('outputs the object', () => expect(output[0]).toContain('{ my: \'object\' }'));
80
+ it('outputs log level', () => expect(output[0]).toContain('debug'));
81
+ it('outputs the time', () => expect(output[0]).toMatch(timeRegex));
82
+ it('outputs in the correct format', () => expect(output[0]).toMatch(new RegExp(
83
+ `^${timePattern} debug \\| { my: 'object' }$`
84
+ )));
85
+ });
86
+
87
+ describe('when logging a request', () => {
88
+ beforeAll(() => {
89
+ output = [];
90
+ logger.info({
91
+ reqId: 'req-c3',
92
+ req: {
93
+ method: 'GET',
94
+ url: '/path/to/resource'
95
+ },
96
+ res: {
97
+ statusCode: 200
98
+ },
99
+ responseTime: 1066,
100
+ msg: 'request completed'
101
+ });
102
+ });
103
+
104
+ it('writes only once', () => expect(output.length).toEqual(1));
105
+ it('outputs the message', () => expect(output[0]).toContain('request completed'));
106
+ it('outputs log level', () => expect(output[0]).toContain('info'));
107
+ it('outputs the time', () => expect(output[0]).toMatch(timeRegex));
108
+ it('outputs in the correct format', () => expect(output[0]).toMatch(new RegExp(
109
+ `^${timePattern} info \\(C3\\) \\| GET /path/to/resource - request completed; 200 OK \\(1066ms\\)$`
110
+ )));
111
+ });
112
+ });
113
+ });
114
+ });
115
+ });
package/src/index.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import type { PrettyOptions, PrettyStream } from 'pino-pretty';
2
+
3
+ export type FastifyDevLoggerOptions = Pick<PrettyOptions, 'append', 'destination', 'minimumLevel', 'mkdir', 'sync'> & {
4
+ };
5
+
6
+ declare export const fastifyDevLogger: (a: FastifyDevLoggerOptions) => PrettyStream;
7
+
8
+ export default fastifyDevLogger;
package/src/index.js ADDED
@@ -0,0 +1,249 @@
1
+ import { inspect } from 'node:util';
2
+ import pretty from 'pino-pretty';
3
+
4
+ const formatNumber = (width, number) => (
5
+ number.toString().padStart(width, '0')
6
+ );
7
+
8
+ export const fastifyDevLogger = ({
9
+ ...prettyOptions
10
+ }) => {
11
+ const colourise = pretty.colorizerFactory(true);
12
+ const colourMessage = colourise.message;
13
+ const greyMessage = colourise.greyMessage;
14
+ const redMessage = colourise.colors.red;
15
+ const amberMessage = colourise.colors.yellow;
16
+ const greenMessage = colourise.colors.green;
17
+ const space = ' ';
18
+ const colourLevel = (level) => (
19
+ space.repeat(5 - level.length) +
20
+ colourise(level)
21
+ .toLowerCase()
22
+ );
23
+ const levelName = {
24
+ 10: colourLevel('trace'),
25
+ 20: colourLevel('debug'),
26
+ 30: colourLevel('info'),
27
+ 40: colourLevel('warn'),
28
+ 50: colourLevel('error'),
29
+ 60: colourLevel('fatal')
30
+ };
31
+ const statusName = {
32
+ 100: colourMessage('100 Continue'),
33
+ 101: colourMessage('101 Switching Protocols'),
34
+ 102: colourMessage('102 Processing'),
35
+ 103: colourMessage('103 Early Hints'),
36
+ 200: greenMessage('200 OK'),
37
+ 201: greenMessage('201 Created'),
38
+ 202: greenMessage('202 Accepted'),
39
+ 203: greenMessage('203 Non-Authoritative Information'),
40
+ 204: greenMessage('204 No Content'),
41
+ 205: greenMessage('205 Reset Content'),
42
+ 206: greenMessage('206 Partial Content'),
43
+ 207: greenMessage('207 Multi-Status'),
44
+ 208: greenMessage('208 Already Reported'),
45
+ 226: greenMessage('226 IM Used'),
46
+ 300: colourMessage('300 Multiple Choices'),
47
+ 301: colourMessage('301 Moved Permanently'),
48
+ 302: colourMessage('302 Found'),
49
+ 303: colourMessage('303 See Other'),
50
+ 304: colourMessage('304 Not Modified'),
51
+ 305: colourMessage('305 Use Proxy'),
52
+ 306: colourMessage('306 unused'),
53
+ 307: colourMessage('307 Temporary Redirect'),
54
+ 308: colourMessage('308 Permanent Redirect'),
55
+ 400: amberMessage('400 Bad Request'),
56
+ 401: amberMessage('401 Unauthorised'),
57
+ 402: amberMessage('402 Payment Required'),
58
+ 403: amberMessage('403 Forbidden'),
59
+ 404: amberMessage('404 Not Found'),
60
+ 405: amberMessage('405 Method Not Allowed'),
61
+ 406: amberMessage('406 Not Acceptable'),
62
+ 407: amberMessage('407 Proxy Authentication Required'),
63
+ 408: amberMessage('408 Request Timeout'),
64
+ 409: amberMessage('409 Conflict'),
65
+ 410: amberMessage('410 Gone'),
66
+ 411: amberMessage('411 Length Required'),
67
+ 412: amberMessage('412 Precondition Failed'),
68
+ 413: amberMessage('413 Content Too Large'),
69
+ 414: amberMessage('414 URI Too Long'),
70
+ 415: amberMessage('415 Unsupported Media Type'),
71
+ 416: amberMessage('416 Range Not Satisfiable'),
72
+ 417: amberMessage('417 Expectation Failed'),
73
+ 418: amberMessage('418 I am a teapot'),
74
+ 421: amberMessage('421 Misdirected Request'),
75
+ 422: amberMessage('422 Unprocessable Content'),
76
+ 423: amberMessage('423 Locked'),
77
+ 424: amberMessage('424 Failed Dependency'),
78
+ 425: amberMessage('425 Too Early'),
79
+ 426: amberMessage('426 Upgrade Required'),
80
+ 428: amberMessage('428 Precondition Required'),
81
+ 429: amberMessage('429 Too Many Requests'),
82
+ 431: amberMessage('431 Request Header Fields Too Large'),
83
+ 451: amberMessage('451 Unavailable For Legal Reasons'),
84
+ 500: redMessage('500 Internal Server Error'),
85
+ 501: redMessage('501 Not Implemented'),
86
+ 502: redMessage('502 Bad Gateway'),
87
+ 503: redMessage('503 Service Unavailable'),
88
+ 504: redMessage('504 Gateway Timeout'),
89
+ 505: redMessage('505 HTTP Version Not Supported'),
90
+ 506: redMessage('506 Variant Also Negotiates'),
91
+ 507: redMessage('507 Insufficient Storage'),
92
+ 508: redMessage('508 Loop Detected'),
93
+ 510: redMessage('510 Not Extended'),
94
+ 511: redMessage('511 Network Authentication Required')
95
+ };
96
+ const reqContext = {};
97
+
98
+ const messageFormat = (log, messageKey, _levelLabel, { colors }) => {
99
+ const {
100
+ level: _level,
101
+ time: _time,
102
+ pid,
103
+ hostname,
104
+ reqId,
105
+ ...obj
106
+ } = log;
107
+ const {
108
+ err,
109
+ req,
110
+ res,
111
+ responseTime: _responseTime,
112
+ [messageKey]: msg,
113
+ } = log;
114
+
115
+ if (reqId && req && !err) {
116
+ // Create new context
117
+ reqContext[reqId] = req;
118
+ }
119
+
120
+ const ctx = (
121
+ reqId
122
+ ? reqContext[reqId]
123
+ : req
124
+ );
125
+
126
+ // Suppress noise in dev environment
127
+ const url = ctx?.url || '';
128
+ const fileRequest = url && (url.startsWith('/@') || url.match(/^\/(app|src|node_modules)\/.*\.[^\.\/]+$/));
129
+ const standardMsg = msg === 'incoming request' || msg === 'request completed';
130
+ const unhealthyResponse = res?.statusCode >= 400;
131
+
132
+ if (fileRequest && standardMsg && !unhealthyResponse) {
133
+ return undefined;
134
+ }
135
+
136
+ const datetime = new Date(_time);
137
+ const HH = formatNumber(2, datetime.getHours());
138
+ const mm = formatNumber(2, datetime.getMinutes());
139
+ const ss = formatNumber(2, datetime.getSeconds());
140
+ const mmm = formatNumber(3, datetime.getMilliseconds());
141
+
142
+ const time = greyMessage(`[${HH}:${mm}:${ss}.${mmm}]`);
143
+ const level = levelName[_level] || '?????';
144
+ const request = (
145
+ reqId
146
+ ? greyMessage(` (${reqId.substring(4).substring(-2).padStart(2, '0').toUpperCase()})`)
147
+ : ' '
148
+ );
149
+ const sep1 = greyMessage(' | ');
150
+ const sep2 = greyMessage(' - ');
151
+ const http = (
152
+ ctx
153
+ ? colourMessage(`${ctx.method} ${ctx.url}`)
154
+ : ''
155
+ );
156
+
157
+ const indent = space.repeat(25) + sep1;
158
+ const breakLength = 52; // Magic constants as colours make it hard to count characters
159
+ const message = (
160
+ err
161
+ ? (
162
+ err.stack
163
+ .split('\n')
164
+ .map((v, i) => (
165
+ i === 0
166
+ ? redMessage(`${err.type}: `) + err.message
167
+ : greyMessage(v)
168
+ ))
169
+ .join('\n')
170
+ )
171
+ : msg || inspect(obj, { breakLength }).replace(/\n/g, '\n' + indent)
172
+ );
173
+ const status = (
174
+ res?.statusCode && !err
175
+ ? greyMessage('; ') + colourMessage(`${statusName[res.statusCode] || res.statusCode}`)
176
+ : ''
177
+ );
178
+ const round = (num) => num.toPrecision(3);
179
+ const responseTimeSeconds = round(_responseTime / 1000) + 's';
180
+ const responseTime = (
181
+ _responseTime
182
+ ? (
183
+ _responseTime < 1
184
+ ? greyMessage(_responseTime.toFixed(2) + 'ms')
185
+ : (
186
+ _responseTime < 1000
187
+ ? greyMessage(round(_responseTime) + 'ms')
188
+ : (
189
+ _responseTime < 3000
190
+ ? colourMessage(responseTimeSeconds)
191
+ : (
192
+ _responseTime < 25000
193
+ ? amberMessage(responseTimeSeconds)
194
+ : redMessage(responseTimeSeconds)
195
+ )
196
+ )
197
+ )
198
+ )
199
+ : ''
200
+ );
201
+ const _contentLength = res?.contentLength;
202
+ const contentLength = (
203
+ _contentLength
204
+ ?
205
+ (
206
+ _contentLength >= 1000000
207
+ ? colourMessage(round(_contentLength / 1000000) + 'MB')
208
+ : (
209
+ _contentLength >= 1000
210
+ ? greyMessage(round(_contentLength / 1000) + 'kB')
211
+ : greyMessage(_contentLength)
212
+ )
213
+ )
214
+ : ''
215
+ );
216
+ const responseInfo = (
217
+ responseTime
218
+ ? (
219
+ greyMessage(' (') + responseTime + (
220
+ contentLength
221
+ ? greyMessage(', ') + contentLength
222
+ : ''
223
+ ) + greyMessage(')')
224
+ )
225
+ : ''
226
+ );
227
+
228
+ if (reqId && res && !err) {
229
+ // Request is finished; clean up the context
230
+ delete reqContext[reqId];
231
+ }
232
+
233
+ return `\r${time} ${level}${request}${sep1}` + (
234
+ http
235
+ ? `${http}${sep2}${message}${status}${responseInfo}`
236
+ : message
237
+ );
238
+ };
239
+
240
+ return pretty({
241
+ ...prettyOptions,
242
+ messageFormat,
243
+ include: '',
244
+ hideObject: true,
245
+ colorize: false
246
+ });
247
+ };
248
+
249
+ export default fastifyDevLogger;