@ogcio/fastify-logging-wrapper 5.1.2 → 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 +1 @@
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;AAgBpD,eAAO,MAAM,sBAAsB,WAAY,eAAe,KAAG,IA6BhE,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
+ {"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"}
@@ -4,17 +4,35 @@ import { pino } from "pino";
4
4
  import { LogMessages, REQUEST_ID_LOG_LABEL, } from "./logging-wrapper-entities.js";
5
5
  import { getLoggerConfiguration, getLoggingContextError, getPartialLoggingContextError, parseFullLoggingRequest, resetLoggingContext, setLoggingContext, } from "./logging-wrapper.js";
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
  });
@@ -1 +1 @@
1
- {"version":3,"file":"fastify-logging-wrapper.js","sourceRoot":"","sources":["../src/fastify-logging-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAUzD,OAAO,OAAO,MAAM,SAAS,CAAC;AAC9B,OAAO,EAA0B,IAAI,EAAE,MAAM,MAAM,CAAC;AACpD,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;AAE9B,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,OA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AC3B,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,6 +1,6 @@
1
1
  {
2
2
  "name": "@ogcio/fastify-logging-wrapper",
3
- "version": "5.1.2",
3
+ "version": "5.2.2-canary-e6d877a",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
@@ -18,9 +18,9 @@
18
18
  "description": "Enable standardized log entries for each request in fastify",
19
19
  "dependencies": {
20
20
  "@fastify/error": "^4.0.0",
21
- "@fastify/sensible": "^6.0.1",
22
- "@ogcio/shared-errors": "^1.0.0",
23
- "fastify": "^5.1.0",
21
+ "@fastify/sensible": "^6.0.2",
22
+ "@ogcio/shared-errors": "^1.1.0",
23
+ "fastify": "^5.2.1",
24
24
  "hyperid": "^3.3.0"
25
25
  },
26
26
  "devDependencies": {
@@ -25,13 +25,31 @@ import {
25
25
 
26
26
  const hyperidInstance = hyperid({ fixedLength: true, urlSafe: true });
27
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
+
28
41
  export const initializeLoggingHooks = (server: FastifyInstance): void => {
29
42
  server.addHook("preHandler", (request, _reply, done) => {
30
43
  setLoggingContext({ request });
31
- request.log.info(
32
- { request: parseFullLoggingRequest(request) },
33
- LogMessages.NewRequest,
34
- );
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
+
35
53
  done();
36
54
  });
37
55
 
@@ -39,10 +57,14 @@ export const initializeLoggingHooks = (server: FastifyInstance): void => {
39
57
  setLoggingContext({ response: reply });
40
58
  reply.log.info(LogMessages.Response);
41
59
  // Include error in API Track if exists
42
- reply.log.info(
43
- { error: getPartialLoggingContextError() },
44
- LogMessages.ApiTrack,
45
- );
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
+
46
68
  resetLoggingContext();
47
69
  done();
48
70
  });
package/tsconfig.json CHANGED
@@ -9,4 +9,4 @@
9
9
  "references": [
10
10
  { "path": "../shared-errors" }
11
11
  ]
12
- }
12
+ }