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: updated logger to use metadata #9

Merged
merged 10 commits into from
Sep 26, 2022
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.4",
"version": "1.1.5",
"description": "Custom logging framework used in SAFE",
"main": "lib/index.js",
"types": "lib",
Expand Down
21 changes: 13 additions & 8 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 All @@ -47,7 +52,7 @@ 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 }) }
});