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

Feature Request: [Feature] How to inject extra info to the logs #284

Open
jwielebnowski opened this issue May 23, 2024 · 1 comment
Open
Labels
enhancement New feature or request

Comments

@jwielebnowski
Copy link

I am trying to include extra information in every log. I have tried to do that with the overwrite settings but nothing seems to work. Here is an example how I am trying to do it:

import { ILogObj, ILogObjMeta, Logger } from 'tslog';

let type: 'json' | 'pretty' = 'pretty';

// Use the lease url as a proxy for running on AWS.
// Send machine readable logs.
if (process.env.PUREWEB_LEASE_URL) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
type = 'json';
}
const ipRegex = /\b\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\b/g;
interface ICustomLogObj extends ILogObj {
launchRequestId?: string;
}

let launchRequestId: string | undefined;

const logger: Logger = new Logger({
maskValuesRegEx: [ipRegex],
type: type,
overwrite: {
addMeta: (logObj: any, logLevelId: number, logLevelName: string) => {
logObj[0] = logObj[0] + ' launchRequestId:' + launchRequestId;
return { logObj, _meta: { logLevelId, logLevelName, launchRequestId } };
}
}
});
export const setLaunchRequestId = (id: string) => {
launchRequestId = id;
};

export default logger;

What I need is when I issue any log eg: logger.info('test log'), the log also contains launchRequestId. How this can be done?

@jwielebnowski jwielebnowski added the enhancement New feature or request label May 23, 2024
@shimms
Copy link

shimms commented Aug 12, 2024

We're looking at the same thing. addMeta doesn't include the default meta and only includes what is returned from this method.

The default meta method has access to the runtime to populate default meta values, which I can't see an easy way to get from a addMeta implementation.

I'm updating addMeta to optionally fetch the default meta and include it in the logObj.[metaProperty] object passed into addMeta for a project. Will open a PR for the change to see if there is any interest in incorporating it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants