初始化环境文件
This commit is contained in:
58
server.js
58
server.js
@@ -13,6 +13,64 @@ const Redis = require('ioredis');
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const winston = require('winston');
|
||||
require('winston-daily-rotate-file');
|
||||
const util = require('util');
|
||||
|
||||
const LOGS_DIR = path.join(__dirname, 'logs');
|
||||
if (!fs.existsSync(LOGS_DIR)) {
|
||||
fs.mkdirSync(LOGS_DIR, { recursive: true });
|
||||
}
|
||||
|
||||
const logger = winston.createLogger({
|
||||
level: 'info',
|
||||
format: winston.format.combine(
|
||||
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
|
||||
winston.format.errors({ stack: true }),
|
||||
winston.format.splat(),
|
||||
winston.format.printf(({ timestamp, level, message, stack, ...meta }) => {
|
||||
return `[${timestamp}] ${level.toUpperCase()}: ${stack || message} ${Object.keys(meta).length ? JSON.stringify(meta) : ''}`;
|
||||
})
|
||||
),
|
||||
transports: [
|
||||
new winston.transports.Console({
|
||||
format: winston.format.combine(
|
||||
winston.format.colorize(),
|
||||
winston.format.printf(({ timestamp, level, message, stack }) => {
|
||||
return `[${timestamp}] ${level}: ${stack || message}`;
|
||||
})
|
||||
)
|
||||
}),
|
||||
new winston.transports.DailyRotateFile({
|
||||
filename: path.join(LOGS_DIR, 'application-%DATE%.log'),
|
||||
datePattern: 'YYYY-MM-DD',
|
||||
zippedArchive: true,
|
||||
maxSize: '20m',
|
||||
maxFiles: '14d'
|
||||
}),
|
||||
new winston.transports.DailyRotateFile({
|
||||
filename: path.join(LOGS_DIR, 'error-%DATE%.log'),
|
||||
datePattern: 'YYYY-MM-DD',
|
||||
level: 'error',
|
||||
zippedArchive: true,
|
||||
maxSize: '20m',
|
||||
maxFiles: '30d'
|
||||
})
|
||||
]
|
||||
});
|
||||
|
||||
console.log = function(...args) { logger.info(util.format(...args)); };
|
||||
console.info = function(...args) { logger.info(util.format(...args)); };
|
||||
console.warn = function(...args) { logger.warn(util.format(...args)); };
|
||||
console.error = function(...args) { logger.error(util.format(...args)); };
|
||||
|
||||
process.on('uncaughtException', (err) => {
|
||||
logger.error(`Uncaught Exception: ${err.message}`, err);
|
||||
});
|
||||
process.on('unhandledRejection', (reason, promise) => {
|
||||
logger.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
||||
});
|
||||
|
||||
const CACHE_DIR = path.join(__dirname, 'cache');
|
||||
if (!fs.existsSync(CACHE_DIR)) {
|
||||
fs.mkdirSync(CACHE_DIR, { recursive: true });
|
||||
|
||||
Reference in New Issue
Block a user