-
Notifications
You must be signed in to change notification settings - Fork 13
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
Add new package supporting Edge runtime (eg. Cloudflare Workers) #78
Conversation
e4695be
to
1acc0aa
Compare
This reverts commit 1489651.
- see https://endoflife.date/nodejs - the package should still work on Node 14.x, it just cannot be built in a monorepo along with @edge-runtime/jest-environment
Example usage in Cloudflare Worker: import { Logtail } from "@logtail/edge"
const baseLogger = new Logtail("<BETTER_STACK_SOURCE_TOKEN>")
export default {
async fetch(request, env, ctx) {
const logger = baseLogger.withExecutionContext(ctx);
logger.info("Logging with structured data.", { field: "context value", myArray: [1, 2, 3] });
logger.warn("Warning: This function is just Hello World.");
return new Response('Hello world!');
},
}; |
packages/edge/src/edge.ts
Outdated
this._warnedAboutMissingCtx = true; | ||
console.warn( | ||
"ExecutionContext hasn't been set via `withExecutionContext` method. Logs may not reach Better Stack unless you manually call `ctx.waitUntil(log)`.", | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be quite dangerous - if there's no ctx.waitUntil
, await logger.log
or await logger.flush
it would mean the logs will probably not get sent to Better Stack. The warning is also quite hard to notice (depending on how you run it).
Maybe this should throw an exception instead to make sure it's correctly set up...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason why people might not want to use the withExecutionContext
? E.g. maybe there could be workers that doesn't make the context easily available?
I would consider throwing an exception if there is never a good reason to not use withExecutionContext
.
But it's still a little weird for logging library to bring down people's worker code.
How about send the warning to both console and Better Stack? Plus flush after the warning to make sure people at least see the warning in Live tail. I imagine seeing the warning instead of actual logs in Live tail could work well.
Also could you make sure that the warning gives people concrete instructions how to fix the issue? 🙏
E.g. now I feel like the warning tells me that the easiest solution is to add ctx.waitUntil(log)
to my worker code. But the recommended way is to use withExecutionContext()
.
Could you change the warning to make it clearer how to fix the issue? Link to docs page might work well. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the warning + flush method, I'll just have to test it...
I don't see a good reason not use withExecutionContext
, but then again, I might be mistaken. Maybe in some future version we'll make a feature toggle for disabling the warning.
And I'll reword it to make sure it is understandable 👍 Thanks for the feedback.
{ | ||
"name": "@logtail/edge", | ||
"version": "0.4.6", | ||
"description": "Logtail.com - Edge runtime logger", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"description": "Logtail.com - Edge runtime logger", | |
"description": "Better Stack Edge runtime logger (formerly Logtail)", |
Let's please use Better Stack instead of Logtail. 🙏
Could you also add "Better Stack" and "Better Stack Logs" to keywords (keep Logtail)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll unify that in a new PR, it's still Logtail everywhere, probably
"serverless", | ||
"Edge" | ||
], | ||
"author": "Logtail <hello@logtail.com>", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better Stack <hello@betterstack.com>
🙏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some comments regarding package description and the "edge warning behavior". Looks good overall! Thanks.
New package supporting running in edge runtime, such as Cloudflare Workers.
Resolves #62