From 6ec5dc06d4595bdafc4c4cb74d53a797289e3c5b Mon Sep 17 00:00:00 2001 From: "Aman Jain (ECS/DEL)" Date: Wed, 21 Sep 2022 14:32:42 +0530 Subject: [PATCH 1/7] fix: updated the lib directory --- lib/index.d.ts | 4 +++- lib/index.js | 7 ++++--- package.json | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/index.d.ts b/lib/index.d.ts index 4a629e0..67e0f3a 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -1,2 +1,4 @@ import winston from "winston"; -export declare const logger: winston.Logger; +export declare const createLogger: ({ logLevel }: { + logLevel: string; +}) => winston.Logger; diff --git a/lib/index.js b/lib/index.js index 2a5a81f..aec562c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,12 +3,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.logger = void 0; +exports.createLogger = void 0; const winston_1 = __importDefault(require("winston")); const { combine, errors, timestamp, splat, json } = winston_1.default.format; -exports.logger = winston_1.default.createLogger({ +const createLogger = ({ logLevel = "info" }) => winston_1.default.createLogger({ // default log level is "info" - level: "info", + level: logLevel, // combining multiple formats to get the desired output format: combine( // required to log errors thrown by the application; ignored otherwise @@ -31,3 +31,4 @@ exports.logger = winston_1.default.createLogger({ // generic metadata applied to all logs defaultMeta: { type: "application" } }); +exports.createLogger = createLogger; diff --git a/package.json b/package.json index b9b4faa..891a1f7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "logger-safe-security", - "version": "1.1.0", + "version": "1.1.1", "description": "Custom logging framework used in SAFE", "main": "lib/index.js", "types": "lib", From 4b96c8e32d8f5e2d27f1e5662e95f8b2de458547 Mon Sep 17 00:00:00 2001 From: "Aman Jain (ECS/DEL)" Date: Wed, 21 Sep 2022 19:37:52 +0530 Subject: [PATCH 2/7] feat: added support to include service --- README.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a873f1c..20ae757 100644 --- a/README.md +++ b/README.md @@ -37,13 +37,11 @@ const logger = createLogger({ logLevel: "info" }); logger.info("Hello world!"); ``` -Additionally, one can create child loggers inherited from the parent/package-default to pass specific metadata. -One such use case would be to add `service` metadata while importing it within the context of a service. For example: +Additionally, one can pass `service` metadata while importing it within the context of a service. For example: ```js const { createLogger } = require("logger-safe-security"); -const logger = createLogger(); -const serviceLogger = logger.child({ service: "sample" }); -serviceLogger.info("This log line will include service metadata"); +const logger = createLogger({ service: "sample" }); +logger.info("This log line will include service metadata"); ``` ## Definition From 4251283bdfe2942b314a96bb9be8d0675489b3ef Mon Sep 17 00:00:00 2001 From: "Aman Jain (ECS/DEL)" Date: Thu, 22 Sep 2022 19:40:07 +0530 Subject: [PATCH 3/7] fix: updated logger not to exit after logging an uncaughtException --- src/index.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/index.ts b/src/index.ts index eab2bc6..a3ea17e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -45,6 +45,9 @@ export const createLogger = ( }) ], + // do not exit the process after logging an uncaughtException + exitOnError: false, + // generic metadata applied to all logs defaultMeta: { type: "application", ...(service && { service }) } }); From 9209c35715acdd43e8307b1d818a5ccfa180f887 Mon Sep 17 00:00:00 2001 From: "Aman Jain (ECS/DEL)" Date: Thu, 22 Sep 2022 19:45:55 +0530 Subject: [PATCH 4/7] fix: updated logger not to exit after logging an uncaughtException --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8e78116..2f96747 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "logger-safe-security", - "version": "1.1.3", + "version": "1.1.4", "description": "Custom logging framework used in SAFE", "main": "lib/index.js", "types": "lib", From 4ff50d6744ea1f6809ef68f0b19a017ee946e403 Mon Sep 17 00:00:00 2001 From: "Aman Jain (ECS/DEL)" Date: Mon, 26 Sep 2022 17:04:42 +0530 Subject: [PATCH 5/7] feat: change the format for messageMetadata --- src/index.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index a3ea17e..5056de0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,6 +6,21 @@ export const levels = winston.config.npm.levels; export type Logger = winston.Logger; +const formatMessageMetadata = winston.format( + ({ message, level, service, type, ...rest }) => { + return { + message, + level, + type, + ...(service && { service }), + messageMetadata: rest, + // this is required as per the winston documentation: https://github.com/winstonjs/winston#streams-objectmode-and-info-objects + [Symbol.for("level")]: level, + [Symbol.for("message")]: message + }; + } +); + export const createLogger = ( { logLevel = "info", @@ -21,6 +36,9 @@ export const createLogger = ( // combining multiple formats to get the desired output format: combine( + // moves all the other fields in the the message to `messageMetadata` property + formatMessageMetadata(), + // required to log errors thrown by the application; ignored otherwise errors({ stack: true }), @@ -47,7 +65,8 @@ export const createLogger = ( // do not exit the process after logging an uncaughtException exitOnError: false, - + // generic metadata applied to all logs defaultMeta: { type: "application", ...(service && { service }) } }); + From 810cd52922e8aead4f148d9952d1918ae0d79a14 Mon Sep 17 00:00:00 2001 From: "Aman Jain (ECS/DEL)" Date: Mon, 26 Sep 2022 17:06:31 +0530 Subject: [PATCH 6/7] fix: updated the version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2f96747..80f8f19 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "logger-safe-security", - "version": "1.1.4", + "version": "1.1.5", "description": "Custom logging framework used in SAFE", "main": "lib/index.js", "types": "lib", From 4505b08be6a5768334e06de7096d4f2b29677fc4 Mon Sep 17 00:00:00 2001 From: "Aman Jain (ECS/DEL)" Date: Mon, 26 Sep 2022 19:05:46 +0530 Subject: [PATCH 7/7] feat: updated logger to user metadata --- src/index.ts | 38 ++++++++++++-------------------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/src/index.ts b/src/index.ts index 5056de0..7eb4d6d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,52 +1,39 @@ import winston from "winston"; -const { combine, errors, timestamp, splat, json } = winston.format; +const { combine, errors, timestamp, splat, json, metadata } = winston.format; export const levels = winston.config.npm.levels; export type Logger = winston.Logger; -const formatMessageMetadata = winston.format( - ({ message, level, service, type, ...rest }) => { - return { - message, - level, - type, - ...(service && { service }), - messageMetadata: rest, - // this is required as per the winston documentation: https://github.com/winstonjs/winston#streams-objectmode-and-info-objects - [Symbol.for("level")]: level, - [Symbol.for("message")]: message - }; - } -); - export const createLogger = ( { - logLevel = "info", + level = "info", service }: { - logLevel?: string; + level?: string; service?: string; - } = { logLevel: "info" } + } = { level: "info" } ): winston.Logger => winston.createLogger({ // default log level is "info" - level: logLevel, + level, // combining multiple formats to get the desired output format: combine( - // moves all the other fields in the the message to `messageMetadata` property - formatMessageMetadata(), - // required to log errors thrown by the application; ignored otherwise errors({ stack: true }), + // adds timestamp to all log messages + timestamp(), + // enables string interpolation of messages splat(), - // adds timestamp to all log messages - timestamp(), + // moves all the other fields in the message to `metadata` property + metadata({ + fillExcept: ["message", "level", "timestamp", "service", "type"] + }), // default log format is JSON json() @@ -69,4 +56,3 @@ export const createLogger = ( // generic metadata applied to all logs defaultMeta: { type: "application", ...(service && { service }) } }); -