Skip to content

Commit e64f010

Browse files
committed
update log
1 parent 0ccd4b7 commit e64f010

File tree

8 files changed

+28
-38
lines changed

8 files changed

+28
-38
lines changed

server/src/channels/base.channel.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { BOT_ENDPOINT, PUBLIC_DOMAIN } from "@/config";
2+
import { logger } from "@/utils/logger";
23
import axios from "axios";
34

45
export class BaseChannel {
@@ -39,14 +40,10 @@ export class BaseChannel {
3940
},
4041
})
4142
if (postMsg.data.success) {
42-
console.log(
43-
`[${this.channelType} - ${this.contactName} ${this.contactId}] - [Conversation ID: ${uid}] - [Send message to bot - Message: ${message}] - [Data: ${data}]`
44-
)
43+
logger.info(`[${this.channelType}] User ${userId} send message to Bot - Message: ${message} - Data: ${data}`)
4544
}
4645
} catch (error) {
47-
console.log(
48-
`[${this.channelType} - ${this.contactName} ${this.contactId}] - [Conversation ID: ${uid}] - [Can not send message to bot - Message: ${message}] - [Error: ${error.message}]`
49-
);
46+
logger.info(`[${this.channelType}] User ${userId} can not send message to Bot - Message: ${message} - Error: ${error.message}`);
5047
}
5148
}
5249

server/src/channels/line.channel.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { PUBLIC_DOMAIN } from "@/config";
2+
import { logger } from "@/utils/logger";
23
import axios from "axios";
34
import { Request, Response } from "express";
45
import { BaseChannel } from "./base.channel";
@@ -36,9 +37,9 @@ export class LineChannel extends BaseChannel {
3637
'Content-Type': 'application/json',
3738
},
3839
});
39-
console.log(`[LIN] Registered webhook for ${this.channelType} - ${this.contactName} ${this.contactId}`);
40+
logger.info(`[LIN] Registered webhook for ${this.channelType} - ${this.contactName} ${this.contactId}`);
4041
} catch (e) {
41-
console.log(`[LIN] Can not register webhook for ${this.channelType} - ${this.contactName} ${this.contactId}`);
42+
logger.info(`[LIN] Can not register webhook for ${this.channelType} - ${this.contactName} ${this.contactId}`);
4243

4344
}
4445
}
@@ -56,7 +57,7 @@ export class LineChannel extends BaseChannel {
5657

5758
return data.userId;
5859
} catch (e) {
59-
console.log(`[LIN] Can not get user ID for ${this.channelType} - ${this.contactName} ${this.contactId}`);
60+
logger.info(`[LIN] Can not get user ID for ${this.channelType} - ${this.contactName} ${this.contactId}`);
6061
}
6162
}
6263

@@ -72,12 +73,8 @@ export class LineChannel extends BaseChannel {
7273
const { message, source } = events[0];
7374

7475
await this.postMessageToBot({ userId: source.userId, message: message.text, data: null });
75-
76-
console.log(`[LIN] Sent message: ${message.text} from ${lineUserId} to Bot`);
7776
}
78-
} catch (e) {
79-
console.log(`[LIN] ${this.contactId} Can not send message to Bot - ${e.message}`);
80-
}
77+
} catch (e) { }
8178
}
8279

8380
public async sendMessageToUser({ userId, text }) {
@@ -96,8 +93,10 @@ export class LineChannel extends BaseChannel {
9693
Authorization: 'Bearer ' + this.pageToken,
9794
},
9895
})
96+
97+
logger.info(`[LIN] Bot send message to User ${lineUserId} - Message: ${text}`);
9998
} catch (e) {
100-
console.log(`[LIN] Send message to User ${lineUserId} failed`);
99+
logger.info(`[LIN] Bot send message to User ${lineUserId} failed - Error: ${e.message}`);
101100
}
102101
}
103102
}

server/src/channels/messenger.channel.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Helper } from "@/utils/helper";
2+
import { logger } from "@/utils/logger";
23
import axios from "axios";
34
import { Request, Response } from "express";
45
import { BaseChannel } from "./base.channel";
@@ -33,7 +34,7 @@ export class MessengerChannel extends BaseChannel {
3334
let challenge = req.query['hub.challenge'];
3435

3536
if (mode === 'subscribe' && this.webhookSecret == token) {
36-
console.log(`[MSG] channel ${this.channelType} - ${this.contactName} ${this.contactId} webhook verified!`);
37+
logger.info(`[MSG] channel ${this.channelType} - ${this.contactName} ${this.contactId} webhook verified!`);
3738
return challenge;
3839
} else {
3940
console.error(`[MSG] Verification channel ${this.channelType} - ${this.contactName} ${this.contactId} failed!`);
@@ -89,9 +90,10 @@ export class MessengerChannel extends BaseChannel {
8990
message: { text },
9091
},
9192
});
92-
console.log(`[MSG] Sent message: ${text} from ${userId} to Bot`);
93+
94+
logger.info(`[MSG] Bot Sent message to User ${userId} - Message: ${text}`);
9395
} catch (e) {
94-
console.log(`[MSG] ${this.contactId} Can not send message to Bot - ${e.message}`);
96+
logger.info(`[MSG] Bot Sent message to User ${userId} failed - Error: ${e.message}`);
9597
}
9698
}
9799

