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

No logger is set up #908

Open
maxxims opened this issue Mar 18, 2023 · 6 comments
Open

No logger is set up #908

maxxims opened this issue Mar 18, 2023 · 6 comments

Comments

@maxxims
Copy link

maxxims commented Mar 18, 2023

An important part of any new project is having a good logging system set up from the very first beginning.
Taking into consideration the current best practices, could you please add a nice logging system to the boilerplate?

@Shchepotin
Copy link
Collaborator

@maxxims I'll consider your proposal

@brocoders brocoders deleted a comment from igolubic Aug 17, 2023
@Shchepotin
Copy link
Collaborator

@igolubic for sharing your solution please use "Awesome" repos. Thanks for understanding.

@pauljonescodes
Copy link

For what it's worth, my solution to this right now is to have this at the top of all my classes where I'd like a logger:

import { Logger } from '@nestjs/common';

@Injectable()
export class FilesService {
  private readonly logger = new Logger(FilesService.name);
  
  // ...
}

Having said that, I could see there being some sense in making this injectable module/service too, so there's a single place they could processed and filtered.

@pauljonescodes
Copy link

pauljonescodes commented Sep 19, 2023

I had another chance to think about this and came up with the following:

import { Module } from '@nestjs/common';
import { AppLogger } from './app.logger.js';

/* logger module */

@Module({
  providers: [AppLogger],
  exports: [AppLogger],
})
export class LoggerModule {}

/* app logger service */

@Injectable({ scope: Scope.TRANSIENT })
export class AppLogger extends ConsoleLogger {}

/* example usage module */

@Module({
  imports: [
    LoggerModule,
    // etc
  ],
  controllers: [],
  providers: [FilesService],
  exports: [FilesService],
})
export class FilesModule {}

/* example usage service */

@Injectable()
export class FilesService {

  constructor(
    protected readonly logger: AppLogger
  ) {
    logger.setContext(FilesService.name);
  }

  async upload(file: Express.Multer.File): Promise<FileEntity> {
    this.logger.verbose(this.upload.name);
    //etc
  }
}

@pauljonescodes
Copy link

I've had some more time to think about this, I recommend the module solution for those that have centralized logging needs, like storing to file or uploading to a particular service or varying by environment, however I think Nest's default is the best go-to approach as it does not involve another module:

export class FilesService {
  private readonly logger = new Logger(FilesService.name);
  private readonly s3: S3;

  constructor(
    @InjectRepository(FileEntity)
    private readonly fileRepository: Repository<FileEntity>,
    @Inject(FilesConfig.KEY)
    private config: ConfigType<typeof FilesConfig>,
  ) {
    this.logger.verbose(this.constructor.name);
    // ...
  }

  async upload(file: Express.Multer.File): Promise<FileEntity> {
    this.logger.verbose(this.upload.name);
    // ...
   }
}

If you ever want to re-use your code in another Nest project, having an AppLogger dependency would be a drag, unless it is actually part of the module's requirements.

@tukusejssirs
Copy link

Consider using Ogma Logger.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants