UNPKG

logsilo

Version:

A lightweight logging utility for sending logs to a configured endpoint.

158 lines (156 loc) 7.94 kB
"use strict"; /** Copyright (c) MuteCode **/ 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Silo = void 0; exports.create = create; const chalk_1 = __importDefault(require("chalk")); const siloConfig_1 = require("./siloConfig"); class Silo { constructor(apiKey, apiUrl, application) { this.siloLogo = ` :+#@@@@#=. .-*@@@@%+: -*@@@@@@@@@@@%*- :+%@@@@@@@@@@@#=. :+#@@@@@@@@@@@@@@@@@@#=*@@@@@@@@@@@@@@@@@@%+: -+%@@@@@@@@@@@#%@@@@@@@@@@@@@@@@@@@%#@@@@@@@@@@@@*-. .=#@@@@@@@@@@@@*- .=@@@@@@@@@@@@@@@*. :+%@@@@@@@@@@@%+: .-*%@@@@@@@@@@@#=: -+%@@@@@@@@@@@@@@@@@%*- .-*%@@@@@@@@@@@#=. +%@@@@@@@@@@@%+: -#@@@@@@@@@@@@#%@@@@@@@@@@@%= .=#@@@@@@@@@@@%+ @@@@@@@@@@#=. *@@@@@@@@@%+: .=#@@@@@@@@@@ -*@@@@@@@@@@. @@@@@@@*- +@@@@@@%=. -*@@@@@@% :+@@@@@@@. @@@@@@@ +@@@@@@#+++++++++++++*@@@@@@% %@@@@@@. @@@@@@@. +@@@@@@@@@@@@@@@@@@@@@@@@@@@@ %@@@@@@. @@@@@@@. +@@@@@@*:::::::::::::-@@@@@@@ %@@@@@@. @@@@@@@. +@@@@@@+ .@@@@@@@ %@@@@@@. @@@@@@@. +@@@@@@@@@@@@@@@@@@@@@@@@@@@% %@@@@@@. @@@@@@@. +@@@@@@%#############%@@@@@@% %@@@@@@. @@@@@@@. +@@@@@@= .@@@@@@% %@@@@@@. @@@@@@@. +@@@@@@#+++++++++++++*@@@@@@@ %@@@@@@. @@@@@@@. +@@@@@@@@@@@@@@@@@@@@@@@@@@@@ %@@@@@@. @@@@@@@. +@@@@@@+.............-@@@@@@@ %@@@@@@. @@@@@@@@*-. +@@@@@@@#=. .=*@@@@@@@@ :+%@@@@@@@. @@@@@@@@@@@#+. +@@@@@@@@@@%*- :+%@@@@@@@@@@@ .=*@@@@@@@@@@@. .=#@@@@@@@@@@@%*- -*@@@@@@@@@@@@@@@@@@@@@@@#=. :=#@@@@@@@@@@@#+: -*@@@@@@@@@@@@#=. :+%@@@@@@@@@@@@@@@@*- -*@@@@@@@@@@@@#=. :+%@@@@@@@@@@@%+: -+@@@@@@@@@@@@@@@#- .=#@@@@@@@@@@@%+- .-*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#=. :+%@@@@@@@@@@@@@@@@%+-=#@@@@@@@@@@@@@@@@@*- .=#@@@@@@@@@@#=. -*@@@@@@@@@@%+: :+#@@#+: .=*@@%*-. :+ -++++- =++++. +++++ .+. .+ :++++- -@. +% #+ %+ @: :@. :@. =@. ** -@. +% #+ %=.**. @**** :@. :@. =@ ** -@. +% #+ %= @: -@ :@. :@. =@ ** :****- -*++*- +*+-*. ***** .*. .**+*+ :**+*= `; /** * The apiKey of the Silo server where to send the logs */ this.apiKey = ''; /** * The URL of the Silo server where to send the logs */ this.apiUrl = ''; /** * Application name */ this.application = ''; this.apiKey = apiKey; this.apiUrl = apiUrl; this.application = application; } showSiloArt() { console.log(` ${chalk_1.default.green(this.siloLogo)} `); } /** * Logs the config details */ showConfig() { const siloConfig = siloConfig_1.SiloConfig.getInstance(); const configData = siloConfig.getConfig(); console.log(` ${chalk_1.default.green('======================= CONFIG START =======================')} ${chalk_1.default.bgRedBright('apiKey:')} ${configData.apiKey} ${chalk_1.default.bgRed('apiUrl:')} ${configData.apiUrl} ${chalk_1.default.bgBlue('application:')} ${configData.application} ${chalk_1.default.green('======================== CONFIG END ========================')} `); } /** * Logs a formatted string with the log details */ console(log) { console.log(` ${chalk_1.default.green('======================= LOG START =======================')} ${chalk_1.default.bgRed('service:')} ${log.service} ${chalk_1.default.bgBlue('shortDescription:')} ${log.shortDescription} ${chalk_1.default.bgCyan('detailedDescription:')} ${log.detailedDescription} ${chalk_1.default.bgMagenta('level:')} ${log.level} ${chalk_1.default.bgRedBright('user:')} ${log.user} ${chalk_1.default.bgCyanBright('path:')} ${log.path} ${chalk_1.default.bgYellowBright('statusCode:')} ${log.statusCode} ${chalk_1.default.bgYellow('timestamp:')} ${log.timestamp.toISOString()} ${chalk_1.default.green('======================== LOG END ========================')} `); } /** * Sends the log details to the specified apiUrl */ send(log) { return __awaiter(this, void 0, void 0, function* () { const logEntry = { apiUrl: this.apiUrl, application: this.application, service: log.service, shortDescription: log.shortDescription, detailedDescription: log.detailedDescription, level: log.level, user: log.user, path: log.path, statusCode: log.statusCode, timestamp: log.timestamp, }; const headers = { 'Content-Type': 'application/json', }; // In case of apiKey authentication use only your server's secret apiKey as header 'x-api-key' if (this.apiKey) { headers['x-api-key'] = this.apiKey; } try { const response = yield fetch(this.apiUrl, { method: 'POST', headers, body: JSON.stringify(logEntry), }); return response; } catch (error) { return error; } }); } } exports.Silo = Silo; /** * Creates a Silo instance with configuration */ function create(config) { const siloConfig = siloConfig_1.SiloConfig.getInstance(); siloConfig.setConfig(config.apiKey, config.apiUrl, config.application); const configData = siloConfig.getConfig(); return new Silo(configData.apiKey, configData.apiUrl, configData.application); }