MST

星途 面试题库

面试题:Node.js日志记录系统中如何实现日志分级与滚动切割

设计一个Node.js日志记录系统,要求实现不同级别的日志记录(如debug、info、warn、error),并且当日志文件达到一定大小后,能够自动进行滚动切割,以避免单个日志文件过大。请阐述你的设计思路,并给出关键代码示例。
26.8万 热度难度
前端开发Node.js

知识考点

AI 面试

面试题答案

一键面试

设计思路

  1. 日志级别定义:在代码中定义不同的日志级别常量,方便区分和使用。
  2. 日志记录函数:为每个日志级别创建对应的记录函数,在函数中拼接日志信息,包括时间、日志级别、具体消息等。
  3. 文件写入:使用Node.js内置的fs模块将日志信息写入文件。
  4. 滚动切割:利用fs.stat获取文件大小,当文件大小达到设定阈值时,重命名当前日志文件,并创建新的日志文件继续记录。可以使用日期或序号等方式命名新的日志文件以方便管理。

关键代码示例

const fs = require('fs');
const path = require('path');
const logDir = 'logs';
const logFile = 'app.log';
const maxSize = 1024 * 1024; // 1MB

// 创建日志目录
if (!fs.existsSync(logDir)) {
    fs.mkdirSync(logDir);
}

const logLevels = {
    DEBUG: 'DEBUG',
    INFO: 'INFO',
    WARN: 'WARN',
    ERROR: 'ERROR'
};

function getTimestamp() {
    return new Date().toISOString();
}

function writeLog(level, message) {
    const timestamp = getTimestamp();
    const logMessage = `${timestamp} [${level}] ${message}\n`;
    const logFilePath = path.join(logDir, logFile);

    fs.stat(logFilePath, (err, stats) => {
        if (err && err.code === 'ENOENT') {
            // 文件不存在则创建
            fs.writeFileSync(logFilePath, logMessage);
        } else if (stats.size >= maxSize) {
            // 滚动切割
            const newLogFileName = `app_${Date.now()}.log`;
            const newLogFilePath = path.join(logDir, newLogFileName);
            fs.renameSync(logFilePath, newLogFilePath);
            fs.writeFileSync(logFilePath, logMessage);
        } else {
            // 追加写入
            fs.appendFileSync(logFilePath, logMessage);
        }
    });
}

function debug(message) {
    writeLog(logLevels.DEBUG, message);
}

function info(message) {
    writeLog(logLevels.INFO, message);
}

function warn(message) {
    writeLog(logLevels.WARN, message);
}

function error(message) {
    writeLog(logLevels.ERROR, message);
}

// 测试
debug('This is a debug log');
info('This is an info log');
warn('This is a warn log');
error('This is an error log');