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] Automatically provide context based on INQUIRER #533

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ export class MyService {
private readonly logger: PinoLogger
) {}

constructor(
// If InjectPinoLogger is used without argument, it will attempt to deduce context
// using INQUIRER injection https://github.com/nestjs/docs.nestjs.com/issues/937
@InjectPinoLogger()
private readonly logger: PinoLogger
) {}

foo() {
// PinoLogger has same methods as pino instance
this.logger.trace({ foo: 'bar' }, 'baz %s', 'qux');
Expand Down
2 changes: 1 addition & 1 deletion __tests__/no-context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('no context', () => {

const ctrlLog = logs.find((v) => v.msg === msg);
expect(ctrlLog).toBeTruthy();
expect(ctrlLog).not.toHaveProperty('context');
expect(ctrlLog?.context).toEqual(TestController.name);
Copy link
Owner

Choose a reason for hiding this comment

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

does this work only when using InjectPinoLogger? will it work for constructor(private logger: PinoLogger) {}? if yes let's add the test for this

Copy link
Author

Choose a reason for hiding this comment

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

It won't work. Though, adding dependency on Inquirer to PinoLogger is possible.

Copy link
Owner

Choose a reason for hiding this comment

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

hm, let me think. In such case, we can get rid of this decorator, I believe in 95% it's called with the name of the class, so if we can add Inquirer to PinoLogger almost everyone can just drop decorator, and the rest who need to redefine context to different name will use .setContext.

So, in the current major we can add Inquirer to PinoLogger, after that deprecate the usage of @InjectPinoLogger. And drop it in the next major. This should simplify the API of this lib. So let's move this way.

So, what we need right now:

  • add inquirer to PinoLogger, add/change test to 1) check that context is set automatically and 2) can be redefined in constructor.
  • add warn log about deprecation of usage of decorator in createDecoratedLoggerProvider function
  • add docs on this feature and a note that decorator is deprecated and context for PinoLogger is set automatically
  • change usage of decorator from example/, and add the comment which value will be for context field by default

@Ginden wdyt?

Copy link
Author

Choose a reason for hiding this comment

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

That sounds like a great idea. <3

I will try to implement it when I have time.

Copy link
Author

@Ginden Ginden Oct 12, 2021

Choose a reason for hiding this comment

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

@iamolegga I think this approach is infeasible, because it breaks other things.

PinoLogger instances request by Loggers (either from @nestjs/common or src/Logger.ts) ends with context being Logger. There is no workaround for that, because it would require changing @nestjs/common.

Copy link
Owner

Choose a reason for hiding this comment

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

@Ginden let me check this in the next couple of weeks, maybe I could find any workaround, if not let's merge this as is

Copy link

@exarus exarus Aug 25, 2022

Choose a reason for hiding this comment

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

@iamolegga maybe we can provide different logger classes for user-injected PinoLogger and for @nestjs/common (e.g. name it PinoLoggerAdapter)?
@Ginden what do you think?

Copy link
Owner

Choose a reason for hiding this comment

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

I'm not sure that I would like to extends codebase with one more class, this became a complex project already for just a logger.

If there is no easy way to implement this feature, maybe complex solution doesn't worth skipping one simple line of code this.logger.setContext(MyService.name)

Copy link

Choose a reason for hiding this comment

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

@iamolegga i think a good, easy to use and performant logger is a bit more complex than just a logger (see pino repo :) ) I'm willing to try to create dumb class class PinoLoggerInjected extends PinoLogger {} and try to inject that one to @nestjs/common.

});
});
}
Expand Down
1 change: 1 addition & 0 deletions __tests__/utils/test-case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export class TestCase {
__resetSingletons();

const app = await NestFactory.create(this.module, this.adapter, {
abortOnError: false,
iamolegga marked this conversation as resolved.
Show resolved Hide resolved
bufferLogs: true,
});
app.useLogger(app.get(Logger));
Expand Down
22 changes: 21 additions & 1 deletion src/InjectPinoLogger.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
import { Inject, Provider } from '@nestjs/common';
import { Inject, Provider, Scope } from '@nestjs/common';
import { INQUIRER } from '@nestjs/core';
import { PinoLogger } from './PinoLogger';

const decoratedTokenPrefix = 'PinoLogger:';

const decoratedLoggers = new Set<string>();
const transientToken = getLoggerToken('');

export function InjectPinoLogger(context = '') {
decoratedLoggers.add(context);
return Inject(getLoggerToken(context));
}

function createDecoratedLoggerProvider(context: string): Provider<PinoLogger> {
if (context === '') {
return {
provide: transientToken,
useFactory: (logger: PinoLogger, inquirer: any) => {
if (!inquirer) {
return logger;
}
if (typeof inquirer === 'string') {
logger.setContext(inquirer);
} else if (typeof inquirer === 'object') {
logger.setContext(inquirer.constructor.name);
}
return logger;
},
inject: [PinoLogger, INQUIRER],
scope: Scope.TRANSIENT,
};
}
return {
provide: getLoggerToken(context),
useFactory: (logger: PinoLogger) => {
Expand Down