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

BREAKING CHANGE: add support to log tenantId #17

Merged
merged 6 commits into from
Dec 13, 2022
Merged
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
59 changes: 55 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ export const levels = winston.config.npm.levels;

export type Logger = winston.Logger;

interface ParameterConfig {
[key: string]: {
/**
* Refers the return value of a custom method, in case a value is not found, it should throw an error
* to be able to use the fallback value instead
*/
valueFromMethod: <T>(arg: string) => T;
/**
* This value will be used if the function passed to the valueFromMethod throws an Exception
*/
fallback?: string;
};
}
interface ConfigParams {
parameters: ParameterConfig;
}

/* A custom format that is used to format the error object. */
const formatError = winston.format(info => {
if ("error" in info && info.error instanceof Error) {
Expand All @@ -18,16 +35,47 @@ const formatError = winston.format(info => {
return info;
});

/* A custom format that is used to include config parameters. */
const formatConfigParams = (parameters: ParameterConfig | undefined) => {
return winston.format(info => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove the dependency on the tenantId and make this generic?
From the parameters config, get the keys, check if the key is present in the store, if yes, add it to info else skip.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the PR title with breaking changes.

if (parameters) {
Object.keys(parameters).forEach(key => {
if (parameters[key]) {
const { valueFromMethod, fallback } = parameters[key];
try {
if (
typeof valueFromMethod === "function" &&
typeof valueFromMethod<string>(key) === "string"
) {
info[key] = valueFromMethod<string>(key);
}
} catch (error) {
if (fallback) {
info[key] = fallback;
}
}
}
});
}

return info;
});
};

export const createLogger = (
{
logLevel = "info",
service
service,
config
}: {
logLevel?: string;
service?: string;
config?: ConfigParams | undefined;
} = { logLevel: "info" }
): winston.Logger =>
winston.createLogger({
): winston.Logger => {
const parameters = config && config.parameters as ParameterConfig | undefined;

return winston.createLogger({
// default log level is "info"
level: logLevel,

Expand Down Expand Up @@ -56,6 +104,9 @@ export const createLogger = (
// custom formatter to format the "error" property
formatError(),

// custom formatter to format the config parameters
formatConfigParams(parameters)(),

// default log format is JSON
json()
),
Expand All @@ -76,4 +127,4 @@ export const createLogger = (

// generic metadata applied to all logs
defaultMeta: { type: "application", ...(service && { service }) }
});
})};