Skip to content

Commit b34af28

Browse files
committed
Update logging references in DaprClient
Signed-off-by: Shubham Sharma <[email protected]>
1 parent 850bcd0 commit b34af28

File tree

7 files changed

+49
-17
lines changed

7 files changed

+49
-17
lines changed

src/implementation/Client/DaprClient.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ import HTTPClient from './HTTPClient/HTTPClient';
5050
import CommunicationProtocolEnum from '../../enum/CommunicationProtocol.enum';
5151
import { DaprClientOptions } from '../../types/DaprClientOptions';
5252
import { Settings } from '../../utils/Settings.util';
53+
import { Logger } from '../../logger/Logger';
5354

5455
export default class DaprClient {
5556
readonly daprHost: string;
5657
readonly daprPort: string;
5758
readonly options: DaprClientOptions;
5859
readonly communicationProtocol: CommunicationProtocolEnum;
60+
readonly logger: Logger;
5961

6062
readonly daprClient: IClient;
6163
readonly pubsub: IClientPubSub;
@@ -74,13 +76,14 @@ export default class DaprClient {
7476
, daprPort?: string
7577
, communicationProtocol: CommunicationProtocolEnum = CommunicationProtocolEnum.HTTP
7678
, options: DaprClientOptions = {
77-
isKeepAlive: true
78-
}
79+
isKeepAlive: true,
80+
},
7981
) {
8082
this.daprHost = daprHost ?? Settings.getDefaultHost();
8183
this.daprPort = daprPort ?? Settings.getDefaultPort(communicationProtocol);
8284
this.communicationProtocol = communicationProtocol;
8385
this.options = options;
86+
this.logger = new Logger(this.options.loggerOptions);
8487

8588
// Validation on port
8689
if (!/^[0-9]+$/.test(this.daprPort)) {
@@ -90,11 +93,11 @@ export default class DaprClient {
9093
// Builder
9194
switch (communicationProtocol) {
9295
case CommunicationProtocolEnum.GRPC: {
93-
const client = new GRPCClient(this.daprHost, this.daprPort, this.options);
96+
const client = new GRPCClient(this.daprHost, this.daprPort, this.options, this.logger);
9497
this.daprClient = client;
9598

9699
this.state = new GRPCClientState(client);
97-
this.pubsub = new GRPCClientPubSub(client);
100+
this.pubsub = new GRPCClientPubSub(client, this.logger);
98101
this.binding = new GRPCClientBinding(client);
99102
this.invoker = new GRPCClientInvoker(client);
100103
this.secret = new GRPCClientSecret(client);
@@ -107,11 +110,11 @@ export default class DaprClient {
107110
}
108111
case CommunicationProtocolEnum.HTTP:
109112
default: {
110-
const client = new HTTPClient(this.daprHost, this.daprPort, this.options);
113+
const client = new HTTPClient(this.daprHost, this.daprPort, this.options, this.logger);
111114
this.daprClient = client;
112115

113116
this.state = new HTTPClientState(client);
114-
this.pubsub = new HTTPClientPubSub(client);
117+
this.pubsub = new HTTPClientPubSub(client, this.logger);
115118
this.binding = new HTTPClientBinding(client);
116119
this.invoker = new HTTPClientInvoker(client);
117120
this.secret = new HTTPClientSecret(client);

src/implementation/Client/GRPCClient/GRPCClient.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,34 @@ import IClient from "../../../interfaces/Client/IClient";
1818
import CommunicationProtocolEnum from "../../../enum/CommunicationProtocol.enum";
1919
import { DaprClientOptions } from "../../../types/DaprClientOptions";
2020
import { Settings } from '../../../utils/Settings.util';
21+
import { Logger } from "../../../logger/Logger";
2122

2223
export default class GRPCClient implements IClient {
2324
private readonly client: DaprClient;
2425
private readonly clientCredentials: grpc.ChannelCredentials;
2526
private readonly clientHost: string;
2627
private readonly clientPort: string;
2728
private readonly options: DaprClientOptions;
29+
private readonly logger: Logger;
30+
31+
private readonly LOG_COMPONENT: string = "GRPCClient";
32+
private readonly LOG_AREA: string = "GRPCClient";
2833

2934
constructor(
3035
host = Settings.getDefaultHost()
3136
, port = Settings.getDefaultGrpcPort()
3237
, options: DaprClientOptions = {
3338
isKeepAlive: true
34-
}
39+
},
40+
logger: Logger,
3541
) {
3642
this.clientHost = host;
3743
this.clientPort = port;
3844
this.clientCredentials = ChannelCredentials.createInsecure();
3945
this.options = options;
46+
this.logger = logger;
4047

41-
console.log(`[Dapr-JS][gRPC] Opening connection to ${this.clientHost}:${this.clientPort}`);
48+
this.logger.info(this.LOG_COMPONENT, this.LOG_AREA,`Opening connection to ${this.clientHost}:${this.clientPort}`);
4249
this.client = new DaprClient(`${this.clientHost}:${this.clientPort}`, this.clientCredentials);
4350
}
4451

src/implementation/Client/GRPCClient/pubsub.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@ limitations under the License.
1414
import GRPCClient from './GRPCClient';
1515
import { PublishEventRequest } from "../../../proto/dapr/proto/runtime/v1/dapr_pb";
1616
import IClientPubSub from "../../../interfaces/Client/IClientPubSub";
17+
import { Logger } from '../../../logger/Logger';
1718

1819
// https://docs.dapr.io/reference/api/pubsub_api/
1920
export default class GRPCClientPubSub implements IClientPubSub {
2021
client: GRPCClient;
22+
23+
private readonly logger: Logger;
24+
private readonly LOG_COMPONENT: string = "GRPCClient";
25+
private readonly LOG_AREA: string = "PubSub";
2126

22-
constructor(client: GRPCClient) {
27+
constructor(client: GRPCClient, logger: Logger) {
2328
this.client = client;
29+
this.logger = logger;
2430
}
2531

2632
// @todo: should return a specific typed Promise<TypePubSubPublishResponse> instead of Promise<any>
@@ -34,7 +40,7 @@ export default class GRPCClientPubSub implements IClientPubSub {
3440
const client = this.client.getClient();
3541
client.publishEvent(msgService, (err, _res) => {
3642
if (err) {
37-
console.error(err);
43+
this.logger.error(this.LOG_COMPONENT, this.LOG_AREA, `publish failed: ${err}`);
3844
return reject(false);
3945
}
4046

src/implementation/Client/HTTPClient/HTTPClient.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,34 @@ import http from "http";
1818
import https from "https";
1919
import { DaprClientOptions } from "../../../types/DaprClientOptions";
2020
import { Settings } from '../../../utils/Settings.util';
21+
import { Logger } from "../../../logger/Logger";
2122

2223
export default class HTTPClient implements IClient {
2324
private client: typeof fetch;
2425
private readonly clientHost: string;
2526
private readonly clientPort: string;
2627
private readonly clientUrl: string;
2728
private readonly options: DaprClientOptions;
29+
private readonly logger: Logger;
2830

2931
private readonly httpAgent;
3032
private readonly httpsAgent;
3133

34+
private readonly LOG_COMPONENT: string = "HTTPClient";
35+
private readonly LOG_AREA: string = "HTTPClient";
36+
3237
constructor(
3338
host = Settings.getDefaultHost()
3439
, port = Settings.getDefaultHttpPort()
3540
, options: DaprClientOptions = {
3641
isKeepAlive: true
37-
}
42+
},
43+
logger: Logger,
3844
) {
3945
this.clientHost = host;
4046
this.clientPort = port;
4147
this.options = options;
48+
this.logger = logger;
4249

4350
if (!this.clientHost.startsWith('http://') && !this.clientHost.startsWith('https://')) {
4451
this.clientUrl = `http://${this.clientHost}:${this.clientPort}/v1.0`;
@@ -109,7 +116,7 @@ export default class HTTPClient implements IClient {
109116
params.headers["Content-Type"] = "text/plain";
110117
break;
111118
default:
112-
console.log(`Unknown body type: ${typeof params?.body}, defaulting to "text/plain"`);
119+
this.logger.warn(this.LOG_COMPONENT, this.LOG_AREA, `Unknown body type: ${typeof params?.body}, defaulting to "text/plain"`);
113120
params.headers["Content-Type"] = "text/plain";
114121
break;
115122
}
@@ -120,7 +127,7 @@ export default class HTTPClient implements IClient {
120127
const agent = urlFull.startsWith("https") ? this.httpsAgent : this.httpAgent;
121128
params.agent = agent;
122129

123-
// console.log(`${params.method} - ${urlFull} (${params.body})`);
130+
this.logger.debug(this.LOG_COMPONENT, this.LOG_AREA, `Fetching ${params.method} ${urlFull} with body: (${params.body})`);
124131
const res = await fetch(urlFull, params);
125132

126133
// Parse body
@@ -151,7 +158,7 @@ export default class HTTPClient implements IClient {
151158
}
152159
// All the others
153160
else {
154-
console.log(txtParsed);
161+
this.logger.debug(this.LOG_COMPONENT, this.LOG_AREA, "Response text: %s", txtParsed);
155162
throw new Error(JSON.stringify({
156163
error: "UNKNOWN",
157164
error_msg: `An unknown problem occured and we got the status ${res.status} with response ${JSON.stringify(res)}`

src/implementation/Client/HTTPClient/pubsub.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@ limitations under the License.
1313

1414
import HTTPClient from './HTTPClient';
1515
import IClientPubSub from '../../../interfaces/Client/IClientPubSub';
16+
import { Logger } from '../../../logger/Logger';
1617

1718
// https://docs.dapr.io/reference/api/pubsub_api/
1819
export default class HTTPClientPubSub implements IClientPubSub {
1920
client: HTTPClient;
21+
private readonly logger: Logger;
22+
private readonly LOG_COMPONENT: string = "HTTPClient";
23+
private readonly LOG_AREA: string = "PubSub";
2024

21-
constructor(client: HTTPClient) {
25+
constructor(client: HTTPClient, logger: Logger) {
2226
this.client = client;
27+
this.logger = logger;
2328
}
2429

2530
async publish(pubSubName: string, topic: string, data: object = {}): Promise<boolean> {
@@ -31,8 +36,8 @@ export default class HTTPClientPubSub implements IClientPubSub {
3136
},
3237
body: JSON.stringify(data),
3338
});
34-
} catch (e) {
35-
console.error(e);
39+
} catch (e: any) {
40+
this.logger.error(this.LOG_COMPONENT, this.LOG_AREA, `publish failed: ${e}`);
3641
return false;
3742
}
3843

src/interfaces/Client/IClient.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ limitations under the License.
1313

1414
import CommunicationProtocolEnum from "../../enum/CommunicationProtocol.enum";
1515
import { DaprClientOptions } from "../../types/DaprClientOptions";
16+
import { LoggerOptions } from "../../types/logger/LoggerOptions";
1617

1718
export default interface IClient {
1819
getClient(): any; // dependent on implementation

src/types/DaprClientOptions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ See the License for the specific language governing permissions and
1111
limitations under the License.
1212
*/
1313

14+
import { LoggerOptions } from "./logger/LoggerOptions";
15+
1416
export type DaprClientOptions = {
1517
isKeepAlive: boolean;
18+
loggerOptions?: LoggerOptions;
1619
}

0 commit comments

Comments
 (0)