73 lines
1.7 KiB
JavaScript
73 lines
1.7 KiB
JavaScript
const winston = require('winston');
|
|
const path = require('path');
|
|
|
|
// 创建日志目录
|
|
const logDir = path.join(__dirname, '../logs');
|
|
|
|
// 日志格式配置
|
|
const logFormat = winston.format.combine(
|
|
winston.format.timestamp({
|
|
format: 'YYYY-MM-DD HH:mm:ss'
|
|
}),
|
|
winston.format.errors({ stack: true }),
|
|
winston.format.json()
|
|
);
|
|
|
|
// 控制台日志格式
|
|
const consoleFormat = winston.format.combine(
|
|
winston.format.colorize(),
|
|
winston.format.timestamp({
|
|
format: 'YYYY-MM-DD HH:mm:ss'
|
|
}),
|
|
winston.format.printf(({ timestamp, level, message, ...meta }) => {
|
|
return `${timestamp} [${level}]: ${message} ${Object.keys(meta).length ? JSON.stringify(meta, null, 2) : ''}`;
|
|
})
|
|
);
|
|
|
|
// 创建logger实例
|
|
const logger = winston.createLogger({
|
|
level: process.env.LOG_LEVEL || 'info',
|
|
format: logFormat,
|
|
defaultMeta: { service: 'integrated-system' },
|
|
transports: [
|
|
// 错误日志文件
|
|
new winston.transports.File({
|
|
filename: path.join(logDir, 'error.log'),
|
|
level: 'error',
|
|
maxsize: 5242880, // 5MB
|
|
maxFiles: 5
|
|
}),
|
|
// 所有日志文件
|
|
new winston.transports.File({
|
|
filename: path.join(logDir, 'combined.log'),
|
|
maxsize: 5242880, // 5MB
|
|
maxFiles: 5
|
|
})
|
|
]
|
|
});
|
|
|
|
// 开发环境添加控制台输出
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
logger.add(new winston.transports.Console({
|
|
format: consoleFormat
|
|
}));
|
|
}
|
|
|
|
// 审计日志记录器
|
|
const auditLogger = winston.createLogger({
|
|
level: 'info',
|
|
format: logFormat,
|
|
defaultMeta: { service: 'audit' },
|
|
transports: [
|
|
new winston.transports.File({
|
|
filename: path.join(logDir, 'audit.log'),
|
|
maxsize: 5242880, // 5MB
|
|
maxFiles: 10
|
|
})
|
|
]
|
|
});
|
|
|
|
module.exports = {
|
|
logger,
|
|
auditLogger
|
|
}; |