Skip to content

Commit 3c4968c

Browse files
committed
docs(terminus): add section about logging with terminus
1 parent 51735dc commit 3c4968c

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

content/recipes/terminus.md

Lines changed: 87 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,92 @@ 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+
##### Custom Logger
534+
535+
In this section, we are going to walk you through how you create a custom logger `TerminusLogger`. This logger extends the built-in logger.
536+
Therefore you can pick and choose which part of the logger you would like to overwrite
537+
538+
> info **Info** If you want to learn more about custom loggers in NestJS, [read more here](/techniques/logger#injecting-a-custom-logger).
539+
540+
541+
```typescript
542+
@@filename(terminus-logger.service)
543+
import { Injectable, Scope, ConsoleLogger } from '@nestjs/common';
544+
545+
@Injectable({ scope: Scope.TRANSIENT })
546+
export class TerminusLogger extends ConsoleLogger {
547+
error(message: any, stack?: string, context?: string): void;
548+
error(message: any, ...optionalParams: any[]): void;
549+
error(
550+
message: unknown,
551+
stack?: unknown,
552+
context?: unknown,
553+
...rest: unknown[]
554+
): void {
555+
// Overwrite here how error messages should be logged
556+
}
557+
}
558+
```
559+
560+
Once you have created your custom logger, all you need to do is simply pass it into the `TerminusModule.forRoot()` as such.
561+
562+
```typescript
563+
@@filename(health.module)
564+
@Module({
565+
imports: [
566+
TerminusModule.forRoot({
567+
logger: TerminusLogger,
568+
}),
569+
],
570+
})
571+
export class HealthModule {}
572+
```
573+
574+
##### Disable the Terminus Logger completely
575+
576+
To completely suppress any log messages coming from Terminus, including error messages, configure Terminus as such.
577+
578+
```typescript
579+
@@filename(health.module)
580+
@Module({
581+
imports: [
582+
TerminusModule.forRoot({
583+
logger: false,
584+
}),
585+
],
586+
})
587+
export class HealthModule {}
588+
```
589+
590+
591+
##### Change the error log style
592+
593+
Terminus allows you to configure how Healthcheck errors should be displayed in your logs.
594+
595+
| Error Log Style | Description | Example |
596+
|:------------------|:-----------------------------------------------------------------------------------------------------------------------------------|:---------------------------------------------------------------------|
597+
| `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> |
598+
| `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> |
599+
600+
You can change the log style using the `errorLogStyle` configuration option as in the following snippet.
601+
602+
```typescript
603+
@@filename(health.module)
604+
@Module({
605+
imports: [
606+
TerminusModule.forRoot({
607+
errorLogStyle: 'pretty',
608+
}),
609+
]
610+
})
611+
export class HealthModule {}
612+
```
613+
528614
#### More examples
529615

530616
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)