From d8c29242e7678fa61aa662238628f9d1e2723ddf Mon Sep 17 00:00:00 2001 From: prathap-safe Date: Mon, 26 Sep 2022 23:58:29 +0530 Subject: [PATCH] feat: Update logger to use metadata --- README.md | 1 + package.json | 2 +- src/index.ts | 19 ++++++++++++------- test/index.test.js | 8 ++++---- test/index.test.ts | 8 ++++---- 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 20ae757..3a95d40 100644 --- a/README.md +++ b/README.md @@ -59,3 +59,4 @@ The logger exported as part of this package contains the following definition: - Metadata: - `{ "type" : "application" }` is added as default metadata to easily identify application logs. This can further be extended to differentiate from audit logs and add parsing/filtering rules (for e.g. in Datadog) as needed. + - Additionally, all other metadata passed to the log messages will be stored under `metadata` property for easy identification & grouping. \ No newline at end of file diff --git a/package.json b/package.json index 410366c..614f426 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "logger-safe-security", - "version": "1.1.7", + "version": "1.1.8", "description": "Custom logging framework used in SAFE", "main": "lib/index.js", "types": "lib", diff --git a/src/index.ts b/src/index.ts index a3ea17e..44b4799 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ 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; @@ -8,27 +8,32 @@ export type Logger = winston.Logger; 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( // 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() diff --git a/test/index.test.js b/test/index.test.js index 69a7607..5842960 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,5 +1,5 @@ -const { logger } = require("logger-safe-security").default; -const appLogger = logger.child({ service: "sample" }); +const { createLogger } = require("logger-safe-security"); +const logger = createLogger({ service: "sample" }); -logger.info("This is a parent logger"); -appLogger.info("This is a child logger"); \ No newline at end of file +logger.info("This is a sample logger"); +logger.info("This is a sample logger with more information", { planet: "Earth" }); \ No newline at end of file diff --git a/test/index.test.ts b/test/index.test.ts index 42fc373..035c22e 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,5 +1,5 @@ -import { logger } from "logger-safe-security"; -const appLogger = logger.child({ service: "sample" }); +import { createLogger } from "logger-safe-security"; +const logger = createLogger({ service: "sample" }); -logger.info("This is a parent logger"); -appLogger.info("This is a child logger"); \ No newline at end of file +logger.info("This is a sample logger"); +logger.info("This is a sample logger with more information", { planet: "Earth" }); \ No newline at end of file