85 lines
2.6 KiB
TypeScript
85 lines
2.6 KiB
TypeScript
// import * as Sentry from "@sentry/nextjs";
|
|
|
|
// 获取Sentry logger实例
|
|
// const { logger } = Sentry;
|
|
|
|
// 导出logger以便在其他文件中使用
|
|
// export { logger };
|
|
|
|
// Fallback logger when Sentry is disabled
|
|
const logger = {
|
|
trace: (message: string, context?: Record<string, any>) => console.log(`[TRACE] ${message}`, context),
|
|
debug: (message: string, context?: Record<string, any>) => console.log(`[DEBUG] ${message}`, context),
|
|
info: (message: string, context?: Record<string, any>) => console.log(`[INFO] ${message}`, context),
|
|
warn: (message: string, context?: Record<string, any>) => console.warn(`[WARN] ${message}`, context),
|
|
error: (message: string, context?: Record<string, any>) => console.error(`[ERROR] ${message}`, context),
|
|
fatal: (message: string, context?: Record<string, any>) => console.error(`[FATAL] ${message}`, context),
|
|
fmt: (strings: TemplateStringsArray, ...values: any[]) => {
|
|
let result = strings[0];
|
|
for (let i = 0; i < values.length; i++) {
|
|
result += values[i] + strings[i + 1];
|
|
}
|
|
return result;
|
|
}
|
|
};
|
|
|
|
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
|
|
});
|
|
}; |