112 lines
2.6 KiB
TypeScript
112 lines
2.6 KiB
TypeScript
/**
|
|
* アプリケーション全体で使用するロギングユーティリティ
|
|
* 開発環境と本番環境で適切にログ出力を制御
|
|
*/
|
|
|
|
import Constants from 'expo-constants';
|
|
|
|
const isDevelopment = __DEV__;
|
|
|
|
/**
|
|
* ログレベル
|
|
*/
|
|
export enum LogLevel {
|
|
DEBUG = 'DEBUG',
|
|
INFO = 'INFO',
|
|
WARN = 'WARN',
|
|
ERROR = 'ERROR',
|
|
}
|
|
|
|
/**
|
|
* ログフォーマッター
|
|
*/
|
|
const formatLog = (level: LogLevel, message: string, ...args: any[]): string => {
|
|
const timestamp = new Date().toISOString();
|
|
return `[${timestamp}] [${level}] ${message}`;
|
|
};
|
|
|
|
/**
|
|
* ロガークラス
|
|
*/
|
|
class Logger {
|
|
/**
|
|
* デバッグログ(開発環境のみ)
|
|
*/
|
|
debug(message: string, ...args: any[]): void {
|
|
if (isDevelopment) {
|
|
console.log(formatLog(LogLevel.DEBUG, message), ...args);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 情報ログ
|
|
*/
|
|
info(message: string, ...args: any[]): void {
|
|
if (isDevelopment) {
|
|
console.info(formatLog(LogLevel.INFO, message), ...args);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 警告ログ
|
|
*/
|
|
warn(message: string, ...args: any[]): void {
|
|
console.warn(formatLog(LogLevel.WARN, message), ...args);
|
|
}
|
|
|
|
/**
|
|
* エラーログ(常に出力)
|
|
*/
|
|
error(message: string, error?: Error | any, ...args: any[]): void {
|
|
console.error(formatLog(LogLevel.ERROR, message), error, ...args);
|
|
|
|
// 本番環境ではエラートラッキングサービスに送信する処理を追加可能
|
|
// 例: Sentry.captureException(error);
|
|
}
|
|
|
|
/**
|
|
* ネットワークリクエストログ
|
|
*/
|
|
network(method: string, url: string, status?: number, duration?: number): void {
|
|
if (isDevelopment) {
|
|
const durationText = duration ? ` (${duration}ms)` : '';
|
|
const statusText = status ? ` - ${status}` : '';
|
|
this.debug(`[NETWORK] ${method} ${url}${statusText}${durationText}`);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* パフォーマンスログ
|
|
*/
|
|
performance(label: string, duration: number): void {
|
|
if (isDevelopment) {
|
|
this.debug(`[PERF] ${label}: ${duration}ms`);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* グローバルロガーインスタンス
|
|
*/
|
|
export const logger = new Logger();
|
|
|
|
/**
|
|
* パフォーマンス計測ヘルパー
|
|
*/
|
|
export const measurePerformance = async <T>(
|
|
label: string,
|
|
fn: () => T | Promise<T>
|
|
): Promise<T> => {
|
|
const start = Date.now();
|
|
try {
|
|
const result = await fn();
|
|
const duration = Date.now() - start;
|
|
logger.performance(label, duration);
|
|
return result;
|
|
} catch (error) {
|
|
const duration = Date.now() - start;
|
|
logger.error(`${label} failed after ${duration}ms`, error);
|
|
throw error;
|
|
}
|
|
};
|