Files
AstrBot/desktop/lib/electron-logger.js
エイカク dc995af34b fix(desktop): 为 Electron 与后端日志增加按大小轮转 (#5029)
* fix(desktop): rotate electron and backend logs

* refactor(desktop): centralize log rotation defaults and debug fs errors

* fix(desktop): harden rotation fs ops and buffer backend log writes

* refactor(desktop): extract buffered logger and reduce sync stat calls

* refactor(desktop): simplify rotation flow and harden logger config

* fix(desktop): make app logging async and flush-safe

* fix: harden app log path switching and debug-gated rotation errors

* fix: cap buffered log chunk size during path switch
2026-02-11 20:17:57 +09:00

51 lines
1.2 KiB
JavaScript

'use strict';
const path = require('path');
const { RotatingLogWriter } = require('./rotating-log-writer');
const { parseLogBackupCount, parseLogMaxBytes } = require('./common');
function createElectronLogger({ app, getRootDir }) {
const electronLogMaxBytes = parseLogMaxBytes(
process.env.ASTRBOT_ELECTRON_LOG_MAX_MB,
);
const electronLogBackupCount = parseLogBackupCount(
process.env.ASTRBOT_ELECTRON_LOG_BACKUP_COUNT,
);
const writer = new RotatingLogWriter({
logPath: null,
maxBytes: electronLogMaxBytes,
backupCount: electronLogBackupCount,
label: 'electron-log',
});
function getElectronLogPath() {
const rootDir =
process.env.ASTRBOT_ROOT ||
(typeof getRootDir === 'function' ? getRootDir() : null) ||
app.getPath('userData');
return path.join(rootDir, 'logs', 'electron.log');
}
function logElectron(message) {
const logPath = getElectronLogPath();
const line = `[${new Date().toISOString()}] ${message}\n`;
void writer.setLogPath(logPath);
void writer.append(line);
}
async function flushElectron() {
await writer.flush();
}
return {
getElectronLogPath,
logElectron,
flushElectron,
};
}
module.exports = {
createElectronLogger,
};