UNPKG

ng2-logger

Version:

isomorphic logger for browser/server in typescript

532 lines (524 loc) 16.3 kB
import { Helpers } from 'tnp-core/browser'; var Level; (function (Level) { Level[Level["DATA"] = 0] = "DATA"; Level[Level["INFO"] = 1] = "INFO"; Level[Level["WARN"] = 2] = "WARN"; Level[Level["ERROR"] = 3] = "ERROR"; Level[Level["SUCCESS"] = 4] = "SUCCESS"; Level[Level["TASK_STARTED"] = 5] = "TASK_STARTED"; Level[Level["TASK_DONE"] = 6] = "TASK_DONE"; Level[Level["__NOTHING"] = 7] = "__NOTHING"; })(Level || (Level = {})); const LevelKey = { [Level.DATA]: 'log', [Level.INFO]: 'info', [Level.WARN]: 'warn', [Level.ERROR]: 'error', [Level.SUCCESS]: 'success', [Level.TASK_STARTED]: 'taskstarted', [Level.TASK_DONE]: 'taskdone', [Level.__NOTHING]: '', }; const LevelOrder = [ LevelKey[Level.DATA], LevelKey[Level.TASK_STARTED], LevelKey[Level.TASK_DONE], LevelKey[Level.INFO], LevelKey[Level.SUCCESS], LevelKey[Level.WARN], LevelKey[Level.ERROR], ]; ; ({}); // @--end-of-file-for-module=ng2-logger lib/level.ts /* */ /* */ /* */ /* */ class Display { static msg(message, params, moduleName, moduleColor, level, moduleWidth, isProductionMode) { if (isProductionMode) { return; } ; let color = 'gray'; if (level === Level.INFO) { color = 'deepskyblue'; } if (level === Level.ERROR) { color = 'red'; } if (level === Level.WARN) { color = 'orange'; } if (moduleWidth) { const diff = moduleWidth - moduleName.length; if (diff > 0) { for (let i = 0; i < diff; i++) { moduleName += ' '; } } } if (Helpers.isBrowser /* */ /* */ ) { // @ts-ignore const isEdgeOrIe8orAbove = (document['documentMode'] || /Edge/.test(navigator.userAgent)); if (isEdgeOrIe8orAbove) { if (typeof message === 'string') { let a1 = '[[ ' + moduleName + ' ]] ' + message + ' '; params.unshift(a1); } else { let a1 = '[[ ' + moduleName + ']] '; params.push(message); params.unshift(a1); } if (level === Level.INFO) { console.info.apply(console, params); } else if (level === Level.ERROR) { console.error.apply(console, params); } else if (level === Level.WARN) { console.warn.apply(console, params); } else { console.log.apply(console, params); } } else { if (typeof message === 'string') { let a1 = '%c ' + moduleName + ' %c ' + message + ' '; let a2 = 'background: ' + moduleColor + ';color:white; border: 1px solid ' + moduleColor + '; '; let a3 = 'border: 1px solid ' + color + '; '; params.unshift(a3); params.unshift(a2); params.unshift(a1); } else { let a1 = '%c ' + moduleName + ' '; let a2 = 'background: ' + moduleColor + ';color:white; border: 1px solid ' + color + '; '; params.push(message); params.unshift(a2); params.unshift(a1); } console.log.apply(console, params); } } /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ } } ; ({}); // @--end-of-file-for-module=ng2-logger lib/display.ts class Logger { setLevel(l) { this._level = l; return this; } get isProductionMode() { return !this.developmentMode; } setProductionMode(productionMode) { this.developmentMode = !productionMode; return this; } mute() { this.isMuted = true; return this; } constructor(name, color, developmentMode, allowed, isMuted, fixedWidth) { this.name = name; this.color = color; this.developmentMode = developmentMode; this.allowed = allowed; this.isMuted = isMuted; this.fixedWidth = fixedWidth; /** * @see data */ this.d = (name, ...data) => this._data(name, data); /** * @see error */ this.er = (name, ...data) => this._error(name, data); /** * @see info */ this.i = (name, ...data) => this._info(name, data); /** * @see warn */ this.w = (name, ...data) => this._warn(name, data); /** * Logs message and data with the level=data * @param message The message * @param otherParams Additional parameters */ this.data = (message, ...otherParams) => { return this._data(message, otherParams); }; /** * Logs message and data with the level=error * @param message The message * @param otherParams Additional parameters */ this.error = (message, ...otherParams) => this._error(message, otherParams); /** * Logs message and data with the level=info * @param message The message * @param otherParams Additional parameters */ this.info = (message, ...otherParams) => this._info(message, otherParams); /** * Logs message and data with the level=success * @param message The message * @param otherParams Additional parameters */ this.success = (message, ...otherParams) => this._success(message, otherParams); /** * Logs message and data with the level=taskStarted * @param message The message * @param otherParams Additional parameters */ this.taskStarted = (message, ...otherParams) => this._taskStarted(message, otherParams); /** * Logs message and data with the level=taskDone * @param message The message * @param otherParams Additional parameters */ this.taskDone = (message, ...otherParams) => this._taskDone(message, otherParams); /** * Logs message and data with the level=warn * @param message The message * @param otherParams Additional parameters */ this.warn = (message, ...otherParams) => this._warn(message, otherParams); } onlyWhen(expression) { if (typeof expression === 'function') { this.isMuted = !expression(); } else if (typeof expression === 'boolean') { this.isMuted = !expression; } } _data(name, ...data) { if (this.isMuted) return this; if (this.allowed.length >= 1 && Helpers.contain(this.allowed, Level.__NOTHING) && !Helpers.contain(this.allowed, Level.DATA)) return this; if (this.allowed.length === 0 || Helpers.contain(this.allowed, Level.DATA)) { // @ts-ignore Display.msg.apply(void 0, [ name, ...data, this.name, this.color, Level.DATA, this.fixedWidth, this.isProductionMode, ]); } return this; } _error(name, ...data) { if (this.isMuted) return this; if (this.allowed.length >= 1 && Helpers.contain(this.allowed, Level.__NOTHING) && !Helpers.contain(this.allowed, Level.ERROR)) return this; if (this.allowed.length === 0 || Helpers.contain(this.allowed, Level.ERROR)) { // @ts-ignore Display.msg.apply(void 0, [ name, ...data, this.name, this.color, Level.ERROR, this.fixedWidth, this.isProductionMode, ]); } return this; } _info(name, ...data) { if (this.isMuted) return this; if (this.allowed.length >= 1 && Helpers.contain(this.allowed, Level.__NOTHING) && !Helpers.contain(this.allowed, Level.INFO)) return this; if (this.allowed.length === 0 || Helpers.contain(this.allowed, Level.INFO)) { // @ts-ignore Display.msg.apply(void 0, [ name, ...data, this.name, this.color, Level.INFO, this.fixedWidth, this.isProductionMode, ]); } return this; } _success(name, ...data) { if (this.isMuted) return this; if (this.allowed.length >= 1 && Helpers.contain(this.allowed, Level.__NOTHING) && !Helpers.contain(this.allowed, Level.SUCCESS)) return this; if (this.allowed.length === 0 || Helpers.contain(this.allowed, Level.SUCCESS)) { // @ts-ignore Display.msg.apply(void 0, [ name, ...data, this.name, this.color, Level.SUCCESS, this.fixedWidth, this.isProductionMode, ]); } return this; } _taskStarted(name, ...data) { if (this.isMuted) return this; if (this.allowed.length >= 1 && Helpers.contain(this.allowed, Level.__NOTHING) && !Helpers.contain(this.allowed, Level.TASK_STARTED)) return this; if (this.allowed.length === 0 || Helpers.contain(this.allowed, Level.TASK_STARTED)) { // @ts-ignore Display.msg.apply(void 0, [ name, ...data, this.name, this.color, Level.TASK_STARTED, this.fixedWidth, this.isProductionMode, ]); } return this; } _taskDone(name, ...data) { if (this.isMuted) return this; if (this.allowed.length >= 1 && Helpers.contain(this.allowed, Level.__NOTHING) && !Helpers.contain(this.allowed, Level.TASK_DONE)) return this; if (this.allowed.length === 0 || Helpers.contain(this.allowed, Level.TASK_DONE)) { // @ts-ignore Display.msg.apply(void 0, [ name, ...data, this.name, this.color, Level.TASK_DONE, this.fixedWidth, this.isProductionMode, ]); } return this; } _warn(name, ...data) { if (this.isMuted) return this; if (this.allowed.length >= 1 && Helpers.contain(this.allowed, Level.__NOTHING) && !Helpers.contain(this.allowed, Level.WARN)) return this; if (this.allowed.length === 0 || Helpers.contain(this.allowed, Level.WARN)) { // @ts-ignore Display.msg.apply(void 0, [ name, ...data, this.name, this.color, Level.WARN, this.fixedWidth, this.isProductionMode, ]); } return this; } } ; ({}); // @--end-of-file-for-module=ng2-logger lib/logger.ts /* */ /* */ /* */ /* */ /* */ class Log { constructor() { this._logOnly = false; this._logModules = false; this.isDevelopmentMode = true; this.modeIsSet = false; this.fixedWidth = 0; this.instances = {}; this.levels = []; this.modules = []; } // @ts-ignore static get instance() { // @ts-ignore if (!Log['_instance']) { // @ts-ignore Log['_instance'] = new Log(); } // @ts-ignore return Log['_instance']; } static { this.Logger = Logger; } static create(name, ...level) { return Log.instance.create(name, ...level); } static { this.consolelogfn = {}; } static disableLogs(level = Level.__NOTHING) { /* */ /* */ /* */ /* */ /* */ /* */ /* */ LevelOrder.reverse().find(a => { // @ts-ignore if (!this.consolelogfn[a]) { // @ts-ignore this.consolelogfn[a] = console[a]; } // @ts-ignore console[a] = () => { }; if (a === LevelKey[level]) { return true; } return false; }); } static enableLogs() { /* */ /* */ /* */ /* */ LevelOrder.forEach(a => { // @ts-ignore console[a] = this.consolelogfn[a]; }); } setProductionMode() { if (this.modeIsSet) { this.modeIsSet = false; throw '[ng2-logger] Production mode is already set'; } else { this.modeIsSet = true; setTimeout(() => { if (this.modeIsSet && console !== void 0 && console.clear !== void 0) { console.clear(); console.log = () => { }; console.error = () => { }; console.warn = () => { }; console.info = () => { }; } }); this.isDevelopmentMode = false; } } onlyModules(...modules) { if (this._logModules) { throw '[ng2-logger] You should use funcion onlyModules only once'; } if (!this._logModules) { this._logModules = true; } if (modules.length === 0) return; this.modules = modules; this.muteAllOtherModules(); } onlyLevel(...level) { if (this._logOnly) { throw '[ng2-logger] You should use funcion onlyLevel only once'; } if (!this._logOnly) { this._logOnly = true; } this.levels = Array.isArray(level) ? level : [level]; for (const logName in this.instances) { if (this.instances.hasOwnProperty(logName)) { const element = this.instances[logName]; element['allowed'] = this.levels; } } } create(name, ...level) { let i; if (Array.isArray(this.levels) && this.levels.length > 0) { level = this.levels; } if (this.instances[name] === void 0) { i = new (Log.Logger)(name, getRandomColor(), this.isDevelopmentMode, level, this.isMutedModule(name), this.levels.length > 0 ? this.fixedWidth : void 0); this.instances[name] = i; } else { i = this.instances[name]; } return i; } isMutedModule(moduleName) { if (this.modules.length == 0) return false; if (!Helpers.contain(this.modules, moduleName)) return true; return false; } muteAllOtherModules() { for (var moduleName in this.instances) { if (!Helpers.contain(this.modules, moduleName)) this.instances[moduleName].mute(); } } } function getRandomColor() { /* */ /* */ /* */ /* */ let letters = '012345'.split(''); let color = '#'; color += letters[Math.round(Math.random() * 5)]; letters = '0123456789ABCDEF'.split(''); for (let i = 0; i < 5; i++) { color += letters[Math.round(Math.random() * 15)]; } if (color === void 0) { return getRandomColor(); } ; return color; } ; ({}); // @--end-of-file-for-module=ng2-logger lib/log.ts ; ({}); // @--end-of-file-for-module=ng2-logger lib/index.ts /** * Generated bundle index. Do not edit. */ export { Display, Level, LevelKey, LevelOrder, Log, Logger }; //# sourceMappingURL=ng2-logger.mjs.map