/
h0tnanny
/
IotPlatform
Обзор
Документация
Войти
/
h0tnanny
/
IotPlatform
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
src/services/LogService.ts
89 строк
3 KB
h0tnanny
Добавление функциональных требований
08 фев 2026, 23:02
08 фев 2026, 23:02
a8a23c5
Код
Авторство
О чём код?
import { LogRepository, LogLevel, LogCategory, LogFilters } from '../repositories/LogRepository'; /** * Сервис логирования — записывает критически важную информацию в БД. * Синглтон: используется через LogService.getInstance(). */ export class LogService { private static instance: LogService; private readonly repo: LogRepository; private constructor() { this.repo = new LogRepository(); } static getInstance(): LogService { if (!LogService.instance) { LogService.instance = new LogService(); } return LogService.instance; } /** * Записать лог в БД */ async log( level: LogLevel, category: LogCategory, message: string, options?: { details?: unknown; userId?: string; resourceId?: string; resourceType?: string; } ): Promise<void> { try { await this.repo.create({ level, category, message, details: options?.details, userId: options?.userId, resourceId: options?.resourceId, resourceType: options?.resourceType, }); } catch (error) { // Fallback: если БД недоступна, пишем в консоль console.error('[LogService] Ошибка записи лога в БД:', error); console.log(`[LogService][${level}][${category}] ${message}`); } } async info(category: LogCategory, message: string, options?: { details?: unknown; userId?: string; resourceId?: string; resourceType?: string }): Promise<void> { return this.log('info', category, message, options); } async warning(category: LogCategory, message: string, options?: { details?: unknown; userId?: string; resourceId?: string; resourceType?: string }): Promise<void> { return this.log('warning', category, message, options); } async error(category: LogCategory, message: string, options?: { details?: unknown; userId?: string; resourceId?: string; resourceType?: string }): Promise<void> { return this.log('error', category, message, options); } async critical(category: LogCategory, message: string, options?: { details?: unknown; userId?: string; resourceId?: string; resourceType?: string }): Promise<void> { return this.log('critical', category, message, options); } /** * Получить логи с фильтрацией и пагинацией */ async getLogs(filters: LogFilters) { return this.repo.findAll(filters); } /** * Получить статистику логов */ async getStats() { return this.repo.getStats(); } /** * Удалить логи старше N дней */ async cleanup(retentionDays: number = 30): Promise<number> { return this.repo.deleteOlderThan(retentionDays); } }