[feat] log

This commit is contained in:
zhhe committed 2026-11-17 07:58:03 +08:00
1 parent 1fcf56e3dd
commit 44e203d868
14 files changed
+305 -210

No files matched your search

+16 -4
View File
@@ -23,6 +23,7 @@ export class MailGateway {
allowed;
accountKey = '';
generation = 0;
logs = [];
status = {
phase: 'disabled',
supportsIdle: false,
@@ -37,7 +38,16 @@ export class MailGateway {
if (!this.sender)
throw new Error('SMTP 尚未配置');
await this.sender.send(job, text);
}, onLog);
this.record(`[mail] 已回信 ${job.from}`);
}, message => this.record(message));
}
/** 所有运行日志都进内存环形缓冲,设置页可直接查看,同时保留控制台输出。 */
record(message) {
const time = new Date().toLocaleTimeString('zh-CN', { hour12: false });
this.logs.push(`${time} ${message}`);
if (this.logs.length > 200)
this.logs.splice(0, this.logs.length - 200);
this.onLog(message);
}
start() {
this.restart();
@@ -72,6 +82,7 @@ export class MailGateway {
passwordSet: Boolean(this.mailbox.password),
allowedAddresses: [...this.allowed],
status: this.status,
logs: [...this.logs],
};
}
restart() {
@@ -83,9 +94,10 @@ export class MailGateway {
if (!isMailboxConfigured(this.mailbox)) {
this.accountKey = '';
this.status = { phase: 'disabled', supportsIdle: false, pollIntervalMs: POLL_INTERVAL_MS };
this.onLog('[mail] 未配置完整邮箱,收信入口保持关闭');
this.record('[mail] 未配置完整邮箱,收信入口保持关闭');
return;
}
this.record(`[mail] 开始连接 ${this.mailbox.imapHost}:${this.mailbox.imapPort}`);
this.accountKey = mailboxKey(this.mailbox);
this.sender = new SmtpSender(this.mailbox);
this.receiver = new ImapReceiver(this.mailbox, () => this.policy, new CursorStore(`${this.mailbox.email}@${this.mailbox.imapHost}`), {
@@ -94,7 +106,7 @@ export class MailGateway {
if (generation === this.generation)
this.status = status;
},
onLog: this.onLog,
onLog: message => this.record(message),
});
this.receiver.start();
}
@@ -114,7 +126,7 @@ export class MailGateway {
await this.deliverer.deliver(job);
}
catch (error) {
this.onLog(`[mail] 投递 ${job.from} 失败: ${errorMessage(error)}`);
this.record(`[mail] 投递 ${job.from} 失败: ${errorMessage(error)}`);
}
}
}