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: add system logger #457

Merged
merged 10 commits into from
Jan 18, 2024
Merged

feat: add system logger #457

merged 10 commits into from
Jan 18, 2024

Conversation

Skn0tt
Copy link
Contributor

@Skn0tt Skn0tt commented Jan 17, 2024

Adds a system logger for internal usage, closely modeled after the edge-functions one.

@Skn0tt Skn0tt requested a review from eduardoboucas January 17, 2024 10:46
@Skn0tt Skn0tt self-assigned this Jan 17, 2024
@Skn0tt Skn0tt requested a review from a team as a code owner January 17, 2024 10:46
) {}

private doLog(logger: typeof console.log, message: string) {
if (this.samplingRate < 1 && Math.random() > this.samplingRate) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added support for sampling here, because I feel like it's a useful thing for collecting metrics without increasing the log volume too much. The more I think about it, though, the more I feel like the sampling rate should be set on a per-log basis, instead of on a per-logger basis. @eduardoboucas curious what you think - is it useful to have sampling built-in to the logger? If yes, should we also add it to the edge functions logger?

Copy link
Member

Choose a reason for hiding this comment

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

I think this adds unnecessary complexity at this point.

// eslint pretends there's a different enum at the same place - it's wrong!
// eslint-disable-next-line no-shadow
export enum LogLevel {
Debug = 1,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The edge functions logger has a simpler "Debug/Log/Error" hierarchy here. Since log levels were introduced there only very recently, we can still change what's being used here. I picked those levels because they match the Go levels. @eduardoboucas do you see any value in having an additional Warn level and using the Info name? Should we change this to be Debug/Log/Error as well?

Copy link
Member

Choose a reason for hiding this comment

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

No strong opinions. I personally don't feel the need for extra levels, and it's not clear to me when we'd use warn vs. info.

If we do it, do we make log() an alias of info()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Then let's stick with Debug/Log/Error for now - we can always add the additional levels in the future.

If we do it, do we make log() an alias of info()?

That's the current impl, yes.

Copy link
Member

Choose a reason for hiding this comment

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

Oops, missed that part!

return this.withFields(fields)
}

withRequest(req: Request) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Edge-Utils only has a withRequestID function, not a withRequest one. I've opened a PR over there to also add withRequest, so both our loggers adhere to the same shape: https://github.com/netlify/edge-utils/pull/36

@Skn0tt
Copy link
Contributor Author

Skn0tt commented Jan 17, 2024

Removed sampling, withRequest, and reduced the number of log levels. Let's start small!

lukasholzer
lukasholzer previously approved these changes Jan 18, 2024
Copy link
Contributor

@lukasholzer lukasholzer left a comment

Choose a reason for hiding this comment

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

Have you created an rc release and tested it?

package.json Outdated
@@ -9,6 +9,10 @@
"dist/**/*.d.ts",
"types/**/*.d.ts"
],
"exports": {
".": "./dist/main.js",
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice way to gate the internal stuff I like!


class SystemLogger {
// eslint-disable-next-line no-useless-constructor
constructor(private readonly fields: Record<string, unknown> = {}, private readonly logLevel = LogLevel.Log) {}
Copy link
Member

Choose a reason for hiding this comment

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

I'm confused. Don't we need to store these things in the class?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

adding private before a constructor parameter makes it a class field

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Interesting. To me we're left with something that is less expressive and an empty constructor that we need an ESLint exception for. 🤷🏻

Copy link
Contributor Author

Choose a reason for hiding this comment

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

there you go: 139158e 😁

@Skn0tt
Copy link
Contributor Author

Skn0tt commented Jan 18, 2024

Have you created an rc release and tested it?

Tried locally with npm pack, and the packaging didn't work ... Apparently exports only really works well for ESM packages. We'll have to stick with @netlify/functions/dist/internal for now!

The system logging itself works perfectly.

@Skn0tt Skn0tt merged commit f09ddc4 into main Jan 18, 2024
6 checks passed
@Skn0tt Skn0tt deleted the system-logger branch January 18, 2024 12:52
Skn0tt pushed a commit that referenced this pull request Jan 18, 2024
🤖 I have created a release *beep* *boop*
---


## [2.5.0](v2.4.1...v2.5.0)
(2024-01-18)


### Features

* add system logger
([#457](#457))
([f09ddc4](f09ddc4))


### Bug Fixes

* **deps:** update dependency @netlify/serverless-functions-api to
v1.13.0 ([#451](#451))
([d15b673](d15b673))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: token-generator-app[bot] <82042599+token-generator-app[bot]@users.noreply.github.com>
Skn0tt added a commit that referenced this pull request Jan 19, 2024
Follow-up to #457. The system
logger shouldn't be visible in local dev.
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 this pull request may close these issues.

3 participants