ng2-logger
Version:
isomorphic logger for browser/server in typescript
246 lines • 8.25 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = void 0;
const level_1 = require("./level");
const display_1 = require("./display");
const lib_1 = require("tnp-core/lib");
class Logger {
name;
color;
developmentMode;
allowed;
isMuted;
fixedWidth;
_level;
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;
}
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 && lib_1.Helpers.contain(this.allowed, level_1.Level.__NOTHING)
&& !lib_1.Helpers.contain(this.allowed, level_1.Level.DATA))
return this;
if (this.allowed.length === 0 || lib_1.Helpers.contain(this.allowed, level_1.Level.DATA)) {
// @ts-ignore
display_1.Display.msg.apply(void 0, [
name,
...data,
this.name,
this.color,
level_1.Level.DATA,
this.fixedWidth,
this.isProductionMode,
]);
}
return this;
}
_error(name, ...data) {
if (this.isMuted)
return this;
if (this.allowed.length >= 1 && lib_1.Helpers.contain(this.allowed, level_1.Level.__NOTHING)
&& !lib_1.Helpers.contain(this.allowed, level_1.Level.ERROR))
return this;
if (this.allowed.length === 0 || lib_1.Helpers.contain(this.allowed, level_1.Level.ERROR)) {
// @ts-ignore
display_1.Display.msg.apply(void 0, [
name,
...data,
this.name,
this.color,
level_1.Level.ERROR,
this.fixedWidth,
this.isProductionMode,
]);
}
return this;
}
_info(name, ...data) {
if (this.isMuted)
return this;
if (this.allowed.length >= 1 && lib_1.Helpers.contain(this.allowed, level_1.Level.__NOTHING)
&& !lib_1.Helpers.contain(this.allowed, level_1.Level.INFO))
return this;
if (this.allowed.length === 0 || lib_1.Helpers.contain(this.allowed, level_1.Level.INFO)) {
// @ts-ignore
display_1.Display.msg.apply(void 0, [
name,
...data,
this.name,
this.color,
level_1.Level.INFO,
this.fixedWidth,
this.isProductionMode,
]);
}
return this;
}
_success(name, ...data) {
if (this.isMuted)
return this;
if (this.allowed.length >= 1 && lib_1.Helpers.contain(this.allowed, level_1.Level.__NOTHING)
&& !lib_1.Helpers.contain(this.allowed, level_1.Level.SUCCESS))
return this;
if (this.allowed.length === 0 || lib_1.Helpers.contain(this.allowed, level_1.Level.SUCCESS)) {
// @ts-ignore
display_1.Display.msg.apply(void 0, [
name,
...data,
this.name,
this.color,
level_1.Level.SUCCESS,
this.fixedWidth,
this.isProductionMode,
]);
}
return this;
}
_taskStarted(name, ...data) {
if (this.isMuted)
return this;
if (this.allowed.length >= 1 && lib_1.Helpers.contain(this.allowed, level_1.Level.__NOTHING)
&& !lib_1.Helpers.contain(this.allowed, level_1.Level.TASK_STARTED))
return this;
if (this.allowed.length === 0 || lib_1.Helpers.contain(this.allowed, level_1.Level.TASK_STARTED)) {
// @ts-ignore
display_1.Display.msg.apply(void 0, [
name,
...data,
this.name,
this.color,
level_1.Level.TASK_STARTED,
this.fixedWidth,
this.isProductionMode,
]);
}
return this;
}
_taskDone(name, ...data) {
if (this.isMuted)
return this;
if (this.allowed.length >= 1 && lib_1.Helpers.contain(this.allowed, level_1.Level.__NOTHING)
&& !lib_1.Helpers.contain(this.allowed, level_1.Level.TASK_DONE))
return this;
if (this.allowed.length === 0 || lib_1.Helpers.contain(this.allowed, level_1.Level.TASK_DONE)) {
// @ts-ignore
display_1.Display.msg.apply(void 0, [
name,
...data,
this.name,
this.color,
level_1.Level.TASK_DONE,
this.fixedWidth,
this.isProductionMode,
]);
}
return this;
}
_warn(name, ...data) {
if (this.isMuted)
return this;
if (this.allowed.length >= 1 && lib_1.Helpers.contain(this.allowed, level_1.Level.__NOTHING)
&& !lib_1.Helpers.contain(this.allowed, level_1.Level.WARN))
return this;
if (this.allowed.length === 0 || lib_1.Helpers.contain(this.allowed, level_1.Level.WARN)) {
// @ts-ignore
display_1.Display.msg.apply(void 0, [
name,
...data,
this.name,
this.color,
level_1.Level.WARN,
this.fixedWidth,
this.isProductionMode,
]);
}
return this;
}
/**
* @see data
*/
d = (name, ...data) => this._data(name, data);
/**
* @see error
*/
er = (name, ...data) => this._error(name, data);
/**
* @see info
*/
i = (name, ...data) => this._info(name, data);
/**
* @see warn
*/
w = (name, ...data) => this._warn(name, data);
/**
* Logs message and data with the level=data
* @param message The message
* @param otherParams Additional parameters
*/
data = (message, ...otherParams) => { return this._data(message, otherParams); };
/**
* Logs message and data with the level=error
* @param message The message
* @param otherParams Additional parameters
*/
error = (message, ...otherParams) => this._error(message, otherParams);
/**
* Logs message and data with the level=info
* @param message The message
* @param otherParams Additional parameters
*/
info = (message, ...otherParams) => this._info(message, otherParams);
/**
* Logs message and data with the level=success
* @param message The message
* @param otherParams Additional parameters
*/
success = (message, ...otherParams) => this._success(message, otherParams);
/**
* Logs message and data with the level=taskStarted
* @param message The message
* @param otherParams Additional parameters
*/
taskStarted = (message, ...otherParams) => this._taskStarted(message, otherParams);
/**
* Logs message and data with the level=taskDone
* @param message The message
* @param otherParams Additional parameters
*/
taskDone = (message, ...otherParams) => this._taskDone(message, otherParams);
/**
* Logs message and data with the level=warn
* @param message The message
* @param otherParams Additional parameters
*/
warn = (message, ...otherParams) => this._warn(message, otherParams);
}
exports.Logger = Logger;
//# sourceMappingURL=logger.js.map