Skip to content

Commit a277961

Browse files
committed
log verbose in file redis
1 parent f51c3b6 commit a277961

5 files changed

Lines changed: 69 additions & 6 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ lerna-debug.log*
3333
!/instances/.gitkeep
3434
/test/
3535
/src/env.yml
36+
37+
/temp/*

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
"homepage": "https://github.com/DavidsonGomes/evolution-api#readme",
4242
"dependencies": {
4343
"@adiwajshing/keyed-db": "^0.2.4",
44-
"@whiskeysockets/baileys": "^6.3.0",
4544
"@ffmpeg-installer/ffmpeg": "^1.1.0",
4645
"@hapi/boom": "^10.0.1",
46+
"@whiskeysockets/baileys": "^6.3.0",
4747
"axios": "^1.3.5",
4848
"class-validator": "^0.13.2",
4949
"compression": "^1.7.4",

src/config/logger.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ export class Logger {
6767
this.configService.get<Log>('LOG').LEVEL.forEach((level) => types.push(Type[level]));
6868

6969
const typeValue = typeof value;
70-
7170
if (types.includes(type)) {
7271
if (configService.get<Log>('LOG').COLOR) {
7372
console.log(

src/db/redis.client.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import { Redis } from '../config/env.config';
55

66
export class RedisCache {
77
constructor() {
8+
this.logger.verbose('instance created');
89
process.on('beforeExit', async () => {
10+
this.logger.verbose('instance destroyed');
911
if (this.statusConnection) {
12+
this.logger.verbose('instance disconnect');
1013
await this.client.disconnect();
1114
}
1215
});
@@ -17,11 +20,14 @@ export class RedisCache {
1720
private redisEnv: Redis;
1821

1922
public set reference(reference: string) {
23+
this.logger.verbose('set reference: ' + reference);
2024
this.instanceName = reference;
2125
}
2226

2327
public async connect(redisEnv: Redis) {
28+
this.logger.verbose('connecting');
2429
this.client = createClient({ url: redisEnv.URI });
30+
this.logger.verbose('connected in ' + redisEnv.URI);
2531
await this.client.connect();
2632
this.statusConnection = true;
2733
this.redisEnv = redisEnv;
@@ -32,6 +38,7 @@ export class RedisCache {
3238

3339
public async instanceKeys(): Promise<string[]> {
3440
try {
41+
this.logger.verbose('instance keys: ' + this.redisEnv.PREFIX_KEY + ':*');
3542
return await this.client.sendCommand(['keys', this.redisEnv.PREFIX_KEY + ':*']);
3643
} catch (error) {
3744
this.logger.error(error);
@@ -40,13 +47,16 @@ export class RedisCache {
4047

4148
public async keyExists(key?: string) {
4249
if (key) {
50+
this.logger.verbose('keyExists: ' + key);
4351
return !!(await this.instanceKeys()).find((i) => i === key);
4452
}
53+
this.logger.verbose('keyExists: ' + this.instanceName);
4554
return !!(await this.instanceKeys()).find((i) => i === this.instanceName);
4655
}
4756

4857
public async writeData(field: string, data: any) {
4958
try {
59+
this.logger.verbose('writeData: ' + field);
5060
const json = JSON.stringify(data, BufferJSON.replacer);
5161

5262
return await this.client.hSet(
@@ -61,21 +71,27 @@ export class RedisCache {
6171

6272
public async readData(field: string) {
6373
try {
74+
this.logger.verbose('readData: ' + field);
6475
const data = await this.client.hGet(
6576
this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
6677
field,
6778
);
6879

6980
if (data) {
81+
this.logger.verbose('readData: ' + field + ' success');
7082
return JSON.parse(data, BufferJSON.reviver);
7183
}
84+
85+
this.logger.verbose('readData: ' + field + ' not found');
86+
return null;
7287
} catch (error) {
7388
this.logger.error(error);
7489
}
7590
}
7691

7792
public async removeData(field: string) {
7893
try {
94+
this.logger.verbose('removeData: ' + field);
7995
return await this.client.hDel(
8096
this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
8197
field,
@@ -87,6 +103,7 @@ export class RedisCache {
87103

88104
public async delAll(hash?: string) {
89105
try {
106+
this.logger.verbose('instance delAll: ' + hash);
90107
return await this.client.del(
91108
hash || this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
92109
);

src/whatsapp/services/whatsapp.service.ts

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export class WAStartupService {
126126
private readonly repository: RepositoryBroker,
127127
private readonly cache: RedisCache,
128128
) {
129+
this.logger.verbose('WAStartupService initialized');
129130
this.cleanStore();
130131
this.instance.qrcode = { count: 0 };
131132
}
@@ -148,6 +149,7 @@ export class WAStartupService {
148149
return;
149150
}
150151
this.instance.name = name;
152+
this.logger.verbose(`Instance '${this.instance.name}' initialized`);
151153
this.sendDataWebhook(Events.STATUS_INSTANCE, {
152154
instance: this.instance.name,
153155
status: 'created',
@@ -163,9 +165,12 @@ export class WAStartupService {
163165
}
164166

165167
public async getProfileName() {
168+
this.logger.verbose('Getting profile name');
166169
let profileName = this.client.user?.name ?? this.client.user?.verifiedName;
167170
if (!profileName) {
171+
this.logger.verbose('Profile name not found, trying to get from database');
168172
if (this.configService.get<Database>('DATABASE').ENABLED) {
173+
this.logger.verbose('Database enabled, trying to get from database');
169174
const collection = dbserver
170175
.getClient()
171176
.db(
@@ -175,10 +180,12 @@ export class WAStartupService {
175180
.collection(this.instanceName);
176181
const data = await collection.findOne({ _id: 'creds' });
177182
if (data) {
183+
this.logger.verbose('Profile name found in database');
178184
const creds = JSON.parse(JSON.stringify(data), BufferJSON.reviver);
179185
profileName = creds.me?.name || creds.me?.verifiedName;
180186
}
181187
} else if (existsSync(join(INSTANCE_DIR, this.instanceName, 'creds.json'))) {
188+
this.logger.verbose('Profile name found in file');
182189
const creds = JSON.parse(
183190
readFileSync(join(INSTANCE_DIR, this.instanceName, 'creds.json'), {
184191
encoding: 'utf-8',
@@ -187,41 +194,71 @@ export class WAStartupService {
187194
profileName = creds.me?.name || creds.me?.verifiedName;
188195
}
189196
}
197+
198+
this.logger.verbose(`Profile name: ${profileName}`);
190199
return profileName;
191200
}
192201

193202
public async getProfileStatus() {
203+
this.logger.verbose('Getting profile status');
194204
const status = await this.client.fetchStatus(this.instance.wuid);
195205

206+
this.logger.verbose(`Profile status: ${status.status}`);
196207
return status.status;
197208
}
198209

199210
public get profilePictureUrl() {
211+
this.logger.verbose('Getting profile picture url');
200212
return this.instance.profilePictureUrl;
201213
}
202214

203215
public get qrCode(): wa.QrCode {
216+
this.logger.verbose('Getting qrcode');
204217
return {
205218
code: this.instance.qrcode?.code,
206219
base64: this.instance.qrcode?.base64,
207220
};
208221
}
209222

210223
private async loadWebhook() {
224+
this.logger.verbose('Loading webhook');
211225
const data = await this.repository.webhook.find(this.instanceName);
212226
this.localWebhook.url = data?.url;
227+
this.logger.verbose(`Webhook url: ${this.localWebhook.url}`);
228+
213229
this.localWebhook.enabled = data?.enabled;
230+
this.logger.verbose(`Webhook enabled: ${this.localWebhook.enabled}`);
231+
214232
this.localWebhook.events = data?.events;
233+
this.logger.verbose(`Webhook events: ${this.localWebhook.events}`);
234+
215235
this.localWebhook.webhook_by_events = data?.webhook_by_events;
236+
this.logger.verbose(`Webhook by events: ${this.localWebhook.webhook_by_events}`);
237+
238+
this.logger.verbose('Webhook loaded');
216239
}
217240

218241
public async setWebhook(data: WebhookRaw) {
242+
this.logger.verbose('Setting webhook');
219243
await this.repository.webhook.create(data, this.instanceName);
244+
this.logger.verbose(`Webhook url: ${data.url}`);
245+
this.logger.verbose(`Webhook events: ${data.events}`);
220246
Object.assign(this.localWebhook, data);
247+
this.logger.verbose('Webhook set');
221248
}
222249

223250
public async findWebhook() {
224-
return await this.repository.webhook.find(this.instanceName);
251+
this.logger.verbose('Finding webhook');
252+
const data = await this.repository.webhook.find(this.instanceName);
253+
254+
if (!data) {
255+
this.logger.verbose('Webhook not found');
256+
throw new NotFoundException('Webhook not found');
257+
}
258+
259+
this.logger.verbose(`Webhook url: ${data.url}`);
260+
this.logger.verbose(`Webhook events: ${data.events}`);
261+
return data;
225262
}
226263

227264
public async sendDataWebhook<T = any>(event: Events, data: T, local = true) {
@@ -474,6 +511,13 @@ export class WAStartupService {
474511
this.instance.wuid,
475512
)}/*.json`,
476513
);
514+
this.logger.verbose(
515+
`Cleaned ${join(
516+
this.storePath,
517+
key.toLowerCase().replace('_', '-'),
518+
this.instance.wuid,
519+
)}/*.json`,
520+
);
477521
}
478522
}
479523
} catch (error) {}
@@ -1189,20 +1233,21 @@ export class WAStartupService {
11891233
private async convertToWebP(image: string) {
11901234
try {
11911235
let imagePath: string;
1192-
const outputPath = `${join(process.cwd(), 'temp', 'sticker.webp')}`;
1236+
const timestamp = new Date().getTime();
1237+
const outputPath = `${join(process.cwd(), 'temp', `${timestamp}.webp`)}`;
11931238

11941239
if (isBase64(image)) {
11951240
const base64Data = image.replace(/^data:image\/(jpeg|png|gif);base64,/, '');
11961241
const imageBuffer = Buffer.from(base64Data, 'base64');
1197-
imagePath = `${join(process.cwd(), 'temp', 'temp-sticker.png')}`;
1242+
imagePath = `${join(process.cwd(), 'temp', `temp-${timestamp}.png`)}`;
11981243
await sharp(imageBuffer).toFile(imagePath);
11991244
} else {
12001245
const timestamp = new Date().getTime();
12011246
const url = `${image}?timestamp=${timestamp}`;
12021247

12031248
const response = await axios.get(url, { responseType: 'arraybuffer' });
12041249
const imageBuffer = Buffer.from(response.data, 'binary');
1205-
imagePath = `${join(process.cwd(), 'temp', 'temp-sticker.png')}`;
1250+
imagePath = `${join(process.cwd(), 'temp', `temp-${timestamp}.png`)}`;
12061251
await sharp(imageBuffer).toFile(imagePath);
12071252
}
12081253

0 commit comments

Comments
 (0)