Skip to content

Commit

Permalink
feat: Update logger to use metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
prathap-safe committed Sep 26, 2022
1 parent 366809d commit d8c2924
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
19 changes: 12 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,34 +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;

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()
Expand Down
8 changes: 4 additions & 4 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -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");
logger.info("This is a sample logger");
logger.info("This is a sample logger with more information", { planet: "Earth" });
8 changes: 4 additions & 4 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -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");
logger.info("This is a sample logger");
logger.info("This is a sample logger with more information", { planet: "Earth" });

0 comments on commit d8c2924

Please sign in to comment.