| 123456789101112131415161718192021222324252627282930 |
- import { LOG_DIR } from './log-path.js';
- import path from 'path';
- import fs from 'fs';
- const MAX_SIZE = 2 * 1024 * 1024; // 2 MB
- function getBaseName() {
- return new Date().toISOString().slice(0, 10);
- }
- function pad(n) { return n.toString().padStart(3, '0'); }
- function currentLogFile() {
- let idx = 0;
- while (true) {
- const name = `${getBaseName()}_${pad(idx)}.log`;
- const p = path.join(LOG_DIR, name);
- try {
- const stat = fs.statSync(p);
- if (stat.size < MAX_SIZE) return p;
- idx++;
- } catch {
- return p; // 文件不存在,直接用它
- }
- }
- }
- export function writeLog(level, msg) {
- const line = `[${new Date().toISOString()}] [${level}] ${msg}\n`;
- fs.appendFileSync(currentLogFile(), line);
- }
|