UNPKG

@isaac-platform/isaac-integration-sdk

Version:

A Typescript SDK for integrating with ISAAC

113 lines (112 loc) 5.45 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import isaacConnection from "../controller/isaac.js"; /** * Logger module for creating and managing log entries */ export class Logger { /** * Creates a new Logger instance */ constructor() { /** * Creates a new log entry * @param key - The key of the log entry. This is used to identify the log entry in the UI. * @param message - The log message. This is the actual content of the log entry. * @param level - The severity level of the log. Must be one of "debug", "info", "warn", "error", or "critical". Default is "info". * @param tags - An array of tags to apply to the log entry. * @returns The created log entry. */ this.createLogEntry = (key, message, level, tags = []) => { return { createdByType: "subsystem", subsystemExternalId: isaacConnection.subsystemID, key: key, severity: level, tags: tags, value: message, }; }; /** * Saves a log entry using the configured connection * @param logEntry - The log entry to save * @returns Promise resolving when the log is saved */ this.sendLogEntry = (logEntry) => __awaiter(this, void 0, void 0, function* () { try { return yield isaacConnection.postRequest('logs', logEntry, { headers: { 'Content-Type': 'application/json', } }); } catch (error) { console.error('ISAAC SDK: Failed to send log entry:', error); throw error; } }); /** * Creates and immediately sends a log entry to ISAAC * @param key - The key of the log entry. This is used to identify the log entry in the UI. * @param message - The log message. This is the actual content of the log entry. * @param level - The severity level of the log. Must be one of "debug", "info", "warn", "error", or "critical". Default is "info". * @param tags - An array of tags to apply to the log entry. */ this.log = (key, message, level = 'info', tags = []) => { const logEntry = this.createLogEntry(key, message, level, tags); this.sendLogEntry(logEntry); }; /** * Convenience method for "debug" level logs. * @param key - The key of the log entry. Thi is used to identify the log entry in the UI. * @param message - The log message. This is the actual content of the log entry. * @param tags - An optional string array of tags to apply to the log entry. */ this.debug = (key, message, tags = []) => { this.log(key, message, 'debug', tags); }; /** * Convenience method for "info" level logs. * @param key - The key of the log entry. Thi is used to identify the log entry in the UI. * @param message - The log message. This is the actual content of the log entry. * @param tags - An optional string array of tags to apply to the log entry. */ this.info = (key, message, tags = []) => { this.log(key, message, 'info', tags); }; /** * Convenience method for "warn" level logs. * @param key - The key of the log entry. Thi is used to identify the log entry in the UI. * @param message - The log message. This is the actual content of the log entry. * @param tags - An optional string array of tags to apply to the log entry. */ this.warn = (key, message, tags) => { this.log(key, message, 'warn', tags || []); }; /** * Convenience method for "error" level logs. * @param key - The key of the log entry. Thi is used to identify the log entry in the UI. * @param message - The log message. This is the actual content of the log entry. * @param tags - An optional string array of tags to apply to the log entry. */ this.error = (key, message, tags) => { this.log(key, message, 'error', tags || []); }; /** * Convenience method for "critical" level logs. * @param key - The key of the log entry. Thi is used to identify the log entry in the UI. * @param message - The log message. This is the actual content of the log entry. * @param tags - An optional string array of tags to apply to the log entry. */ this.critical = (key, message, tags) => { this.log(key, message, 'critical', tags || []); }; } }