Custom Logger service not working as expected
See original GitHub issueI’m submitting a…
[ ] Regression
[X] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
- It doesn’t print on console the message you send to the custom log function.
- The logger is always calling the log function inside the custom logger class. Also it doesn’t print the message.
Expected behavior
Use the custom logger.
Minimal reproduction of the problem with instructions
Implement the following custom logger inside a project (I am using the cqrs project as an example):
logger.module.ts
import * as winston from 'winston'
import {Component, Logger, LoggerService} from '@nestjs/common';
import {DateTime} from 'luxon'
@Component()
export class MyLogger extends winston.Logger implements LoggerService {
private winston
constructor() {
super()
this.winston = new winston.Logger({
transports: [
new winston.transports.Console({
json: true,
colorize: true,
label: 'GameService',
timestamp: () => DateTime.local().toString(),
formatter: options => `${options.timestamp()} [${options.level.toUpperCase()}] ${options.label} - ${options.message}`,
}),
],
})
}
error(message: string, trace: string) {
this.winston.log({
level: 'error',
message
})
}
log(message: string) {
this.winston.log({
level: 'warn',
message,
})
}
warn(message: string) {
this.winston.log({level: 'warn', message})
}
info(message: string) {
this.winston.log({
level: 'info',
message
})
}
}
Then, in the heroes.module.ts import it:
import { CommandBus, EventBus, CQRSModule } from '@nestjs/cqrs'
import { ModuleRef } from '@nestjs/core'
import { HeroesGameController } from './heroes.controller';
import {Module, OnModuleInit} from '@nestjs/common';
import {HeroesGameService} from './heroes.service';
import {HeroesGameSagas} from './sagas/heroes.sagas';
import {CommandHandlers} from './commands/handlers';
import {EventHandlers} from './events/handlers';
import {HeroRepository} from './repository/hero.repository';
import {MyLogger} from './middlewares/logger/logger.module';
@Module({
modules: [CQRSModule],
controllers: [HeroesGameController],
components: [
HeroesGameService,
HeroesGameSagas,
...CommandHandlers,
...EventHandlers,
HeroRepository,
MyLogger,
],
exports: [MyLogger],
})
export class HeroesGameModule implements OnModuleInit {
constructor(
private readonly moduleRef: ModuleRef,
private readonly command$: CommandBus,
private readonly event$: EventBus,
private readonly heroesGameSagas: HeroesGameSagas,
private logger: MyLogger,
) {}
onModuleInit() {
this.command$.setModuleRef(this.moduleRef)
this.event$.setModuleRef(this.moduleRef)
this.event$.register(EventHandlers)
this.command$.register(CommandHandlers)
this.event$.combineSagas([this.heroesGameSagas.dragonKilled])
}
}
I have tried adding it in the bootstrap function in main.ts
import { NestFactory } from '@nestjs/core';
import { ApplicationModule } from './app.module';
import {MyLogger} from './cqrs/modules/heroes/middlewares/logger/logger.module';
async function bootstrap() {
const app = await NestFactory.create(ApplicationModule, {
logger: new MyLogger(),
})
await app.listen(3000);
}
bootstrap();
What is the motivation / use case for changing the behavior?
We need to use our custom injectable logger.
Environment
Nest version: 4.6.5
For Tooling issues:
- Node version: v8.10.0
- Platform: Linux xUbuntu 64bit 17.10
Another things
Related from: https://github.com/nestjs/nest/issues/247, https://github.com/nestjs/nest/issues/435
I think I am doing something wrong, so if you don’t mind writing a little example please.
Thank you so much.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:12 (5 by maintainers)
Top Results From Across the Web
java.util.logging custom formatter not working as expected
I am using Java 6. I have created a custom formatter which creates only the time and the message, but it always prints...
Read more >Logger | NestJS - A progressive Node.js framework
You can provide a custom logger implementation to be used by Nest for system logging by setting the value of the logger property...
Read more >Systemd/rsyslog logging not working as expected
It seems that part of the issue is described in bug #1861881. I was able to get a working log file while excluding...
Read more >Logging in K2 - Nintex help documentation
Check this log file to see if the expected data was returned to the SmartObject, in which case the problem is likely on...
Read more >Troubleshoot pushing log data to CloudWatch - Amazon AWS
Even if you aren't using IMDSv2, it's a best practice to use the newer unified CloudWatch agent instead of the logs agent. Fingerprinting...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hmm, in my Daily projects I used to use my own logger, which i’d love to share it it’s fairly simple, and easy to use.
I used to have a utils folder in my shared module, it helps me alot in my projects.
in your bootstrap function
and finally you can use it everywhere.
Here is how it looks like 😃
Anyway, it’s easy to implement your own. Hope this helped.
Does anyone have an example of doing the above but with a decorator for dependency injection into the constructor?