Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Use a class-scoped named logger instead of static logger calls
Create a per-class logger instance with the class name as context so every log line is prefixed with its source.
Bad example
| 1 | import { Injectable, Logger } from '@nestjs/common'; |
| 2 |
|
| 3 | @Injectable() |
| 4 | export class OrderService { |
| 5 | process() { |
| 6 | Logger.debug(`Processing order with URL: ${url}`); |
| 7 | } |
| 8 | } |
Explanation (EN)
The static call emits a log line with no source context, so you can't tell which class produced it when scanning logs.
Objašnjenje (HR)
Staticki poziv ispisuje log redak bez konteksta izvora, pa ne mozes znati koja ga je klasa proizvela kad pregledavas logove.
Good example
| 1 | import { Injectable, Logger } from '@nestjs/common'; |
| 2 |
|
| 3 | @Injectable() |
| 4 | export class OrderService { |
| 5 | private readonly logger = new Logger(OrderService.name); |
| 6 |
|
| 7 | process() { |
| 8 | this.logger.debug(`Processing order with URL: ${url}`); |
| 9 | } |
| 10 | } |
Explanation (EN)
Every line from this class is prefixed with [OrderService], making logs traceable to their origin.
Objašnjenje (HR)
Svaki redak iz ove klase ima prefiks [OrderService], cime su logovi sljedivi do svog izvora.
Notes (EN)
Using ClassName.name (rather than a hardcoded string) keeps the context correct after renames.
Bilješke (HR)
Koristenje ClassName.name (umjesto tvrdo kodiranog stringa) cuva ispravan kontekst i nakon preimenovanja.