-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
[Proposal] Refactor Logger implementation for better extensibility #5353
Comments
The last argument is nonessential for custom loggers.
Whether to show timestamp or not depends on where we call the
What would be the benefit of splitting this class into 2 separate classes? |
The problem here that it is still passed, so custom logger needs to be aware of it, otherwise it could lead to bugs. I experienced that issue with As I understand TypeScript interface semantics, having inteface with method signature with two arguments means that user of such interface should not pass any more than two arguments to it. Here is my small test showing that: Hence, the problem here, that while there exists an abstraction called
There should at least be some type check before doing such type cast.
Thanks for pointing that out. Yes, my proposal of moving it As I see from the sources, nest/packages/core/nest-factory.ts Line 29 in f0e7597
Therefore it is bound to the lifetime of transitent Logger instance. Other way to use Overall, existance of such property of the
Currently So splitting current |
Let's track this here #6221 |
Feature Request
Is your feature request related to a problem? Please describe.
In Nest you can create a custom logger by implementing the
LoggerService
interface from here https://github.com/nestjs/nest/blob/master/packages/common/services/logger.service.ts#L11 and passing instance of a custom logger toapp.useLogger
function (as described in the docs).app.useLogger
setsLogger.instance
property to the provided logger instance: https://github.com/nestjs/nest/blob/master/packages/common/services/logger.service.ts#L120.If you use the Nest logger in your class like so:
The line
this.logger.log
eventually calls methodlog
onMyLogger
and passes three arguments:message
,context
andisTimestampEnabled
. First two are described in theLoggerService
interface whileisTimestampEnabled
is not.I expect
LoggerService
interface to be on par with the actual usage of the logger.Also, the way
Logger
class itself is organized is quite strange and in my opinion has some extension limitations.For example, having
isTimestampEnabled
property passed to each log message is strange for me.Here is the link to Codesandbox with an example: https://codesandbox.io/s/nestjs-custom-logger-fwrjl?file=/src/main.ts.
Note that there are some problems with displaying logs in the console of codesandbox (sometimes I see them and other times don't).
Describe the solution you'd like
I think the only good way out of this issue is to refactor the way
Logger
is implemented which will probably be a breaking change.Here is the solution I can propose:
We could have current
Logger
class as an abstract implementation-agnostic logger which simply passescontext
to each instance call. Actual logging methods which are currently implemented as static methods of theLogger
will be extracted into separate class, e.g.DefaultLogger
, instance of which is set asLogger.instance
by default. The logger parameters such asisTimestampEnabled
will then be supplied to theDefaultLogger
instead of theLogger
since they may not be valid for every logger implementation. This way we can even leave currentLoggerService
interface untouched.Code examples for the proposed solution (shortened for brevity):
Teachability, Documentation, Adoption, Migration Strategy
Most of the public API won't change, but we will have to emphasize the correct way of using and customizing the logger in the docs. I have started doing this in this PR to the docs: nestjs/docs.nestjs.com#1441. But if we to refactor the Logger as I described, some more docs changes would be required such as explaining how to configure the default logger and explaining how default
Logger
is just an abstract wrapper.For some custom loggers this will probably be a breaking change, but since I have only tried
nestjs-pino
, I cannot predict consequences on a larger scale, so additional input from community is required. But in the end it should make the Nest API clearer and more extensible.What is the motivation / use case for changing the behavior?
I mostly explained my motivation in the PR to the docs: nestjs/docs.nestjs.com#1441. Basically I found a good library called nestjs-pino which provides custom logger using pino. But the way it was supposed to be used was feeling strange to me. I wanted nest to somehow abstract the logger implementation. And through the source code digging I have found that it actually kinda does, but is not described anywhere. So I want this feature to be more fleshed out.
The text was updated successfully, but these errors were encountered: