Skip to content

Commit b0e9b90

Browse files
Merge pull request #2604 from BrunnerLivio/feature/terminus-custom-logger
docs(terminus): add section about logging with terminus
2 parents 4154190 + 3f62e55 commit b0e9b90

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

content/recipes/terminus.md

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ the following object with a 200 status code.
158158
The interface of this response object can be accessed from the `@nestjs/terminus` package with the `HealthCheckResult` interface.
159159

160160
| | | |
161-
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------ |
161+
|-----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------|
162162
| `status` | If any health indicator failed the status will be `'error'`. If the NestJS app is shutting down but still accepting HTTP requests, the health check will have the `'shutting_down'` status. | `'error' \| 'ok' \| 'shutting_down'` |
163163
| `info` | Object containing information of each health indicator which is of status `'up'`, or in other words "healthy". | `object` |
164164
| `error` | Object containing information of each health indicator which is of status `'down'`, or in other words "unhealthy". | `object` |
@@ -525,6 +525,88 @@ export class HealthController {
525525
}
526526
```
527527

528+
#### Logging
529+
530+
Terminus only logs error messages, for instance when a Healthcheck has failed. With the `TerminusModule.forRoot()` method you have more control over how errors are being logged
531+
as well as completely take over the logging itself.
532+
533+
In this section, we are going to walk you through how you create a custom logger `TerminusLogger`. This logger extends the built-in logger.
534+
Therefore you can pick and choose which part of the logger you would like to overwrite
535+
536+
> info **Info** If you want to learn more about custom loggers in NestJS, [read more here](/techniques/logger#injecting-a-custom-logger).
537+
538+
539+
```typescript
540+
@@filename(terminus-logger.service)
541+
import { Injectable, Scope, ConsoleLogger } from '@nestjs/common';
542+
543+
@Injectable({ scope: Scope.TRANSIENT })
544+
export class TerminusLogger extends ConsoleLogger {
545+
error(message: any, stack?: string, context?: string): void;
546+
error(message: any, ...optionalParams: any[]): void;
547+
error(
548+
message: unknown,
549+
stack?: unknown,
550+
context?: unknown,
551+
...rest: unknown[]
552+
): void {
553+
// Overwrite here how error messages should be logged
554+
}
555+
}
556+
```
557+
558+
Once you have created your custom logger, all you need to do is simply pass it into the `TerminusModule.forRoot()` as such.
559+
560+
```typescript
561+
@@filename(health.module)
562+
@Module({
563+
imports: [
564+
TerminusModule.forRoot({
565+
logger: TerminusLogger,
566+
}),
567+
],
568+
})
569+
export class HealthModule {}
570+
```
571+
572+
573+
To completely suppress any log messages coming from Terminus, including error messages, configure Terminus as such.
574+
575+
```typescript
576+
@@filename(health.module)
577+
@Module({
578+
imports: [
579+
TerminusModule.forRoot({
580+
logger: false,
581+
}),
582+
],
583+
})
584+
export class HealthModule {}
585+
```
586+
587+
588+
589+
Terminus allows you to configure how Healthcheck errors should be displayed in your logs.
590+
591+
| Error Log Style | Description | Example |
592+
|:------------------|:-----------------------------------------------------------------------------------------------------------------------------------|:---------------------------------------------------------------------|
593+
| `json` (default) | Prints a summary of the health check result in case of an error as JSON object | <figure><img src="/assets/Terminus_Error_Log_Json.png" /></figure> |
594+
| `pretty` | Prints a summary of the health check result in case of an error within formatted boxes and highlights successful/erroneous results | <figure><img src="/assets/Terminus_Error_Log_Pretty.png" /></figure> |
595+
596+
You can change the log style using the `errorLogStyle` configuration option as in the following snippet.
597+
598+
```typescript
599+
@@filename(health.module)
600+
@Module({
601+
imports: [
602+
TerminusModule.forRoot({
603+
errorLogStyle: 'pretty',
604+
}),
605+
]
606+
})
607+
export class HealthModule {}
608+
```
609+
528610
#### More examples
529611

530612
More working examples are available [here](https://github.com/nestjs/terminus/tree/master/sample).
52 KB
Loading
62.6 KB
Loading

0 commit comments

Comments
 (0)