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

Setting the log levels using ConfigService #7108

Closed
fmagno opened this issue May 12, 2021 · 2 comments
Closed

Setting the log levels using ConfigService #7108

fmagno opened this issue May 12, 2021 · 2 comments
Labels
needs triage This issue has not been looked into type: question 🙌

Comments

@fmagno
Copy link

fmagno commented May 12, 2021

How can I set the allowed log levels by using the ConfigService?

I tried to do it through main.ts but the logLevels are set during the app creation call which is also required to get the ConfigService.

const app = await NestFactory.create(
    AppModule.register(),
    { logger: configService.get('LOG_LEVELS') }    // <-- at this point I still can't do: const configService = app.get(ConfigService);
);

Is is possible to create the app object with the logLevels set to default and then override it? Something like this:

const app = await NestFactory.create(
    AppModule.register(),
    { logger: true }
);

const configService = app.get('ConfigService');
app.setLogLevels(configService.get('LOG_LEVELS'));

Also tried a different avenue but the solution is not ideal. I tried first to set the logLevels in the constructor of MyLogger but the class member logLevels is private to Logger so I can't override it. The same seems to be the case for the method isLogLevelEnabled(level).

The only option I found so far is to check the logLevels allowed inside the actual logging methods: log, warn, error, etc...

@Injectable()
export class MyLogger extends Logger {
    constructor(private configService: ConfigService) {
        super();
        MyLogger.logLevels = configService.get('LOG_LEVELS')   // <-- can't set because it's private to Logger
    }

    isLogLevelEnabled(level) {}   // <-- can't override because it's private to Logger


    log(message: any, context? string): void {
        const logLevels = this.configService.get('LOG_LEVELS');
        // check if this method has the right level to log the message depending on logLevels before proceeding
        super.log(message, context);
    }
}

Is there a better way to do this?

@fmagno fmagno added needs triage This issue has not been looked into type: question 🙌 labels May 12, 2021
@kamilmysliwiec
Copy link
Member

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.

@fmagno
Copy link
Author

fmagno commented May 12, 2021

Okay, cheers!

Just so it doesn't get lost in discord messages, this question relates to these issues: #6349 and #2343.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into type: question 🙌
Projects
None yet
Development

No branches or pull requests

2 participants