UNPKG

@ar.io/sdk

Version:

[![codecov](https://codecov.io/gh/ar-io/ar-io-sdk/graph/badge.svg?token=7dXKcT7dJy)](https://codecov.io/gh/ar-io/ar-io-sdk)

84 lines (83 loc) 2.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Logger = void 0; /** * Copyright (C) 2022-2024 Permanent Data Solutions, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ const version_js_1 = require("../version.js"); class Logger { level = 'info'; levels = { debug: 0, info: 1, warn: 2, error: 3, none: 999, }; static default = new Logger(); constructor({ level = 'info', } = {}) { this.level = level; } formatMessage(level, message, ...args) { const timestamp = new Date().toISOString(); const meta = { timestamp, level, message, name: 'ar-io-sdk', version: version_js_1.version, }; if (args.length > 0) { return JSON.stringify({ ...meta, args }); } return JSON.stringify(meta); } log(level, message, ...args) { if (this.levels[level] < this.levels[this.level]) { return; } const formattedMessage = this.formatMessage(level, message, ...args); switch (level) { case 'debug': console.debug(formattedMessage); break; case 'info': console.info(formattedMessage); break; case 'warn': console.warn(formattedMessage); break; case 'error': console.error(formattedMessage); break; } } info(message, ...args) { this.log('info', message, ...args); } warn(message, ...args) { this.log('warn', message, ...args); } error(message, ...args) { this.log('error', message, ...args); } debug(message, ...args) { this.log('debug', message, ...args); } setLogLevel(level) { this.level = level; } } exports.Logger = Logger;