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(logger): simplify logger API #2344

Merged
merged 8 commits into from
Jul 11, 2021
Merged

Conversation

balazsorban44
Copy link
Member

@balazsorban44 balazsorban44 commented Jul 10, 2021

Similar to #2342, this aims to unify the user-facing API and provide an easier way to extend in the future.

In addition, this PR also solves the problem when the logger.error method sometimes did not print results, because Error instances are not serializable and will be printed as empty objects "{}".

After this PR, we make any Error instances serializable as described here: https://iaincollins.medium.com/error-handling-in-javascript-a6172ccdf9af

Closes #1602
Achieved by adding a client: true flag when logs are coming from the frontend.

BREAKING CHANGE:

The main change is that instead of an unknown number of parameters, the log events have at most two, where the second parameter is usually an object. In the case of the error event, it can also be an Error instance (that is serializable by JSON.stringify). If it is an object, an Error instance will be available on metadata.error, and message will default to metadata.error.message. This is done so that an error event always provides some kind of a stack to see where the error happened

// [...nextauth.js]
import log from "some-logger-service"
...
logger: {
- error(code, ...message) {},
+ error(code, metadata) {},
- warn(code, ...message) {},
+ warn(code) {}
- debug(code, ...message) {}
+ debug(code, metadata) {}
}

@vercel
Copy link

vercel bot commented Jul 10, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/nextauthjs/next-auth/9REUgGGDfCkWgh9mdGaKguGRpAHu
✅ Preview: https://next-auth-git-feat-logger-named-params-nextauthjs.vercel.app

@github-actions github-actions bot added client Client related code core Refers to `@auth/core` hacktoberfest-docs Relates to documentation TypeScript Issues relating to TypeScript labels Jul 10, 2021
These adapter specific errors are now generated on-the-fly.
@vercel vercel bot temporarily deployed to Preview July 10, 2021 13:53 Inactive
@balazsorban44 balazsorban44 changed the title Feat/logger named params feat(logger): simplify logger API Jul 10, 2021
@vercel vercel bot temporarily deployed to Preview July 10, 2021 14:29 Inactive
@codecov-commenter
Copy link

codecov-commenter commented Jul 10, 2021

Codecov Report

Merging #2344 (eb0f160) into next (6911dd9) will increase coverage by 0.10%.
The diff coverage is 9.52%.

Impacted file tree graph

@@            Coverage Diff             @@
##             next    #2344      +/-   ##
==========================================
+ Coverage   10.78%   10.89%   +0.10%     
==========================================
  Files          83       83              
  Lines        1409     1395      -14     
  Branches      389      389              
==========================================
  Hits          152      152              
+ Misses       1042     1026      -16     
- Partials      215      217       +2     
Impacted Files Coverage Δ
src/lib/errors.js 0.00% <ø> (ø)
src/server/index.js 0.00% <0.00%> (ø)
src/server/lib/oauth/callback.js 0.00% <0.00%> (ø)
src/server/lib/oauth/client.js 0.00% <0.00%> (ø)
src/server/lib/oauth/pkce-handler.js 0.00% <0.00%> (ø)
src/server/lib/oauth/state-handler.js 0.00% <0.00%> (ø)
src/server/lib/signin/oauth.js 0.00% <0.00%> (ø)
src/server/routes/callback.js 0.00% <ø> (ø)
src/lib/logger.js 21.05% <5.26%> (-4.76%) ⬇️
src/client/react.js 85.35% <100.00%> (+1.07%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6911dd9...eb0f160. Read the comment docs.

@balazsorban44 balazsorban44 marked this pull request as ready for review July 10, 2021 14:31
Copy link
Collaborator

@ubbe-xyz ubbe-xyz left a comment

Choose a reason for hiding this comment

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

Super 💪🏽 💯

src/client/react.js Show resolved Hide resolved
types/index.d.ts Outdated Show resolved Hide resolved
mnphpexpert added a commit to mnphpexpert/next-auth that referenced this pull request Sep 2, 2024
Similar to nextauthjs#2342, this aims to unify the user-facing API and provide an easier way to extend in the future.

In addition, this PR also solves the problem when the `logger.error` method sometimes did not print results, because `Error` instances are not serializable and will be printed as empty objects `"{}"`.

After this PR, we make any `Error` instances serializable as described here: https://iaincollins.medium.com/error-handling-in-javascript-a6172ccdf9af

Closes nextauthjs#1602
Achieved by adding a `client: true` flag when logs are coming from the frontend.

BREAKING CHANGE:

The main change is that instead of an unknown number of parameters, the log events have at most two, where the second parameter is usually an object. In the case of the `error` event, it can also be an `Error` instance (that is serializable by `JSON.stringify`). If it is an object, an `Error` instance will be available on `metadata.error`, and `message` will default to `metadata.error.message`. This is done so that an error event always provides some kind of a stack to see where the error happened

```diff
// [...nextauth.js]
import log from "some-logger-service"
...
logger: {
- error(code, ...message) {},
+ error(code, metadata) {},
- warn(code, ...message) {},
+ warn(code) {}
- debug(code, ...message) {}
+ debug(code, metadata) {}
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client Client related code core Refers to `@auth/core` hacktoberfest-docs Relates to documentation TypeScript Issues relating to TypeScript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants