geoapify-mcp-server
Version:
Geoapify API MCP Server for location-based services - 一键部署的地理位置服务
66 lines (56 loc) • 1.58 kB
JavaScript
const winston = require('winston');
const { combine, timestamp, errors, json, printf, colorize } = winston.format;
// 自定义日志格式
const customFormat = printf(({ level, message, timestamp, ...meta }) => {
let log = `${timestamp} [${level}]: ${message}`;
if (Object.keys(meta).length > 0) {
log += ` ${JSON.stringify(meta)}`;
}
return log;
});
// 创建logger实例
const logger = winston.createLogger({
level: process.env.LOG_LEVEL || 'info',
format: combine(
timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
errors({ stack: true }),
json()
),
defaultMeta: { service: 'geoapify-mcp-server' },
transports: [
// 错误日志文件
new winston.transports.File({
filename: 'logs/error.log',
level: 'error',
maxsize: 5242880, // 5MB
maxFiles: 5,
}),
// 所有日志文件
new winston.transports.File({
filename: 'logs/combined.log',
maxsize: 5242880, // 5MB
maxFiles: 5,
}),
],
});
// 开发环境添加控制台输出
if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({
format: combine(
colorize(),
timestamp({ format: 'HH:mm:ss' }),
customFormat
)
}));
}
// 生产环境错误处理
if (process.env.NODE_ENV === 'production') {
logger.exceptions.handle(
new winston.transports.File({ filename: 'logs/exceptions.log' })
);
logger.rejections.handle(
new winston.transports.File({ filename: 'logs/rejections.log' })
);
}
module.exports = { logger };
module.exports.default = logger;