Skip to content

Commit 1f66fe4

Browse files
authored
feat: Update logger to use metadata (#12)
1 parent 366809d commit 1f66fe4

File tree

5 files changed

+18
-12
lines changed

5 files changed

+18
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,4 @@ The logger exported as part of this package contains the following definition:
5959
- Metadata:
6060
- `{ "type" : "application" }` is added as default metadata to easily identify application logs. This can further be extended to differentiate
6161
from audit logs and add parsing/filtering rules (for e.g. in Datadog) as needed.
62+
- Additionally, all other metadata passed to the log messages will be stored under `metadata` property for easy identification & grouping.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "logger-safe-security",
3-
"version": "1.1.7",
3+
"version": "1.1.8",
44
"description": "Custom logging framework used in SAFE",
55
"main": "lib/index.js",
66
"types": "lib",

src/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import winston from "winston";
22

3-
const { combine, errors, timestamp, splat, json } = winston.format;
3+
const { combine, errors, timestamp, splat, json, metadata } = winston.format;
44

55
export const levels = winston.config.npm.levels;
66

@@ -24,11 +24,16 @@ export const createLogger = (
2424
// required to log errors thrown by the application; ignored otherwise
2525
errors({ stack: true }),
2626

27+
// adds timestamp to all log messages
28+
timestamp(),
29+
2730
// enables string interpolation of messages
2831
splat(),
2932

30-
// adds timestamp to all log messages
31-
timestamp(),
33+
// moves all the other fields in the message to `metadata` property
34+
metadata({
35+
fillExcept: ["message", "level", "timestamp", "service", "type"]
36+
}),
3237

3338
// default log format is JSON
3439
json()

test/index.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { logger } = require("logger-safe-security").default;
2-
const appLogger = logger.child({ service: "sample" });
1+
const { createLogger } = require("logger-safe-security");
2+
const logger = createLogger({ service: "sample" });
33

4-
logger.info("This is a parent logger");
5-
appLogger.info("This is a child logger");
4+
logger.info("This is a sample logger");
5+
logger.info("This is a sample logger with more information", { planet: "Earth" });

test/index.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { logger } from "logger-safe-security";
2-
const appLogger = logger.child({ service: "sample" });
1+
import { createLogger } from "logger-safe-security";
2+
const logger = createLogger({ service: "sample" });
33

4-
logger.info("This is a parent logger");
5-
appLogger.info("This is a child logger");
4+
logger.info("This is a sample logger");
5+
logger.info("This is a sample logger with more information", { planet: "Earth" });

0 commit comments

Comments
 (0)