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).
Log objects as structured fields or JSON, never interpolated into the message
Don't string-interpolate objects into log messages (they become '[object Object]'); pass them as a structured argument or JSON.stringify the payload.
Bad example
| 1 | // Renders as: "Failed to get all files: [object Object]" |
| 2 | logger.error(`Failed to get all files: ${error.response?.data}`); |
Explanation (EN)
Template-literal interpolation calls toString() on objects, producing '[object Object]' in the log aggregator (Kibana/CloudWatch) and discarding the actual error payload you need to debug.
Objašnjenje (HR)
Interpolacija u predlosku string-a poziva toString() nad objektima, sto u agregatoru logova (Kibana/CloudWatch) daje '[object Object]' i odbacuje stvarni sadrzaj greske koji vam treba za debugiranje.
Good example
| 1 | // Pass structured data as a separate arg... |
| 2 | logger.error('Failed to save to index:', { status: error.response?.status }); |
| 3 |
|
| 4 | // ...or serialize explicitly when the transport can't handle objects |
| 5 | logger.error(`Failed to get all files: ${JSON.stringify(error.response?.data, null, 2)}`); |
Explanation (EN)
Passing the object as a separate argument lets a structured logger index its fields, and JSON.stringify guarantees a readable payload when the transport only handles strings. Confirm how your logger serializes the extra argument before relying on it.
Objašnjenje (HR)
Prosljedivanje objekta kao zasebnog argumenta omogucuje strukturiranom loggeru da indeksira njegova polja, a JSON.stringify jamci citljiv sadrzaj kad transport podrzava samo string-ove. Provjerite kako vas logger serijalizira dodatni argument prije nego se na to oslonite.
Notes (EN)
Verify your logger's behaviour for the extra-argument form; some transports still flatten it to '[object Object]', in which case JSON.stringify is the safe choice.
Bilješke (HR)
Provjerite ponasanje svog loggera za oblik s dodatnim argumentom; neki transporti ga i dalje pretvore u '[object Object]', u kojem slucaju je JSON.stringify siguran izbor.