UNPKG

@homecheck/logger

Version:

A simple logger for Web, Node, Capacitor apps.

80 lines (79 loc) 2.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CapacitorStorage = void 0; const preferences_1 = require("@capacitor/preferences"); class CapacitorStorage { constructor() { this.storageKey = 'logger-queue'; this.initialized = false; } async init() { this.initialized = true; } async addLog(log) { if (!this.initialized) await this.init(); // 현재 저장된 로그 가져오기 const logs = await this.getAllLogs(); // 로그 데이터에 ID와 생성 시간 추가 const logWithId = { ...log, id: this.generateUUID(), createdAt: Date.now() }; // 저장 logs.push(logWithId); await preferences_1.Preferences.set({ key: this.storageKey, value: JSON.stringify(logs) }); } async getLogs(limit = 50) { if (!this.initialized) await this.init(); const logs = await this.getAllLogs(); return logs.slice(0, limit); } async removeLogs(ids) { if (!this.initialized) await this.init(); // 현재 저장된 로그 가져오기 let logs = await this.getAllLogs(); // 지정된 ID를 제외한 로그만 유지 logs = logs.filter(log => !ids.includes(log.id)); // 저장 await preferences_1.Preferences.set({ key: this.storageKey, value: JSON.stringify(logs) }); } async getCount() { if (!this.initialized) await this.init(); const logs = await this.getAllLogs(); return logs.length; } async clear() { if (!this.initialized) { await this.init(); } await preferences_1.Preferences.remove({ key: this.storageKey }); } // 모든 로그 가져오기 async getAllLogs() { const result = await preferences_1.Preferences.get({ key: this.storageKey }); if (!result.value) { return []; } return JSON.parse(result.value); } // UUID 생성 generateUUID() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { const r = Math.random() * 16 | 0; const v = c === 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); } } exports.CapacitorStorage = CapacitorStorage;