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 8bb3597 commit c70a1f7
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions lib/imap-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class IMAPConnection extends BaseClient {

let imapConfig = await this.getImapConfig(accountData);

this.commandClient = new ImapFlow(
let commandClient = new ImapFlow(
Object.assign({}, imapConfig, {
disableAutoIdle: true,
id: commandCid,
Expand All @@ -219,27 +219,29 @@ class IMAPConnection extends BaseClient {
})
);

this.commandClient.secondaryConnection = true;
this.commandClient = commandClient;

commandClient.secondaryConnection = true;

try {
await this.commandClient.connect();
this.logger.info({ msg: 'Command channel connected', cid: commandCid, channel: 'command', account: this.account });
await commandClient.connect();
commandClient.log.info({ msg: 'Command channel connected', cid: commandCid, channel: 'command', account: this.account });
} catch (err) {
this.logger.error({ msg: 'Failed to connect command client', cid: commandCid, channel: 'command', account: this.account, err });
commandClient.log.error({ msg: 'Failed to connect command client', cid: commandCid, channel: 'command', account: this.account, err });
throw err;
}

this.commandClient.on('error', err => {
this.logger.error({ msg: 'IMAP connection error', cid: commandCid, channel: 'command', account: this.account, err });
commandClient.on('error', err => {
commandClient.log.error({ msg: 'IMAP connection error', cid: commandCid, channel: 'command', account: this.account, err });
this.commandClient = null;
});

this.commandClient.on('close', async () => {
this.logger.info({ msg: 'Connection closed', cid: commandCid, channel: 'command', account: this.account });
commandClient.on('close', async () => {
commandClient.log.info({ msg: 'Connection closed', cid: commandCid, channel: 'command', account: this.account });
this.commandClient = null;
});

return this.commandClient;
return commandClient;
} finally {
await lock.releaseLock(connectLock);
}
Expand Down Expand Up @@ -579,7 +581,7 @@ class IMAPConnection extends BaseClient {
try {
await mailbox.onExists(event);
} catch (err) {
imapClient.logger.error({ msg: 'Exists error', err });
imapClient.log.error({ msg: 'Exists error', err });
}
});

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

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

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

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

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

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

Expand Down

0 comments on commit c70a1f7

Please sign in to comment.