Skip to content

Commit bf5955d

Browse files
authored
Add documentation for logger usage and make keepAlive optional (#289)
* WIP add logs Signed-off-by: Shubham Sharma <[email protected]> * Make keepAlive optional Signed-off-by: Shubham Sharma <[email protected]> * Update docs Signed-off-by: Shubham Sharma <[email protected]> * Fix links Signed-off-by: Shubham Sharma <[email protected]> * Rephrase Signed-off-by: Shubham Sharma <[email protected]>
1 parent a5ae563 commit bf5955d

File tree

8 files changed

+131
-16
lines changed

8 files changed

+131
-16
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
```

src/actors/client/ActorClient/ActorClient.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ export default class ActorClient {
2626
readonly actor: IClientActor;
2727
readonly options: DaprClientOptions;
2828

29-
constructor(daprHost: string, daprPort: string, communicationProtocol: CommunicationProtocolEnum, options: DaprClientOptions = {
30-
isKeepAlive: true
31-
}) {
29+
constructor(daprHost: string, daprPort: string, communicationProtocol: CommunicationProtocolEnum, options: DaprClientOptions = {}) {
3230
this.daprHost = daprHost;
3331
this.daprPort = daprPort;
3432
this.communicationProtocol = communicationProtocol;

src/implementation/Client/DaprClient.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ export default class DaprClient {
7878
daprHost?: string
7979
, daprPort?: string
8080
, communicationProtocol: CommunicationProtocolEnum = CommunicationProtocolEnum.HTTP
81-
, options: DaprClientOptions = {
82-
isKeepAlive: true,
83-
},
81+
, options: DaprClientOptions = {},
8482
) {
8583
this.daprHost = daprHost ?? Settings.getDefaultHost();
8684
this.daprPort = daprPort ?? Settings.getDefaultPort(communicationProtocol);

src/implementation/Client/GRPCClient/GRPCClient.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ export default class GRPCClient implements IClient {
3232
constructor(
3333
host = Settings.getDefaultHost()
3434
, port = Settings.getDefaultGrpcPort()
35-
, options: DaprClientOptions = {
36-
isKeepAlive: true
37-
},
35+
, options: DaprClientOptions = {},
3836
) {
3937
this.clientHost = host;
4038
this.clientPort = port;

src/implementation/Client/HTTPClient/HTTPClient.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,19 @@ export default class HTTPClient implements IClient {
3737
constructor(
3838
host = Settings.getDefaultHost()
3939
, port = Settings.getDefaultHttpPort()
40-
, options: DaprClientOptions = {
41-
isKeepAlive: true
42-
},
40+
, options: DaprClientOptions = {},
4341
) {
4442
this.clientHost = host;
4543
this.clientPort = port;
4644
this.options = options;
4745
this.logger = new Logger("HTTPClient", "HTTPClient", this.options.logger);
4846
this.isInitialized = false;
4947

48+
// fallback to default
49+
if (this.options.isKeepAlive === undefined) {
50+
this.options.isKeepAlive = true;
51+
}
52+
5053
if (!this.clientHost.startsWith('http://') && !this.clientHost.startsWith('https://')) {
5154
this.clientUrl = `http://${this.clientHost}:${this.clientPort}/v1.0`;
5255
} else {

src/implementation/Server/DaprServer.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ export default class DaprServer {
5757
, daprHost?: string
5858
, daprPort?: string
5959
, communicationProtocol: CommunicationProtocolEnum = CommunicationProtocolEnum.HTTP
60-
, clientOptions: DaprClientOptions = {
61-
isKeepAlive: true
62-
}
60+
, clientOptions: DaprClientOptions = {}
6361
) {
6462
this.serverHost = serverHost ?? Settings.getDefaultHost();
6563
this.serverPort = serverPort ?? Settings.getDefaultAppPort(communicationProtocol);

src/types/DaprClientOptions.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ limitations under the License.
1414
import { LoggerOptions } from "./logger/LoggerOptions";
1515

1616
export type DaprClientOptions = {
17-
isKeepAlive: boolean;
17+
/**
18+
* If set to false, the HTTP client will not reuse the same connection for multiple requests.
19+
* Default is true.
20+
*/
21+
isKeepAlive?: boolean;
22+
/**
23+
* Options related to logging.
24+
*/
1825
logger?: LoggerOptions;
1926
}

src/types/logger/LoggerOptions.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ import { LogLevel } from "./LogLevel"
1818
* LoggerOptions provides options for configuring the logger.
1919
*/
2020
export type LoggerOptions = {
21+
/**
22+
* The minimum level of messages to log.
23+
* Default is LogLevel.Info.
24+
*/
2125
level?: LogLevel
26+
/**
27+
* Logging implementation to use.
28+
* Default is to use the console.
29+
*/
2230
service?: LoggerService
2331
}

0 commit comments

Comments
 (0)