Skip to content

Commit

Permalink
Use logger from imapClient object
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Mar 15, 2024
1 parent e7f2826 commit 8bb3597
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions lib/imap-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ class IMAPConnection extends BaseClient {
try {
await mailbox.onExists(event);
} catch (err) {
this.logger.error({ msg: 'Exists error', err });
imapClient.logger.error({ msg: 'Exists error', err });
}
});

Expand All @@ -592,7 +592,7 @@ class IMAPConnection extends BaseClient {
try {
await mailbox.onOpen(event);
} catch (err) {
this.logger.error({ msg: 'Open error', err });
imapClient.logger.error({ msg: 'Open error', err });
}
});

Expand All @@ -605,7 +605,7 @@ class IMAPConnection extends BaseClient {
try {
await mailbox.onClose(event);
} catch (err) {
this.logger.error({ msg: 'Close error', err });
imapClient.logger.error({ msg: 'Close error', err });
}
});

Expand All @@ -618,12 +618,12 @@ class IMAPConnection extends BaseClient {
try {
await mailbox.onFlags(event);
} catch (err) {
this.logger.error({ msg: 'Flags error', err });
imapClient.logger.error({ msg: 'Flags error', err });
}
});

imapClient.on('close', async () => {
this.logger.info({ msg: 'Connection closed', type: 'imapClient', account: this.account, disabled: imapClient.disabled });
imapClient.logger.info({ msg: 'Connection closed', type: 'imapClient', account: this.account, disabled: imapClient.disabled });

try {
for (let [, mailbox] of this.mailboxes) {
Expand Down Expand Up @@ -653,10 +653,11 @@ class IMAPConnection extends BaseClient {
}

if (!imapClient.disabled) {
imapClient.logger.debug({ msg: 'Requesting reconnection due to unexpected close', type: 'imapClient', account: this.account });
await this.reconnect();
}
} catch (err) {
this.logger.error({ msg: 'Connection close error', err });
imapClient.logger.error({ msg: 'Connection close error', err });
}
});

Expand All @@ -679,16 +680,16 @@ class IMAPConnection extends BaseClient {
return false;
}

this._connecting = true;
this.isClosed = false;

if (force) {
this.closeSubconnections();
if (this.connectionClient) {
this.connectionClient.close();
}
}

this._connecting = true;
this.isClosed = false;

try {
this.logger.debug({ msg: 'Initiating connection to IMAP' });
await backOff(() => this.start(), {
Expand Down Expand Up @@ -1224,16 +1225,17 @@ class IMAPConnection extends BaseClient {
let imapConfig = await this.getImapConfig(accountData);
imapConfig.id = `${imapConfig.id}:m:${this.connectionCount++}`;

this.imapClient = new ImapFlow(
let imapClient = new ImapFlow(
Object.assign({}, imapConfig, {
expungeHandler: async payload => await this.expungeHandler(payload)
})
);
this.imapClient = imapClient;

this.imapClient.primaryConnection = true;
imapClient.primaryConnection = true;

// if emitLogs option is true then separate log event is fired for every log entry
this.imapClient.on('log', entry => {
imapClient.on('log', entry => {
if (!entry) {
return false;
}
Expand All @@ -1246,18 +1248,21 @@ class IMAPConnection extends BaseClient {
this.accountLogger.log(entry);
});

this.imapClient.on('error', err => {
imapClient.on('error', err => {
this.logger.error({ msg: 'IMAP connection error', type: 'imapClient', account: this.account, err });
if (imapClient !== this.imapClient) {
return;
}
this.reconnect().catch(err => {
this.logger.error({ msg: 'IMAP reconnection error', account: this.account, err });
});
});

this.imapClient.on('response', data => {
imapClient.on('response', data => {
metricsMeta({}, this.logger, 'imapResponses', 'inc', data);

// update byte counters as well
let imapStats = this.imapClient.stats(true);
let imapStats = imapClient.stats(true);

metricsMeta({}, this.logger, 'imapBytesSent', 'inc', imapStats.sent);
metricsMeta({}, this.logger, 'imapBytesReceived', 'inc', imapStats.received);
Expand Down

0 comments on commit 8bb3597

Please sign in to comment.