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

Improve context.runtime.file when logtail is called indirectly #89

Closed
gustavopch opened this issue Oct 18, 2023 · 4 comments · Fixed by #93
Closed

Improve context.runtime.file when logtail is called indirectly #89

gustavopch opened this issue Oct 18, 2023 · 4 comments · Fixed by #93

Comments

@gustavopch
Copy link
Contributor

Suppose I have a file named utils/logging.ts containing:

export const log = (message, context) => {
  logtail.info(message, context)
}

If I import and call log('Hello') from a file named foo.js, the context.runtime.file is set to utils/logging.js. I wanted it to be foo.js instead.

@curusarn
Copy link
Contributor

curusarn commented Oct 23, 2023

Hi @gustavopch,

Thank you for reaching out!
You can customize which file and function becomes part of the log context.

Here's an example of file logging.js with simple function for logging:

const { Logtail } = require("@logtail/node");
const { LogLevel } = require("@logtail/types");

module.exports = {
    log(msg) {
        const token = process.env.BETTER_STACK_SOURCE_TOKEN;
        const logtail = new Logtail(token, {level: "debug"});
        const stackContextHint = { fileName: "logging", methodNames: [ "log" ] };
        logtail.log(msg, "info", { key: "value" }, stackContextHint);
    },
};

Note that you need to use logtail.log() and pass stackContextHint as an argument.
Could you try this out and let me know if it works for you? 🙏

Thanks again for your question!

@gustavopch
Copy link
Contributor Author

Hi @curusarn, thanks for the information. I think it's missing from the docs. I tried what you suggested, but it didn't point to the line/function that I was expecting. I ended up passing the source location manually to context. It's super simple, looks like new Error().stack?.split('\n')[3]?.trim(), and works on the browser too – by inspecting this repo, it seems @logtail/browser doesn't include that logic.

@PetrHeinz
Copy link
Member

Hi @gustavopch, thanks for opening the issue 🙌

It's tru that there were some issues in how StackContextHint was handled at the moment. I've fixed them in #93 and added a usage example to our example project. I'm planning on mentioning it in the docs as well.

I hope it would work for you after the changes, works nicely in example project. 🙏

The reason we're not using new Error().stack in browser environment is that the output is not standardized at the moment. Each browser has slightly different syntax. Also, code used in browser env is often going through a build process and/or minification, which often makes the results unusable.

However, if new Error().stack works for your use cases, feel free to continue using it in your custom context 👍

@gustavopch
Copy link
Contributor Author

Hi @PetrHeinz, got it, thanks for the explanation.

Also, code used in browser env is often going through a build process and/or minification, which often makes the results unusable.

FWIW, source map supported (as asked in #63) would help on this specific part.

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

Successfully merging a pull request may close this issue.

3 participants