@@ -115,7 +117,7 @@ export class MessengerChannel extends BaseChannel {
115117
},
116118
});
117119
} catch (e) {
118-
console.log('[MSG] Messenger can not send action to user', e.message);
120+
logger.info(`[MSG] Messenger can not send action to User - Error: ${e.message}`);
119121
}
120122
}
121123
}

server/src/i18n/ctx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Service } from 'typedi';
2-
import { Locales } from './i18n-types';
32
import L from './i18n-node';
3+
import { Locales } from './i18n-types';
44

55
@Service()
66
export class LocaleService {

server/src/services/channels.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
ChannelType,
1414
} from '@/interfaces/channels.interface';
1515
import { Paging } from '@/interfaces/paging.interface';
16+
import { logger } from '@/utils/logger';
1617
import { and, asc, desc, eq, inArray, like, sql } from 'drizzle-orm';
1718
import { StatusCodes } from 'http-status-codes';
1819
import { Inject, Service } from 'typedi';
@@ -239,7 +240,7 @@ export class ChannelService {
239240
initChannel(channel: ChannelInfo) {
240241
const { id, contactId, contactName, channelType, credentials } = channel;
241242

242-
console.log(`[Init channel] ${channelType} - ${contactName} ${contactId}`);
243+
logger.info(`[Init channel] ${channelType} - ${contactName} ${contactId}`);
243244

244245
switch (channelType) {
245246
case 'MSG':
@@ -263,7 +264,7 @@ export class ChannelService {
263264

264265
return linChannel;
265266
default:
266-
console.log(`[Init channel] Does not support channel type ${channel.channelType}`);
267+
logger.info(`[Init channel] Does not support channel type ${channel.channelType}`);
267268
break;
268269
}
269270

server/src/services/conversation.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { LineChannel } from "@/channels/line.channel";
22
import { MessengerChannel } from "@/channels/messenger.channel";
3+
import { logger } from "@/utils/logger";
34
import { Request } from "express-serve-static-core";
45
import { Service } from "typedi";
56
import { ChannelService } from "./channels.service";
@@ -15,7 +16,7 @@ export class ConversationService {
1516

1617
const expectedChannel = await this.chanelService.findOneByContactId(from.id);
1718

18-
if (!expectedChannel) console.log('Can not find channel to send to user!');
19+
if (!expectedChannel) logger.info('Can not find channel to send to user!');
1920

2021
const { id, contactName, channelType, credentials } = expectedChannel;
2122

@@ -35,7 +36,7 @@ export class ConversationService {
3536

3637
break;
3738
default:
38-
console.log(`[Incoming message] Send message to Bot: Does not support channel type ${channelType}`);
39+
logger.info(`[Incoming message] Send message to Bot: Does not support channel type ${channelType}`);
3940
break;
4041
}
4142
}

server/src/services/webhook.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { MessengerChannel } from "@/channels/messenger.channel";
33
import { LOCALE_KEY } from "@/constants";
44
import { HttpException } from "@/exceptions/http-exception";
55
import { LocaleService } from "@/i18n/ctx";
6+
import { logger } from "@/utils/logger";
67
import { Request, Response } from "express";
78
import { StatusCodes } from "http-status-codes";
89
import { Inject, Service } from "typedi";
@@ -48,7 +49,7 @@ export class WebhookService {
4849
const expectedChannel = await this.chanelService.findOneByContactId(contactId);
4950

5051
if (!expectedChannel) {
51-
console.log('[Incoming message] Can not find channel with id ', req.params.id);
52+
logger.info('[Incoming message] Can not find channel with id ', req.params.id);
5253
return;
5354
}
5455

@@ -66,7 +67,7 @@ export class WebhookService {
6667
prepareMessage = await lineChannel.prepareMessage(req, res);
6768
break;
6869
default:
69-
console.log(`[Incoming message] Does not support channel type ${channelType}`);
70+
logger.info(`[Incoming message] Does not support channel type ${channelType}`);
7071
break;
7172
}
7273

server/src/utils/logger.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,11 @@ logger.add(
6868
})
6969
);
7070

71-
['log', 'error', 'warn', 'info'].forEach((method) => {
72-
const originalMethod = console[method];
73-
console[method] = function () {
74-
const modifiedArgs = Array.from(arguments).map((arg) => (typeof arg === 'object' ? JSON.stringify(arg) : arg));
75-
originalMethod.call(console, ...modifiedArgs);
76-
console[method] = function () {
77-
if (method === 'log') return logger.info.apply(logger, arguments);
78-
return logger[method].apply(logger, arguments);
79-
};
80-
};
81-
});
82-
8371
const stream = {
8472
write: (message: string) => {
8573
logger.info(message.substring(0, message.lastIndexOf('\n')));
8674
},
8775
};
8876

8977
export { logger, stream };
78+

0 commit comments

Comments
 (0)