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 prettyPrint option to logger type definitions #2550
Add prettyPrint option to logger type definitions #2550
Conversation
Could you also add type test for this? See https://github.com/fastify/fastify/tree/master/test/types |
Yes. Will do. |
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.
LGTM
types/logger.d.ts
Outdated
@@ -1,6 +1,7 @@ | |||
import { FastifyError } from 'fastify-error' | |||
import { RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression } from './utils' | |||
import { FastifyRequest, RequestGenericInterface } from './request' | |||
import { PrettyOptions } from 'pino' |
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'm a bit worried about TypeScript users. Will it work when user does not have @types/pino
package installed?
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.
Indeed!
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.
pino
does not provides own types and they shipped via @types/pino
package hence If I won't install @types/pino
TypeScript will consider typings as invalid
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.
So would you prefer to just copy the type definition over? That way would mean it would have to be updated here on changes from pino
/ @types/pino
.
Alternatively a note could be added to the docs. In order to use pino-pretty
with fastify
you need to install the pino-pretty
dependency regardless so in the docs a note can be added to also install @types/pino
if you are using TypeScript.
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 would include typings in pino
itself.
BTW moving @types/pino
to dependencies
or adding notes to README also acceptable, but as a WebStorm IDE user I would prefer first (moving @types/pino
) for better smart completion.
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 don't like including types that are not under our control (from
@types/pino
) to the fastify dependency tree.
@mcollina what do you think? What about a PR to pino itself to add typings? @jsumners any thoughts?
As you've learned, I 1. have these TS issues muted and 2. I have opinions. Opinions you will not appreciate.
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.
@jsumners I appreciate very much your opinions actually. Even more, if your opinions are different from mine. That's why I tagged you here and why I ask your point of view whenever I can.
However, if you don't want to be tagged on TS issues, I am not going to tag you in the future.
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.
That would be fine. I cannot provide any useful input for TS related things. If you want the full story, read through #649
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've just finished reading it. That issue was a really long ride.
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.
Indeed.
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.
It should not depend on pino types, as stated multiple times in a lot of previous PRS on this topic. Maybe we can add a comment in the file?
types/logger.d.ts
Outdated
@@ -1,6 +1,7 @@ | |||
import { FastifyError } from 'fastify-error' | |||
import { RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression } from './utils' | |||
import { FastifyRequest, RequestGenericInterface } from './request' | |||
import { PrettyOptions } from 'pino' |
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.
Pino has no official types. No one of the maintainers of pino has enough typescript skills to have types. The types on @types
are unofficial and should not be used.
This is easy to fix by lifting whatever parts of the pino typedefs is needed in fastify itself. it's easy and swift. |
I'm not interested in maintaining more types than Fastify's so I'd rather we keep them here. Copy-pasting pino types and shipping them ourselves from within Fastify is okay as the pino api is not too complex. What I've tried to articulate in the past is that there exists a minimal logger api that most fastify users use. When a dev needs more than that minimal interface; we should recommend them to install the pino types and use them in their fastify project themselves. I can't remember if they need to use declaration merging or use a generic to achieve that. This is the best path that doesn't add any additional maintenance responsibility to the projects in question |
If we are fine in "duplicating" this over here, we should maintain Pino's typings in Fastify's codebase. |
We the types we write diverge in any non-mergiable way from If we are all fine with this, then I vote too to add them to Fastify. :) |
this is why I think we should only ship a minimal viable type api for the logger. if the user wants full pino types they should be instructed to |
Are you suggesting adding a new generics to Fastify typings? 🤔 If so, we might have conflicts anyway if both types are decalred using the same |
https://github.com/fastify/fastify/blob/master/types/instance.d.ts#L20 Generic already exists for users to pass their own logger instance (i.e. Pino). There is even a test case: https://github.com/fastify/fastify/blob/master/test/types/logger.test-d.ts#L46 One aspect I would like to see changed in v4 is that we move the logger generic to the front of the list so that users don't have to specify the server, request, and reply ones as well. (We could try to use a generic object like the requestgenericinterface but iirc that breaks some of the magical inference so it might not be an actual solution here). |
Now I am even more confused than before. I know you can already pass a generic to "inject" pino's typings, but this PR is trying to fix a different issue. Am I wrong? I am referring to this: https://github.com/fastify/fastify/blob/master/test/types/logger.test-d.ts#L4 P.S. Using object for generic would brake inference for sure as of today :( |
The inject method for pino typings is the solution to this issue. By recommending the user to inject pino typings they should get access to the prettyPrint property |
OK let me recap this for clarity, just to see if I understood what you are saying. If not, why some options are "integrated" and others must be provided by passing explicitly Pino typings? Sorry for asking this again, but I have a really hard time understanding this. :) |
No worries thank you for working through this with me!
Yes
No
Because of the idea of minimal viable api. We should provide a valid, minimal version of the logger types that is easy for us to maintain and will enable a basic TypeScript fastify user to have logging support in their app. If the user needs more than the provided basic api they need to install and inject the Pino types themselves. My explanation here is already implemented as far as I know. It may need some small changes as well as better documentation. If others disagree with the way it is now then that is okay and we can continue having this discussion |
I think this is the most important part of this discussion. I don't understand why this PR is needed at all. Can we ease this scenario? |
@mcollina this is the first time that an option is more complex than a single value: prettyPrint could be an object. To avoid duplicating its types in fastify, this PR tried to import pino's typings and to use it here. It is the same as we do, for example, in the One possible way of simplifying it is to duplicate in this repo also the prettyPrint object types. |
Let's do this. |
@agramian You can copy the object interface from here: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/72c9bd83316bd31e93ab86d64ddf598d922f33cd/types/pino/index.d.ts#L514 |
Ok I lifted the interface and placed it just above the |
Please add a non-jsdoc comment above the interface that says where it was copied from. Additionally, I think now is the best time for us to add a comment at the top of the logger file that summarizes the current decision on why we don't include @types/pino in the types. That tweet is now 7 years old and it is more true than ever. Thank you for sharing 😂❤️ |
… from @types/pino
I added the comment referencing the copy source as well as the summary of the decision as best I could understand it from your guys' discussion. |
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 love this. Great work on the comment at the top of the file ❤️
Thank you all for collaborating and discussing this issue!
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.
lgtm
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.
LGTM!
@fox1t @kibertoad @Eomm are there any outstanding concerns before we land this? |
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.
LGTM
This is perfectly fine. If the pino public interface will ever change we will update these types. 👌 |
refs #2395 |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
The
FastifyLoggerOptions
interface is missing a type definition for theprettyPrint
option whichpino
supports whenpino-pretty
is installed. The latest general Logging docs already reference theprettyPrint
option however without the type definition an error is shown when using TypeScript and passing the option to the logger when creating the Fastify instance.Checklist
npm run test
andnpm run benchmark
and the Code of conduct