@ogcio/fastify-logging-wrapper 5.1.1 → 5.2.2-canary-e6d877a

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/README.md CHANGED
@@ -5,20 +5,24 @@ This logging wrapper goal is to standardize the records written by our Fastify s
5
5
  ## How to
6
6
 
7
7
  To use this package three steps are needed:
8
+
8
9
  - install the package with
9
- ```
10
+
11
+ ```bash
10
12
  npm i @ogcio/fastify-logging-wrapper
11
13
  ```
12
14
 
13
15
  - use the `getLoggingConfiguration()` method to get the configuration for the `fastify` server
14
- ```
16
+
17
+ ```typescript
15
18
  const server = fastify({
16
19
  ...getLoggingConfiguration()
17
20
  });
18
21
  ```
19
22
 
20
23
  - after the server is initialized, invoke the `initializeLoggingHooks(server)` to setup the needed `fastify` hooks
21
- ```
24
+
25
+ ```typescript
22
26
  initializeLoggingHooks(server);
23
27
  ```
24
28
 
@@ -29,15 +33,21 @@ That's it! Just log as you usually do!
29
33
  We will have 3 mandatory log entries that will be written for each request the service manages.
30
34
 
31
35
  Those 3 records are:
36
+
32
37
  - **New Request**, written when a request is received
38
+
33
39
  ```
34
40
  {"level":30,"level_name":"INFO","hostname":"hostname","request_id":"q9Y6NwwbRimle4TxcXRPkQ-0000000000","timestamp":1713868947766,"request":{"scheme":"http","method":"GET","path":"/ping","hostname":"localhost:80","query_params":{},"headers":{"user-agent":"lightMyRequest","host":"localhost:80"},"client_ip":"127.0.0.1","user_agent":"lightMyRequest"},"message":"NEW_REQUEST"}
35
41
  ```
42
+
36
43
  - **Response**, containing most of the response data
44
+
37
45
  ```
38
46
  {"level":30,"level_name":"INFO","hostname":"hostname","request_id":"q9Y6NwwbRimle4TxcXRPkQ-0000000000","timestamp":1713868947769,"request":{"scheme":"http","method":"GET","path":"/ping","hostname":"localhost:80","query_params":{}},"response":{"status_code":200,"headers":{"content-type":"application/json; charset=utf-8","content-length":"17"}},"message":"RESPONSE"}
39
47
  ```
48
+
40
49
  - **API Track**, it contains data about the lifecycle of the request, including errors, if any
50
+
41
51
  ```
42
52
  {"level":30,"level_name":"INFO","hostname":"hostname","request_id":"5c_RLAnSS4y9-Q5STsJyiQ-0000000008","timestamp":1713869128434,"request":{"scheme":"http","method":"GET","path":"/this-path-must-not-exist","hostname":"localhost:80","query_params":{"status_code":"404","error_message":"Not Found"}},"response":{"status_code":404,"headers":{"content-type":"application/json; charset=utf-8","content-length":"107"}},"error":{"class":"REQUEST_ERROR","message":"Not Found","code":"FST_ERR_NOT_FOUND"},"message":"API_TRACK"}
43
53
  ```
@@ -36,6 +36,8 @@ describe("Logging entries when all works fine are the expected ones", () => {
36
36
  responseStatusCode: 200,
37
37
  expectedMessage: LogMessages.ApiTrack,
38
38
  });
39
+ const apiTrackEntry = JSON.parse(loggedRecords[2]);
40
+ assert.notNestedProperty(apiTrackEntry, "error");
39
41
  });
40
42
  });
41
43
 
@@ -1,9 +1,14 @@
1
- import type { FastifyServerOptions, FastifyInstance } from "fastify";
1
+ import type { FastifyInstance, FastifyServerOptions, RawServerBase } from "fastify";
2
+ import type { FastifyLoggerOptions, PinoLoggerOptions } from "fastify/types/logger.js";
2
3
  import { type DestinationStream } from "pino";
3
- import type { PinoLoggerOptions } from "fastify/types/logger.js";
4
4
  export declare const initializeLoggingHooks: (server: FastifyInstance) => void;
5
5
  export declare const getLoggingConfiguration: (customConfig?: {
6
6
  pinoOptions?: PinoLoggerOptions;
7
7
  loggerDestination?: DestinationStream;
8
+ additionalLoggerConfigs?: never;
9
+ } | {
10
+ pinoOptions?: never;
11
+ loggerDestination?: never;
12
+ additionalLoggerConfigs?: FastifyLoggerOptions<RawServerBase> & PinoLoggerOptions;
8
13
  }) => FastifyServerOptions;
9
14
  //# sourceMappingURL=fastify-logging-wrapper.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"fastify-logging-wrapper.d.ts","sourceRoot":"","sources":["../src/fastify-logging-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAcrE,OAAO,EAAQ,KAAK,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAEpD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAIjE,eAAO,MAAM,sBAAsB,WAAY,eAAe,KAAG,IA6BhE,CAAC;AAEF,eAAO,MAAM,uBAAuB,kBAAmB;IACrD,WAAW,CAAC,EAAE,iBAAiB,CAAC;IAChC,iBAAiB,CAAC,EAAE,iBAAiB,CAAC;CACvC,KAAG,oBAoBH,CAAC"}
1
+ {"version":3,"file":"fastify-logging-wrapper.d.ts","sourceRoot":"","sources":["../src/fastify-logging-wrapper.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,eAAe,EACf,oBAAoB,EACpB,aAAa,EACd,MAAM,SAAS,CAAC;AACjB,OAAO,KAAK,EACV,oBAAoB,EACpB,iBAAiB,EAClB,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EAAE,KAAK,iBAAiB,EAAQ,MAAM,MAAM,CAAC;AA6BpD,eAAO,MAAM,sBAAsB,WAAY,eAAe,KAAG,IAsChE,CAAC;AAEF,eAAO,MAAM,uBAAuB,kBAE9B;IACE,WAAW,CAAC,EAAE,iBAAiB,CAAC;IAChC,iBAAiB,CAAC,EAAE,iBAAiB,CAAC;IACtC,uBAAuB,CAAC,EAAE,KAAK,CAAC;CACjC,GACD;IACE,WAAW,CAAC,EAAE,KAAK,CAAC;IACpB,iBAAiB,CAAC,EAAE,KAAK,CAAC;IAC1B,uBAAuB,CAAC,EAAE,oBAAoB,CAAC,aAAa,CAAC,GAC3D,iBAAiB,CAAC;CACrB,KACJ,oBA2BF,CAAC"}
@@ -1,20 +1,38 @@
1
+ import { REQUEST_ID_HEADER } from "@ogcio/shared-errors";
1
2
  import hyperid from "hyperid";
3
+ import { pino } from "pino";
2
4
  import { LogMessages, REQUEST_ID_LOG_LABEL, } from "./logging-wrapper-entities.js";
3
5
  import { getLoggerConfiguration, getLoggingContextError, getPartialLoggingContextError, parseFullLoggingRequest, resetLoggingContext, setLoggingContext, } from "./logging-wrapper.js";
4
- import { pino } from "pino";
5
- import { REQUEST_ID_HEADER } from "@ogcio/shared-errors";
6
6
  const hyperidInstance = hyperid({ fixedLength: true, urlSafe: true });
7
+ const isObjectNotEmpty = (value) => {
8
+ return (value &&
9
+ typeof value === "object" &&
10
+ !Array.isArray(value) &&
11
+ Object.keys(Object.fromEntries(Object.entries(value).filter(([_, v]) => v !== undefined))).length > 0);
12
+ };
7
13
  export const initializeLoggingHooks = (server) => {
8
14
  server.addHook("preHandler", (request, _reply, done) => {
9
15
  setLoggingContext({ request });
10
- request.log.info({ request: parseFullLoggingRequest(request) }, LogMessages.NewRequest);
16
+ const requestParsed = parseFullLoggingRequest(request);
17
+ if (isObjectNotEmpty(requestParsed)) {
18
+ request.log.info({ request: requestParsed }, LogMessages.NewRequest);
19
+ }
20
+ else {
21
+ request.log.info(LogMessages.NewRequest);
22
+ }
11
23
  done();
12
24
  });
13
25
  server.addHook("onResponse", (_req, reply, done) => {
14
26
  setLoggingContext({ response: reply });
15
27
  reply.log.info(LogMessages.Response);
16
28
  // Include error in API Track if exists
17
- reply.log.info({ error: getPartialLoggingContextError() }, LogMessages.ApiTrack);
29
+ const error = getPartialLoggingContextError();
30
+ if (isObjectNotEmpty(error)) {
31
+ reply.log.info({ error: error }, LogMessages.ApiTrack);
32
+ }
33
+ else {
34
+ reply.log.info(LogMessages.ApiTrack);
35
+ }
18
36
  resetLoggingContext();
19
37
  done();
20
38
  });
@@ -25,7 +43,7 @@ export const initializeLoggingHooks = (server) => {
25
43
  });
26
44
  };
27
45
  export const getLoggingConfiguration = (customConfig) => {
28
- if (customConfig)
46
+ if (customConfig?.pinoOptions || customConfig?.loggerDestination)
29
47
  return {
30
48
  loggerInstance: pino({ ...getLoggerConfiguration(), ...(customConfig?.pinoOptions ?? {}) }, customConfig?.loggerDestination),
31
49
  disableRequestLogging: true,
@@ -33,8 +51,15 @@ export const getLoggingConfiguration = (customConfig) => {
33
51
  requestIdLogLabel: REQUEST_ID_LOG_LABEL,
34
52
  requestIdHeader: REQUEST_ID_HEADER,
35
53
  };
54
+ let loggerConfiguration = getLoggerConfiguration();
55
+ if (customConfig?.additionalLoggerConfigs) {
56
+ loggerConfiguration = {
57
+ ...loggerConfiguration,
58
+ ...customConfig?.additionalLoggerConfigs,
59
+ };
60
+ }
36
61
  return {
37
- logger: getLoggerConfiguration(),
62
+ logger: loggerConfiguration,
38
63
  disableRequestLogging: true,
39
64
  genReqId: () => hyperidInstance(),
40
65
  requestIdLogLabel: REQUEST_ID_LOG_LABEL,
@@ -1 +1 @@
1
- {"version":3,"file":"fastify-logging-wrapper.js","sourceRoot":"","sources":["../src/fastify-logging-wrapper.ts"],"names":[],"mappings":"AACA,OAAO,OAAO,MAAM,SAAS,CAAC;AAC9B,OAAO,EACL,WAAW,EACX,oBAAoB,GACrB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EACL,sBAAsB,EACtB,sBAAsB,EACtB,6BAA6B,EAC7B,uBAAuB,EACvB,mBAAmB,EACnB,iBAAiB,GAClB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,IAAI,EAA0B,MAAM,MAAM,CAAC;AACpD,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAGzD,MAAM,eAAe,GAAG,OAAO,CAAC,EAAE,WAAW,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;AAEtE,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,MAAuB,EAAQ,EAAE;IACtE,MAAM,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE;QACrD,iBAAiB,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;QAC/B,OAAO,CAAC,GAAG,CAAC,IAAI,CACd,EAAE,OAAO,EAAE,uBAAuB,CAAC,OAAO,CAAC,EA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CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;QAE7B,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,sBAAsB,EAAE,EAAE,EAAE,WAAW,CAAC,KAAK,CAAC,CAAC;QAE1E,IAAI,EAAE,CAAC;IACT,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,uBAAuB,GAAG,CACrC,YAWK,EACiB,EAAE;IACxB,IAAI,YAAY,EAAE,WAAW,IAAI,YAAY,EAAE,iBAAiB;QAC9D,OAAO;YACL,cAAc,EAAE,IAAI,CAClB,EAAE,GAAG,sBAAsB,EAAE,EAAE,GAAG,CAAC,YAAY,EAAE,WAAW,IAAI,EAAE,CAAC,EAAE,EACrE,YAAY,EAAE,iBAAiB,CAChC;YACD,qBAAqB,EAAE,IAAI;YAC3B,QAAQ,EAAE,GAAG,EAAE,CAAC,eAAe,EAAE;YACjC,iBAAiB,EAAE,oBAAoB;YACvC,eAAe,EAAE,iBAAiB;SACnC,CAAC;IAEJ,IAAI,mBAAmB,GAAG,sBAAsB,EAAE,CAAC;IACnD,IAAI,YAAY,EAAE,uBAAuB,EAAE,CAAC;QAC1C,mBAAmB,GAAG;YACpB,GAAG,mBAAmB;YACtB,GAAG,YAAY,EAAE,uBAAuB;SACzC,CAAC;IACJ,CAAC;IACD,OAAO;QACL,MAAM,EAAE,mBAAmB;QAC3B,qBAAqB,EAAE,IAAI;QAC3B,QAAQ,EAAE,GAAG,EAAE,CAAC,eAAe,EAAE;QACjC,iBAAiB,EAAE,oBAAoB;QACvC,eAAe,EAAE,iBAAiB;KACnC,CAAC;AACJ,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@ogcio/fastify-logging-wrapper",
3
- "version": "5.1.1",
3
+ "version": "5.2.2-canary-e6d877a",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
7
7
  "scripts": {
8
8
  "build": "rm -rf dist tsconfig.prod.tsbuildinfo tsconfig.tsbuildinfo && tsc -p tsconfig.prod.json",
9
- "test": "vitest run --coverage --outputFile=results.xml"
9
+ "test": "vitest run --coverage --outputFile=results.xml",
10
+ "prepublishOnly": "npm i && npm run build && npm run test"
10
11
  },
11
12
  "keywords": [],
12
13
  "author": {
@@ -17,9 +18,9 @@
17
18
  "description": "Enable standardized log entries for each request in fastify",
18
19
  "dependencies": {
19
20
  "@fastify/error": "^4.0.0",
20
- "@fastify/sensible": "^6.0.1",
21
- "@ogcio/shared-errors": "^1.0.0",
22
- "fastify": "^5.1.0",
21
+ "@fastify/sensible": "^6.0.2",
22
+ "@ogcio/shared-errors": "^1.1.0",
23
+ "fastify": "^5.2.1",
23
24
  "hyperid": "^3.3.0"
24
25
  },
25
26
  "devDependencies": {
@@ -1,5 +1,15 @@
1
- import type { FastifyServerOptions, FastifyInstance } from "fastify";
1
+ import { REQUEST_ID_HEADER } from "@ogcio/shared-errors";
2
+ import type {
3
+ FastifyInstance,
4
+ FastifyServerOptions,
5
+ RawServerBase,
6
+ } from "fastify";
7
+ import type {
8
+ FastifyLoggerOptions,
9
+ PinoLoggerOptions,
10
+ } from "fastify/types/logger.js";
2
11
  import hyperid from "hyperid";
12
+ import { type DestinationStream, pino } from "pino";
3
13
  import {
4
14
  LogMessages,
5
15
  REQUEST_ID_LOG_LABEL,
@@ -12,19 +22,34 @@ import {
12
22
  resetLoggingContext,
13
23
  setLoggingContext,
14
24
  } from "./logging-wrapper.js";
15
- import { pino, type DestinationStream } from "pino";
16
- import { REQUEST_ID_HEADER } from "@ogcio/shared-errors";
17
- import type { PinoLoggerOptions } from "fastify/types/logger.js";
18
25
 
19
26
  const hyperidInstance = hyperid({ fixedLength: true, urlSafe: true });
20
27
 
28
+ const isObjectNotEmpty = (value: object | undefined) => {
29
+ return (
30
+ value &&
31
+ typeof value === "object" &&
32
+ !Array.isArray(value) &&
33
+ Object.keys(
34
+ Object.fromEntries(
35
+ Object.entries(value).filter(([_, v]) => v !== undefined),
36
+ ),
37
+ ).length > 0
38
+ );
39
+ };
40
+
21
41
  export const initializeLoggingHooks = (server: FastifyInstance): void => {
22
42
  server.addHook("preHandler", (request, _reply, done) => {
23
43
  setLoggingContext({ request });
24
- request.log.info(
25
- { request: parseFullLoggingRequest(request) },
26
- LogMessages.NewRequest,
27
- );
44
+
45
+ const requestParsed = parseFullLoggingRequest(request);
46
+
47
+ if (isObjectNotEmpty(requestParsed)) {
48
+ request.log.info({ request: requestParsed }, LogMessages.NewRequest);
49
+ } else {
50
+ request.log.info(LogMessages.NewRequest);
51
+ }
52
+
28
53
  done();
29
54
  });
30
55
 
@@ -32,10 +57,14 @@ export const initializeLoggingHooks = (server: FastifyInstance): void => {
32
57
  setLoggingContext({ response: reply });
33
58
  reply.log.info(LogMessages.Response);
34
59
  // Include error in API Track if exists
35
- reply.log.info(
36
- { error: getPartialLoggingContextError() },
37
- LogMessages.ApiTrack,
38
- );
60
+
61
+ const error = getPartialLoggingContextError();
62
+ if (isObjectNotEmpty(error)) {
63
+ reply.log.info({ error: error }, LogMessages.ApiTrack);
64
+ } else {
65
+ reply.log.info(LogMessages.ApiTrack);
66
+ }
67
+
39
68
  resetLoggingContext();
40
69
  done();
41
70
  });
@@ -49,11 +78,21 @@ export const initializeLoggingHooks = (server: FastifyInstance): void => {
49
78
  });
50
79
  };
51
80
 
52
- export const getLoggingConfiguration = (customConfig?: {
53
- pinoOptions?: PinoLoggerOptions;
54
- loggerDestination?: DestinationStream;
55
- }): FastifyServerOptions => {
56
- if (customConfig)
81
+ export const getLoggingConfiguration = (
82
+ customConfig?:
83
+ | {
84
+ pinoOptions?: PinoLoggerOptions;
85
+ loggerDestination?: DestinationStream;
86
+ additionalLoggerConfigs?: never;
87
+ }
88
+ | {
89
+ pinoOptions?: never;
90
+ loggerDestination?: never;
91
+ additionalLoggerConfigs?: FastifyLoggerOptions<RawServerBase> &
92
+ PinoLoggerOptions;
93
+ },
94
+ ): FastifyServerOptions => {
95
+ if (customConfig?.pinoOptions || customConfig?.loggerDestination)
57
96
  return {
58
97
  loggerInstance: pino(
59
98
  { ...getLoggerConfiguration(), ...(customConfig?.pinoOptions ?? {}) },
@@ -65,8 +104,15 @@ export const getLoggingConfiguration = (customConfig?: {
65
104
  requestIdHeader: REQUEST_ID_HEADER,
66
105
  };
67
106
 
107
+ let loggerConfiguration = getLoggerConfiguration();
108
+ if (customConfig?.additionalLoggerConfigs) {
109
+ loggerConfiguration = {
110
+ ...loggerConfiguration,
111
+ ...customConfig?.additionalLoggerConfigs,
112
+ };
113
+ }
68
114
  return {
69
- logger: getLoggerConfiguration(),
115
+ logger: loggerConfiguration,
70
116
  disableRequestLogging: true,
71
117
  genReqId: () => hyperidInstance(),
72
118
  requestIdLogLabel: REQUEST_ID_LOG_LABEL,
package/tsconfig.json CHANGED
@@ -9,4 +9,4 @@
9
9
  "references": [
10
10
  { "path": "../shared-errors" }
11
11
  ]
12
- }
12
+ }