Skip to content

Commit e5a0562

Browse files
committed
Refactor the logger module
Signed-off-by: Shubham Sharma <[email protected]>
1 parent 30d99ae commit e5a0562

File tree

20 files changed

+108
-151
lines changed

20 files changed

+108
-151
lines changed

src/actors/client/ActorClient/ActorClient.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,16 @@ export default class ActorClient {
3535
this.communicationProtocol = communicationProtocol;
3636
this.options = options;
3737

38-
const logger = new Logger(this.options.logger);
39-
4038
// Builder
4139
switch (communicationProtocol) {
4240
case CommunicationProtocolEnum.GRPC: {
43-
const client = new GRPCClient(this.daprHost, this.daprPort, this.options, logger);
41+
const client = new GRPCClient(this.daprHost, this.daprPort, this.options);
4442
this.actor = new ActorClientGRPC(client);
4543
break;
4644
}
4745
case CommunicationProtocolEnum.HTTP:
4846
default: {
49-
const client = new HTTPClient(this.daprHost, this.daprPort, this.options, logger);
47+
const client = new HTTPClient(this.daprHost, this.daprPort, this.options);
5048
this.actor = new ActorClientHTTP(client);
5149
break;
5250
}

src/actors/runtime/AbstractActor.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ export default abstract class AbstractActor {
4545
private readonly actorType: any; // set at constructor level
4646
private readonly logger: Logger;
4747

48-
private readonly LOG_COMPONENT: string = "Actors";
49-
private readonly LOG_AREA: string = "AbstractActor";
50-
5148
/**
5249
* Instantiates a new Actor
5350
*
@@ -57,7 +54,7 @@ export default abstract class AbstractActor {
5754
constructor(daprClient: DaprClient, id: ActorId) {
5855
this.daprClient = daprClient;
5956
this.actorClient = new ActorClient(daprClient.getDaprHost(), daprClient.getDaprPort(), daprClient.getCommunicationProtocol(), daprClient.getOptions());
60-
this.logger = new Logger(daprClient.getOptions().logger)
57+
this.logger = new Logger("Actors", "AbstractActor", daprClient.getOptions().logger)
6158
this.id = id;
6259

6360
this.stateManager = new ActorStateManager(this);
@@ -206,7 +203,7 @@ export default abstract class AbstractActor {
206203
}
207204

208205
async receiveReminder(_data: string): Promise<void> {
209-
this.logger.warn(this.LOG_COMPONENT, this.LOG_AREA, JSON.stringify({
206+
this.logger.warn(JSON.stringify({
210207
error: "ACTOR_METHOD_NOT_IMPLEMENTED",
211208
errorMsg: `A reminder was created for the actor with id: ${this.id} but the method 'receiveReminder' was not implemented`,
212209
}));

src/implementation/Client/DaprClient.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ import CommunicationProtocolEnum from '../../enum/CommunicationProtocol.enum';
5353
import { DaprClientOptions } from '../../types/DaprClientOptions';
5454
import { Settings } from '../../utils/Settings.util';
5555
import { Logger } from '../../logger/Logger';
56+
import { LoggerOptions } from "../../types/logger/LoggerOptions";
5657

5758
export default class DaprClient {
5859
readonly daprHost: string;
5960
readonly daprPort: string;
6061
readonly options: DaprClientOptions;
6162
readonly communicationProtocol: CommunicationProtocolEnum;
62-
readonly logger: Logger;
6363

6464
readonly daprClient: IClient;
6565
readonly pubsub: IClientPubSub;
@@ -73,8 +73,7 @@ export default class DaprClient {
7373
readonly configuration: IClientConfiguration;
7474
readonly actor: IClientActorBuilder;
7575

76-
private readonly LOG_COMPONENT: string = "DaprClient";
77-
private readonly LOG_AREA: string = "DaprClient";
76+
private readonly logger: Logger;
7877

7978
constructor(
8079
daprHost?: string
@@ -88,7 +87,7 @@ export default class DaprClient {
8887
this.daprPort = daprPort ?? Settings.getDefaultPort(communicationProtocol);
8988
this.communicationProtocol = communicationProtocol;
9089
this.options = options;
91-
this.logger = new Logger(this.options.logger);
90+
this.logger = new Logger("DaprClient", "DaprClient", this.options.logger);
9291

9392
// Validation on port
9493
if (!/^[0-9]+$/.test(this.daprPort)) {
@@ -98,11 +97,11 @@ export default class DaprClient {
9897
// Builder
9998
switch (communicationProtocol) {
10099
case CommunicationProtocolEnum.GRPC: {
101-
const client = new GRPCClient(this.daprHost, this.daprPort, this.options, this.logger);
100+
const client = new GRPCClient(this.daprHost, this.daprPort, this.options);
102101
this.daprClient = client;
103102

104103
this.state = new GRPCClientState(client);
105-
this.pubsub = new GRPCClientPubSub(client, this.logger);
104+
this.pubsub = new GRPCClientPubSub(client);
106105
this.binding = new GRPCClientBinding(client);
107106
this.invoker = new GRPCClientInvoker(client);
108107
this.secret = new GRPCClientSecret(client);
@@ -115,11 +114,11 @@ export default class DaprClient {
115114
}
116115
case CommunicationProtocolEnum.HTTP:
117116
default: {
118-
const client = new HTTPClient(this.daprHost, this.daprPort, this.options, this.logger);
117+
const client = new HTTPClient(this.daprHost, this.daprPort, this.options);
119118
this.daprClient = client;
120119

121120
this.state = new HTTPClientState(client);
122-
this.pubsub = new HTTPClientPubSub(client, this.logger);
121+
this.pubsub = new HTTPClientPubSub(client);
123122
this.binding = new HTTPClientBinding(client);
124123
this.invoker = new HTTPClientInvoker(client);
125124
this.secret = new HTTPClientSecret(client);
@@ -148,9 +147,9 @@ export default class DaprClient {
148147
let isHealthyRetryCount = 0;
149148
const isHealthyMaxRetryCount = 60; // 1s startup delay and we try max for 60s
150149

151-
this.logger.info(this.LOG_COMPONENT, this.LOG_AREA, `Awaiting Sidecar to be Started`);
150+
this.logger.info(`Awaiting Sidecar to be Started`);
152151
while (!isHealthy) {
153-
this.logger.verbose(this.LOG_COMPONENT, this.LOG_AREA,`Waiting for the Dapr Sidecar to start, retry count: (#${isHealthyRetryCount})`);
152+
this.logger.verbose(`Waiting for the Dapr Sidecar to start, retry count: (#${isHealthyRetryCount})`);
154153
await NodeJSUtils.sleep(Settings.getDaprSidecarPollingDelayMs());
155154

156155
// Implement API call manually as we need to enable calling without initialization
@@ -177,7 +176,7 @@ export default class DaprClient {
177176
await this.awaitSidecarStarted();
178177
await this.daprClient.start();
179178
await this.daprClient.setIsInitialized(true);
180-
this.logger.info(this.LOG_COMPONENT, this.LOG_AREA, "Sidecar Started");
179+
this.logger.info("Sidecar Started");
181180
}
182181

183182
getDaprClient(): IClient {

src/implementation/Client/GRPCClient/GRPCClient.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,21 @@ export default class GRPCClient implements IClient {
2929
private readonly options: DaprClientOptions;
3030
private readonly logger: Logger;
3131

32-
private readonly LOG_COMPONENT: string = "GRPCClient";
33-
private readonly LOG_AREA: string = "GRPCClient";
34-
3532
constructor(
3633
host = Settings.getDefaultHost()
3734
, port = Settings.getDefaultGrpcPort()
3835
, options: DaprClientOptions = {
3936
isKeepAlive: true
4037
},
41-
logger: Logger,
4238
) {
4339
this.clientHost = host;
4440
this.clientPort = port;
4541
this.clientCredentials = grpc.ChannelCredentials.createInsecure();
4642
this.options = options;
47-
this.logger = logger;
43+
this.logger = new Logger("GRPCClient", "GRPCClient", options.logger);
4844
this.isInitialized = false;
4945

50-
this.logger.info(this.LOG_COMPONENT, this.LOG_AREA,`Opening connection to ${this.clientHost}:${this.clientPort}`);
46+
this.logger.info(`Opening connection to ${this.clientHost}:${this.clientPort}`);
5147
this.client = new DaprClient(`${this.clientHost}:${this.clientPort}`, this.clientCredentials);
5248
}
5349

@@ -85,7 +81,7 @@ export default class GRPCClient implements IClient {
8581
return new Promise((resolve, reject) => {
8682
this.client.waitForReady(deadline, (err?) => {
8783
if (err) {
88-
this.logger.error(this.LOG_COMPONENT, this.LOG_AREA, `Error waiting for client to be ready: ${err}`);
84+
this.logger.error(`Error waiting for client to be ready: ${err}`);
8985
return reject();
9086
}
9187

src/implementation/Client/GRPCClient/pubsub.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@ export default class GRPCClientPubSub implements IClientPubSub {
2121
client: GRPCClient;
2222

2323
private readonly logger: Logger;
24-
private readonly LOG_COMPONENT: string = "GRPCClient";
25-
private readonly LOG_AREA: string = "PubSub";
2624

27-
constructor(client: GRPCClient, logger: Logger) {
25+
constructor(client: GRPCClient) {
2826
this.client = client;
29-
this.logger = logger;
27+
this.logger = new Logger("GRPCClient", "PubSub", client.getOptions().logger);
3028
}
3129

3230
// @todo: should return a specific typed Promise<TypePubSubPublishResponse> instead of Promise<any>
@@ -40,7 +38,7 @@ export default class GRPCClientPubSub implements IClientPubSub {
4038
const client = this.client.getClient();
4139
client.publishEvent(msgService, (err, _res) => {
4240
if (err) {
43-
this.logger.error(this.LOG_COMPONENT, this.LOG_AREA, `publish failed: ${err}`);
41+
this.logger.error(`publish failed: ${err}`);
4442
return reject(false);
4543
}
4644

src/implementation/Client/HTTPClient/HTTPClient.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,17 @@ export default class HTTPClient implements IClient {
3434
private readonly httpAgent;
3535
private readonly httpsAgent;
3636

37-
private readonly LOG_COMPONENT: string = "HTTPClient";
38-
private readonly LOG_AREA: string = "HTTPClient";
39-
4037
constructor(
4138
host = Settings.getDefaultHost()
4239
, port = Settings.getDefaultHttpPort()
4340
, options: DaprClientOptions = {
4441
isKeepAlive: true
4542
},
46-
logger: Logger,
4743
) {
4844
this.clientHost = host;
4945
this.clientPort = port;
5046
this.options = options;
51-
this.logger = logger;
47+
this.logger = new Logger("HTTPClient", "HTTPClient", this.options.logger);
5248
this.isInitialized = false;
5349

5450
if (!this.clientHost.startsWith('http://') && !this.clientHost.startsWith('https://')) {
@@ -145,7 +141,7 @@ export default class HTTPClient implements IClient {
145141
params.headers["Content-Type"] = "text/plain";
146142
break;
147143
default:
148-
this.logger.warn(this.LOG_COMPONENT, this.LOG_AREA, `Unknown body type: ${typeof params?.body}, defaulting to "text/plain"`);
144+
this.logger.warn(`Unknown body type: ${typeof params?.body}, defaulting to "text/plain"`);
149145
params.headers["Content-Type"] = "text/plain";
150146
break;
151147
}
@@ -160,7 +156,7 @@ export default class HTTPClient implements IClient {
160156
await this.start();
161157
}
162158

163-
this.logger.debug(this.LOG_COMPONENT, this.LOG_AREA, `Fetching ${params.method} ${urlFull} with body: (${params.body})`);
159+
this.logger.debug(`Fetching ${params.method} ${urlFull} with body: (${params.body})`);
164160

165161
const res = await fetch(urlFull, params);
166162

@@ -192,7 +188,7 @@ export default class HTTPClient implements IClient {
192188
}
193189
// All the others
194190
else {
195-
this.logger.debug(this.LOG_COMPONENT, this.LOG_AREA, "Response text: %s", txtParsed);
191+
this.logger.debug("Execute response text: %s", txtParsed);
196192
throw new Error(JSON.stringify({
197193
error: "UNKNOWN",
198194
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: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ import { Logger } from '../../../logger/Logger';
1919
export default class HTTPClientPubSub implements IClientPubSub {
2020
client: HTTPClient;
2121
private readonly logger: Logger;
22-
private readonly LOG_COMPONENT: string = "HTTPClient";
23-
private readonly LOG_AREA: string = "PubSub";
2422

25-
constructor(client: HTTPClient, logger: Logger) {
23+
constructor(client: HTTPClient) {
2624
this.client = client;
27-
this.logger = logger;
25+
this.logger = new Logger("HTTPClient", "PubSub", client.getOptions().logger);
2826
}
2927

3028
async publish(pubSubName: string, topic: string, data: object = {}): Promise<boolean> {
@@ -37,7 +35,7 @@ export default class HTTPClientPubSub implements IClientPubSub {
3735
body: JSON.stringify(data),
3836
});
3937
} catch (e: any) {
40-
this.logger.error(this.LOG_COMPONENT, this.LOG_AREA, `publish failed: ${e}`);
38+
this.logger.error(`publish failed: ${e}`);
4139
return false;
4240
}
4341

src/implementation/Server/DaprServer.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ export default class DaprServer {
5151
readonly actor: IServerActor;
5252
readonly client: DaprClient;
5353

54-
private readonly LOG_COMPONENT: string = "DaprServer";
55-
private readonly LOG_AREA: string = "DaprServer";
56-
5754
constructor(
5855
serverHost?: string
5956
, serverPort?: string
@@ -68,7 +65,7 @@ export default class DaprServer {
6865
this.serverPort = serverPort ?? Settings.getDefaultAppPort(communicationProtocol);
6966
this.daprHost = daprHost ?? Settings.getDefaultHost();
7067
this.daprPort = daprPort ?? Settings.getDefaultPort(communicationProtocol);
71-
this.logger = new Logger(clientOptions.logger);
68+
this.logger = new Logger("DaprServer", "DaprServer", clientOptions.logger);
7269

7370
// Create a client to interface with the sidecar from the server side
7471
this.client = new DaprClient(daprHost, daprPort, communicationProtocol, clientOptions);
@@ -89,24 +86,24 @@ export default class DaprServer {
8986
// Builder
9087
switch (communicationProtocol) {
9188
case CommunicationProtocolEnum.GRPC: {
92-
const server = new GRPCServer(this.client, this.logger);
89+
const server = new GRPCServer(this.client);
9390
this.daprServer = server;
9491

95-
this.pubsub = new GRPCServerPubSub(server, this.logger);
96-
this.binding = new GRPCServerBinding(server, this.logger);
97-
this.invoker = new GRPCServerInvoker(server, this.logger);
92+
this.pubsub = new GRPCServerPubSub(server);
93+
this.binding = new GRPCServerBinding(server);
94+
this.invoker = new GRPCServerInvoker(server);
9895
this.actor = new GRPCServerActor(server);
9996
break;
10097
}
10198
case CommunicationProtocolEnum.HTTP:
10299
default: {
103-
const server = new HTTPServer(this.client, this.logger);
100+
const server = new HTTPServer(this.client);
104101
this.daprServer = server;
105102

106-
this.pubsub = new HTTPServerPubSub(server, this.logger);
107-
this.binding = new HTTPServerBinding(server, this.logger);
108-
this.invoker = new HTTPServerInvoker(server, this.logger);
109-
this.actor = new HTTPServerActor(server, this.client, this.logger);
103+
this.pubsub = new HTTPServerPubSub(server);
104+
this.binding = new HTTPServerBinding(server);
105+
this.invoker = new HTTPServerInvoker(server);
106+
this.actor = new HTTPServerActor(server, this.client);
110107
break;
111108
}
112109
}
@@ -120,7 +117,7 @@ export default class DaprServer {
120117
await this.client.start();
121118

122119
// We are initialized
123-
this.logger.info(this.LOG_COMPONENT, this.LOG_AREA, "Sidecar Started");
120+
this.logger.info("Sidecar Started");
124121
}
125122

126123
async stop(): Promise<void> {

src/implementation/Server/GRPCServer/GRPCServer.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,21 @@ export default class GRPCServer implements IServer {
3333
client: DaprClient;
3434
private readonly logger: Logger;
3535

36-
private readonly LOG_COMPONENT: string = "GRPCServer";
37-
private readonly LOG_AREA: string = "GRPCServer";
38-
39-
constructor(client: DaprClient, logger: Logger) {
36+
constructor(client: DaprClient) {
4037
this.isInitialized = false;
4138

4239
this.serverHost = "";
4340
this.serverPort = "";
4441
this.client = client;
45-
this.logger = logger;
42+
this.logger = new Logger("GRPCServer", "GRPCServer", client.options.logger);
4643

4744
// Create Server
4845
this.server = new grpc.Server();
4946
this.serverCredentials = grpc.ServerCredentials.createInsecure();
50-
this.serverImpl = new GRPCServerImpl(this.logger);
47+
this.serverImpl = new GRPCServerImpl(client.options.logger);
5148

5249
// Add our implementation
53-
this.logger.info(this.LOG_COMPONENT, this.LOG_AREA, "Adding Service Implementation - AppCallbackService")
50+
this.logger.info("Adding Service Implementation - AppCallbackService")
5451
// @ts-ignore
5552
this.server.addService(AppCallbackService, this.serverImpl);
5653
}
@@ -108,14 +105,14 @@ export default class GRPCServer implements IServer {
108105
}
109106

110107
private async initializeBind(): Promise<void> {
111-
this.logger.info(this.LOG_COMPONENT, this.LOG_AREA, `Starting to listen on ${this.serverHost}:${this.serverPort}`);
108+
this.logger.info(`Starting to listen on ${this.serverHost}:${this.serverPort}`);
112109
return new Promise((resolve, reject) => {
113110
this.server.bindAsync(`${this.serverHost}:${this.serverPort}`, this.serverCredentials, (err, _port) => {
114111
if (err) {
115112
return reject(err);
116113
}
117114

118-
this.logger.info(this.LOG_COMPONENT, this.LOG_AREA, `Listening on ${this.serverHost}:${this.serverPort}`);
115+
this.logger.info(`Listening on ${this.serverHost}:${this.serverPort}`);
119116
return resolve();
120117
});
121118
})

0 commit comments

Comments
 (0)