UNPKG

@bitdiver/result-rest-api

Version:
94 lines (81 loc) 2.14 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.getLogAdapter = getLogAdapter; const LEVEL_DEBUG = exports.LEVEL_DEBUG = 'debug'; const LEVEL_INFO = exports.LEVEL_INFO = 'info'; const LEVEL_WARNING = exports.LEVEL_WARNING = 'warning'; const LEVEL_ERROR = exports.LEVEL_ERROR = 'error'; const LEVEL_FATAL = exports.LEVEL_FATAL = 'fatal'; function getTime() { function fill(val) { if (val < 10) { return '0' + val; } return val; } const date = new Date(Date.now()); const dateStr = `${date.getFullYear()}-${fill(date.getMonth() + 1)}-${fill(date.getDate())}`; const timeStr = `${fill(date.getHours())}:${fill(date.getMinutes())}:${fill(date.getSeconds())}`; return dateStr + ' ' + timeStr; } /** * Implements a default logAdapter * @class */ class LogAdapter { constructor() { // default is to write to console this.writeConsole = true; this.storeLog = false; // Stores all the messages this.entries = { debug: [], info: [], warning: [], error: [], fatal: [] }; } /** * This method will be called each time the runner * starts a new run */ reset() {} log(logMessage, level) { if (this.writeConsole) { // eslint-disable-next-line no-console console.log(`${getTime()}: ${level.toUpperCase()}: ${logMessage}`); } let logEntry = logMessage; if (typeof logMessage === 'string') { logEntry = { message: logMessage }; } logEntry.created = getTime(); logEntry.level = level; if (this.storeLog) { this.entries[level].push(logMessage); } } logDebug(logMessage) { this.log(logMessage, LEVEL_DEBUG); } logInfo(logMessage) { this.log(logMessage, LEVEL_INFO); } logWarning(logMessage) { this.log(logMessage, LEVEL_WARNING); } logError(logMessage) { this.log(logMessage, LEVEL_ERROR); } logFatal(logMessage) { this.log(logMessage, LEVEL_FATAL); } } exports.LogAdapter = LogAdapter; // Stores the logger instance const logAdapter = new LogAdapter(); function getLogAdapter() { return logAdapter; }