@pbvision/fastify-firestore-service 0.0.7 → 0.0.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pbvision/fastify-firestore-service",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "Web Framework using Fastify and Firestore ORM",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
package/src/make-app.js CHANGED
@@ -1,9 +1,9 @@
1
- import Ajv from 'ajv/dist/2020'
1
+ import Ajv from 'ajv/dist/2020.js'
2
2
  import fastify from 'fastify'
3
3
  import { v4 as uuidv4 } from 'uuid'
4
4
 
5
5
  import ComponentRegistrar from './component-registrar.js'
6
- import makeLogger from './make-logger.js'
6
+ import { makePinoLoggerOptions } from './make-logger.js'
7
7
  import compressPlugin from './plugins/compress.js'
8
8
  import contentParserPlugin from './plugins/content-parser.js'
9
9
  import cookiePlugin from './plugins/cookie.js'
@@ -157,10 +157,10 @@ export default async function makeService (params = {}) {
157
157
  coerceTypes: new Ajv({ ...ajvParams, coerceTypes: true }),
158
158
  noCoerceTypes: new Ajv({ ...ajvParams, coerceTypes: false })
159
159
  }
160
+ const logger = makePinoLoggerOptions(logging.customizePinoOpts)
160
161
  const app = fastify({
161
- ignoreTrailingSlash: true,
162
- disableRequestLogging: false,
163
- logger: makeLogger(logging.customizePinoOpts),
162
+ disableRequestLogging: true,
163
+ logger,
164
164
  genReqId: () => `${fastifyServerId}-${++requestCount}`
165
165
  })
166
166
  .setValidatorCompiler(({ httpPart, schema }) => {
@@ -174,6 +174,26 @@ export default async function makeService (params = {}) {
174
174
  ? ({ value, error: validate.errors })
175
175
  : ({ value })
176
176
  })
177
+ .addHook('onResponse', (req, reply, done) => {
178
+ let objToLog = {}
179
+ try {
180
+ objToLog = logger.serializers.res(reply.raw)
181
+ Object.assign(objToLog, logger.serializers.req(req))
182
+ // istanbul ignore else
183
+ if (latencyTracker.header) {
184
+ // latency in milliseconds
185
+ objToLog.latency = reply.getHeader(latencyTracker.header)
186
+ }
187
+ } catch (err) /* istanbul ignore next */ {
188
+ // crashes in onResponse() aren't reported or caught unless we do this
189
+ if (typeof objToLog !== 'object') {
190
+ objToLog = {}
191
+ }
192
+ objToLog.failedToLog = JSON.stringify(err)
193
+ }
194
+ req.log.info(objToLog)
195
+ done()
196
+ })
177
197
 
178
198
  const registrar = new RegistrarCls(app, service)
179
199
 
@@ -1,4 +1,4 @@
1
- export default function makeCustomLogger (customizeOpts) {
1
+ export function makePinoLoggerOptions (customizeOpts) {
2
2
  function serializeReq (req) {
3
3
  const q = req.query
4
4
  // istanbul ignore else
@@ -21,9 +21,7 @@ export default function makeCustomLogger (customizeOpts) {
21
21
  level: 'debug',
22
22
  serializers: {
23
23
  req: serializeReq,
24
- res: res => {
25
- return { status: res.statusCode, req: serializeReq(res.request) }
26
- }
24
+ res: res => ({ status: res.statusCode })
27
25
  }
28
26
  })
29
27
  return options
@@ -27,17 +27,6 @@ export default fp(function (fastify, options, next) {
27
27
  return payload
28
28
  })
29
29
 
30
- fastify.addHook('onResponse', (req, reply, done) => {
31
- // if errored, then it was already logged
32
- if (!reply.raw.logged) {
33
- req.log.info({
34
- req,
35
- status: reply.raw.statusCode,
36
- latency: reply.getHeader(header)
37
- })
38
- }
39
- done()
40
- })
41
30
  next()
42
31
  }, {
43
32
  fastify: '>=3.x',