Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update logger to use metadata #12

Merged
merged 1 commit into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -24,11 +24,16 @@ export const createLogger = (
// 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" });