|
| 1 | +--- |
| 2 | +type: docs |
| 3 | +title: "Logging in JavaScript SDK" |
| 4 | +linkTitle: "Logging" |
| 5 | +weight: 500 |
| 6 | +description: Configuring logging in JavaScript SDK |
| 7 | +--- |
| 8 | + |
| 9 | +## Introduction |
| 10 | + |
| 11 | +The JavaScript SDK comes with a out-of-box `Console` based logger. The SDK emits various internal logs to help users understand the chain of events and troubleshoot problems. A consumer of this SDK can customize the verbosity of the log, as well as provide their own implementation for the logger. |
| 12 | + |
| 13 | +## Configure log level |
| 14 | + |
| 15 | +There are five levels of logging in **descending order of importance** - `error`, `warn`, `info`, `verbose`, and `debug`. Setting the log to a level means that the logger will emit all the logs that are at least as important as the mentioned level. For example, setting to `verbose` log means that the SDK will not emit `debug` level logs. The default log level is `info`. |
| 16 | + |
| 17 | +### Dapr Client |
| 18 | + |
| 19 | +```js |
| 20 | +import { CommunicationProtocolEnum, DaprClient, LogLevel } from "@dapr/dapr"; |
| 21 | + |
| 22 | +// create a client instance with log level set to verbose. |
| 23 | +const client = new DaprClient( |
| 24 | + daprHost, |
| 25 | + daprPort, |
| 26 | + CommunicationProtocolEnum.HTTP, |
| 27 | + { logger: { level: LogLevel.Verbose } }); |
| 28 | +``` |
| 29 | + |
| 30 | +> For more details on how to use the Client, see [JavaScript Client]({{< ref js-client >}}). |
| 31 | +
|
| 32 | +### DaprServer |
| 33 | + |
| 34 | +```js |
| 35 | +import { CommunicationProtocolEnum, DaprServer, LogLevel } from "@dapr/dapr"; |
| 36 | + |
| 37 | +// create a server instance with log level set to error. |
| 38 | +const server = new DaprServer( |
| 39 | + serverHost, |
| 40 | + serverPort, |
| 41 | + daprHost, |
| 42 | + daprPort, |
| 43 | + CommunicationProtocolEnum.HTTP, |
| 44 | + { logger: { level: LogLevel.Error } }); |
| 45 | +``` |
| 46 | + |
| 47 | +> For more details on how to use the Server, see [JavaScript Server]({{< ref js-server >}}). |
| 48 | +
|
| 49 | +## Custom LoggerService |
| 50 | + |
| 51 | +The JavaScript SDK uses the in-built `Console` for logging. To use a custom logger like Winston or Pino, you can implement the `LoggerService` interface. |
| 52 | + |
| 53 | +### Winston based logging: |
| 54 | + |
| 55 | +Create a new implementation of `LoggerService`. |
| 56 | + |
| 57 | +```ts |
| 58 | +import { LoggerService } from "@dapr/dapr"; |
| 59 | +import * as winston from 'winston'; |
| 60 | + |
| 61 | +export class WinstonLoggerService implements LoggerService { |
| 62 | + private logger; |
| 63 | + |
| 64 | + constructor() { |
| 65 | + this.logger = winston.createLogger({ |
| 66 | + transports: [ |
| 67 | + new winston.transports.Console(), |
| 68 | + new winston.transports.File({ filename: 'combined.log' }) |
| 69 | + ] |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + error(message: any, ...optionalParams: any[]): void { |
| 74 | + this.logger.error(message, ...optionalParams) |
| 75 | + } |
| 76 | + warn(message: any, ...optionalParams: any[]): void { |
| 77 | + this.logger.warn(message, ...optionalParams) |
| 78 | + } |
| 79 | + info(message: any, ...optionalParams: any[]): void { |
| 80 | + this.logger.info(message, ...optionalParams) |
| 81 | + } |
| 82 | + verbose(message: any, ...optionalParams: any[]): void { |
| 83 | + this.logger.verbose(message, ...optionalParams) |
| 84 | + } |
| 85 | + debug(message: any, ...optionalParams: any[]): void { |
| 86 | + this.logger.debug(message, ...optionalParams) |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +Pass the new implementation to the SDK. |
| 92 | + |
| 93 | +```ts |
| 94 | +import { CommunicationProtocolEnum, DaprClient, LogLevel } from "@dapr/dapr"; |
| 95 | +import { WinstonLoggerService } from "./WinstonLoggerService"; |
| 96 | + |
| 97 | +const winstonLoggerService = new WinstonLoggerService(); |
| 98 | + |
| 99 | +// create a client instance with log level set to verbose and logger service as winston. |
| 100 | +const client = new DaprClient( |
| 101 | + daprHost, |
| 102 | + daprPort, |
| 103 | + CommunicationProtocolEnum.HTTP, |
| 104 | + { logger: { level: LogLevel.Verbose, service: winstonLoggerService } }); |
| 105 | +``` |
0 commit comments