mosaicmap/lib/logger.ts
2025-08-19 12:17:11 +08:00

66 lines
1.7 KiB
TypeScript

import * as Sentry from "@sentry/nextjs";
// 获取Sentry logger实例
const { logger } = Sentry;
// 导出logger以便在其他文件中使用
export { logger };
// 示例用法的辅助函数
export const logUserAction = (action: string, userId?: string, metadata?: Record<string, any>) => {
logger.info(logger.fmt`User action: ${action}`, {
userId,
action,
timestamp: new Date().toISOString(),
...metadata
});
};
export const logPerformanceMetric = (metric: string, value: number, unit: string) => {
logger.debug(logger.fmt`Performance metric: ${metric} = ${value}${unit}`, {
metric,
value,
unit,
timestamp: new Date().toISOString()
});
};
export const logApiCall = (endpoint: string, method: string, status: number, duration?: number) => {
const level = status >= 400 ? 'error' : status >= 300 ? 'warn' : 'info';
logger[level](logger.fmt`API ${method} ${endpoint} - ${status}`, {
endpoint,
method,
status,
duration,
timestamp: new Date().toISOString()
});
};
export const logError = (error: Error, context?: Record<string, any>) => {
logger.error(logger.fmt`Error occurred: ${error.message}`, {
error: error.name,
message: error.message,
stack: error.stack,
...context
});
};
// WebGL相关的特殊logger
export const logWebGLError = (operation: string, error: string, context?: Record<string, any>) => {
logger.error(logger.fmt`WebGL error during ${operation}: ${error}`, {
operation,
error,
webglContext: true,
...context
});
};
// Map相关的logger
export const logMapEvent = (event: string, details?: Record<string, any>) => {
logger.debug(logger.fmt`Map event: ${event}`, {
event,
component: 'map',
...details
});
};