log4js官网
npm i log4js
const log4js = require("log4js");
const path = require("path");
Object.defineProperties;
log4js.configure({appenders: {aaa: {type: "dateFile", //设置为这种类型,日志文件会分割filename: path.resolve(__dirname, "logs", "sql", "sql.log"), //文件名maxLogSize: 1024, //一个日志文件的大小,超过就备份新建keepFileExt: true, //新建的文件保存文件后缀numBackups: Number.MAX_SAFE_INTEGER - 1, //同时保存多少个文件layout: {type: "pattern", //自定义输出的内容pattern: "yyyy-MM-dd %m",},},default111: {type: "file",filename: path.resolve(__dirname, "logs", "default.log"),},},categories: {// log4js.getLogger("sql")这里相对应,要使用那个类别,sql: {appenders: ["aaa"], //要使用那个输出源,和上面的appenders对应level: "all", //这个类别啥等级才需要记录},default: {//默认类别,必须要有appenders: ["default111"],level: "all",},},
});
process.on("exit", () => {//必须有,当程序意外关闭的时候,必须先把日志写完log4js.shutdown();
});
const logger = log4js.getLogger("sql");
setInterval(() => {logger.info("你好,hi");
}, 100);
很简陋,就一些简单配置,其余的看官网