@nglogger/console
Version:
Console transport for @nglogger
163 lines • 5.57 kB
JavaScript
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
'use strict';
import { Injectable } from '@angular/core';
/* Constants */
var /** @type {?} */ COLOR_TRANSPARENT = 'rgba(0, 0, 0, 0)';
var /** @type {?} */ COLOR_WHITE = '#ffffff';
var /** @type {?} */ COLOR_BLACK = '#1d1f21';
var /** @type {?} */ COLOR_GREY = '#bbbbbb';
var /** @type {?} */ COLOR_YELLOW = '#f0c674';
var /** @type {?} */ COLOR_AQUA = '#8abeb7';
var /** @type {?} */ COLOR_RED = '#cc6666';
var /** @type {?} */ COLOR_PURPLE = '#b294bb';
var /** @type {?} */ COLOR_BLUE = '#0074d9';
var /** @type {?} */ LEVEL_STYLE = {
fatal: "background: " + COLOR_RED + "; color: " + COLOR_WHITE,
error: "background: " + COLOR_RED + "; color: " + COLOR_WHITE,
warn: "background: " + COLOR_AQUA + "; color: " + COLOR_WHITE,
info: "background: " + COLOR_YELLOW + "; color: " + COLOR_WHITE,
verbose: "background: " + COLOR_GREY + "; color: " + COLOR_WHITE,
trace: "background: " + COLOR_GREY + "; color: " + COLOR_WHITE,
debug: "background: " + COLOR_PURPLE + "; color: " + COLOR_WHITE
};
var /** @type {?} */ BRAKET_STYLE = "color: " + COLOR_GREY;
var /** @type {?} */ COLON_STYLE = "color: " + COLOR_GREY;
var /** @type {?} */ DATE_STYLE = "color: " + COLOR_BLACK;
var /** @type {?} */ RESET_STYLE = "background: " + COLOR_TRANSPARENT + "; color: " + COLOR_BLACK;
var /** @type {?} */ SCOPE_STYLE = "color: " + COLOR_BLUE;
/**
* @param {?} val
* @param {?} count
* @return {?}
*/
function stubZero(val, count) {
var /** @type {?} */ strVal = "" + val;
var /** @type {?} */ len = strVal.length;
if (len >= count) {
return strVal;
}
var /** @type {?} */ res = '';
for (var /** @type {?} */ i = 0, /** @type {?} */ ii = count - len; i < ii; i++) {
res += '0';
}
res += strVal;
return res;
}
var ConsoleTransport = /** @class */ (function () {
function ConsoleTransport() {
this._console = console;
}
/**
* @param {?} level
* @param {?} scope
* @param {?} subject
* @param {?=} meta
* @return {?}
*/
ConsoleTransport.prototype.log = /**
* @param {?} level
* @param {?} scope
* @param {?} subject
* @param {?=} meta
* @return {?}
*/
function (level, scope, subject, meta) {
var /** @type {?} */ date = this._getDate();
var /** @type {?} */ consoleLevel = this._getConsoleLevel(level);
var /** @type {?} */ levelStyle = this._getLevelStyle(level);
var /** @type {?} */ template = "%c[%c" + date + "%c]%c %c " + level + " %c(%c" + scope + "%c)%c:";
var /** @type {?} */ title = [
template,
BRAKET_STYLE,
DATE_STYLE,
BRAKET_STYLE,
RESET_STYLE,
levelStyle,
BRAKET_STYLE,
SCOPE_STYLE,
BRAKET_STYLE,
COLON_STYLE
];
if (meta !== undefined) {
(_a = this._console)[consoleLevel].apply(_a, title.concat([subject, meta]));
}
else {
(_b = this._console)[consoleLevel].apply(_b, title.concat([subject]));
}
var _a, _b;
};
/**
* @return {?}
*/
ConsoleTransport.prototype._getDate = /**
* @return {?}
*/
function () {
var /** @type {?} */ now = new Date();
var /** @type {?} */ h = now.getHours();
var /** @type {?} */ m = now.getMinutes();
var /** @type {?} */ s = now.getSeconds();
var /** @type {?} */ ms = now.getMilliseconds();
// tslint:disable-next-line:no-magic-numbers
var /** @type {?} */ hh = stubZero(h, 2);
// tslint:disable-next-line:no-magic-numbers
var /** @type {?} */ mm = stubZero(m, 2);
// tslint:disable-next-line:no-magic-numbers
var /** @type {?} */ ss = stubZero(s, 2);
// tslint:disable-next-line:no-magic-numbers
var /** @type {?} */ sss = stubZero(ms, 3);
return hh + ":" + mm + ":" + ss + "." + sss;
};
/**
* @param {?} level
* @return {?}
*/
ConsoleTransport.prototype._getConsoleLevel = /**
* @param {?} level
* @return {?}
*/
function (level) {
switch (level) {
case 'fatal': return 'error';
case 'error': return 'error';
case 'warn': return 'warn';
case 'info': return 'info';
case 'verbose': return 'log';
case 'trace': return 'trace';
case 'debug': return 'log';
default: return 'log';
}
};
/**
* @param {?} level
* @return {?}
*/
ConsoleTransport.prototype._getLevelStyle = /**
* @param {?} level
* @return {?}
*/
function (level) {
var /** @type {?} */ style = LEVEL_STYLE[level];
return style !== undefined ? style : RESET_STYLE;
};
ConsoleTransport.decorators = [
{ type: Injectable },
];
return ConsoleTransport;
}());
export { ConsoleTransport };
function ConsoleTransport_tsickle_Closure_declarations() {
/** @type {!Array<{type: !Function, args: (undefined|!Array<?>)}>} */
ConsoleTransport.decorators;
/**
* @nocollapse
* @type {function(): !Array<(null|{type: ?, decorators: (undefined|!Array<{type: !Function, args: (undefined|!Array<?>)}>)})>}
*/
ConsoleTransport.ctorParameters;
/** @type {?} */
ConsoleTransport.prototype._console;
}
//# sourceMappingURL=console-transport.service.js.map