yandex-tank-ammo-generator
Version:
Cartridge generation for Yandex tank
81 lines (80 loc) • 3.03 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Logger = require("pino");
// "pino": "=4.16.1",
// "pino-pretty": "=1.0.1",
const writable = process.stdout;
// // не юзается
// const dest = factory({
// colorize: true, // --colorize
// crlf: false, // --crlf
// errorLikeObjectKeys: ['err', 'error', 'fatal'], // --errorLikeObjectKeys
// errorProps: '', // --errorProps
// levelFirst: false, // --levelFirst
// messageKey: 'msg', // --messageKey
// translateTime: false, // --translateTime
// search: 'foo == `bar`', // --search
// ignore: 'pid,hostname,level,time', // --ignore
// }).asMetaWrapper(writable);
/**
* @Class Log
* @Classdesc Логи с цветовой индикацией при отображении и уровнями логов.
*/
class Log {
constructor() {
this._name = process.env.SERVICE_NAME || '';
this._cacheTimers = {};
this.toStringArray = (arr) => arr.map(item => this.customStringify(item));
this.customStringify = (item) => {
if (!item) {
return String(item);
}
if (typeof item === 'string') {
return item;
}
if (item instanceof Error) {
return JSON.stringify(item.stack ? item.stack.toString() : item.toString());
}
return JSON.stringify(item);
};
this.fatal = (...args) => this._nativeLogger.fatal(this.getBody(args));
this.error = (...args) => this._nativeLogger.error(this.getBody(args));
this.warn = (...args) => this._nativeLogger.warn(this.getBody(args));
this.info = (...args) => this._nativeLogger.info(this.getBody(args));
this.debug = (...args) => this._nativeLogger.debug(this.getBody(args));
this.trace = (...args) => this._nativeLogger.trace(this.getBody(args));
this._nativeLogger = Logger({
name: 'Kari',
safe: true,
level: (process.env.LOGLEVEL || 'debug').toString(),
prettyPrint: true,
});
}
setLocation(locationOrHandlerName) {
this._location = locationOrHandlerName || 'oldLog';
return this;
}
getLogger() {
this._location = 'oldLog';
return this;
}
getBody(args) {
return JSON.stringify({
service: this._name,
location: this._location,
data: this.toStringArray(args).join(' '),
}).toString();
}
time(timerName) {
this._cacheTimers[timerName] = new Date();
}
timeEnd(timerName) {
if (this._cacheTimers.hasOwnProperty(timerName)) {
this.debug(`Таймер ${timerName}. Результат ${+new Date() - +this._cacheTimers[timerName]} ms`);
this._cacheTimers[timerName] = undefined;
}
this.error(`Таймер ${timerName} не был установлен`);
}
}
exports.Log = Log;
exports.logger = new Log().setLocation('oldLog');