123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.Logger = exports.LoggerLevel = void 0;
- const util_1 = require("util");
- const error_1 = require("./error");
- const utils_1 = require("./utils");
- const classFilters = {};
- let filteredClasses = {};
- let level;
- const pid = process.pid;
- let currentLogger = console.warn;
- exports.LoggerLevel = Object.freeze({
- ERROR: 'error',
- WARN: 'warn',
- INFO: 'info',
- DEBUG: 'debug',
- error: 'error',
- warn: 'warn',
- info: 'info',
- debug: 'debug'
- });
- class Logger {
-
- constructor(className, options) {
- options = options !== null && options !== void 0 ? options : {};
-
- this.className = className;
-
- if (!(options.logger instanceof Logger) && typeof options.logger === 'function') {
- currentLogger = options.logger;
- }
-
- if (options.loggerLevel) {
- level = options.loggerLevel || exports.LoggerLevel.ERROR;
- }
-
- if (filteredClasses[this.className] == null) {
- classFilters[this.className] = true;
- }
- }
-
- debug(message, object) {
- if (this.isDebug() &&
- ((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) ||
- (Object.keys(filteredClasses).length === 0 && classFilters[this.className]))) {
- const dateTime = new Date().getTime();
- const msg = (0, util_1.format)('[%s-%s:%s] %s %s', 'DEBUG', this.className, pid, dateTime, message);
- const state = {
- type: exports.LoggerLevel.DEBUG,
- message,
- className: this.className,
- pid,
- date: dateTime
- };
- if (object)
- state.meta = object;
- currentLogger(msg, state);
- }
- }
-
- warn(message, object) {
- if (this.isWarn() &&
- ((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) ||
- (Object.keys(filteredClasses).length === 0 && classFilters[this.className]))) {
- const dateTime = new Date().getTime();
- const msg = (0, util_1.format)('[%s-%s:%s] %s %s', 'WARN', this.className, pid, dateTime, message);
- const state = {
- type: exports.LoggerLevel.WARN,
- message,
- className: this.className,
- pid,
- date: dateTime
- };
- if (object)
- state.meta = object;
- currentLogger(msg, state);
- }
- }
-
- info(message, object) {
- if (this.isInfo() &&
- ((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) ||
- (Object.keys(filteredClasses).length === 0 && classFilters[this.className]))) {
- const dateTime = new Date().getTime();
- const msg = (0, util_1.format)('[%s-%s:%s] %s %s', 'INFO', this.className, pid, dateTime, message);
- const state = {
- type: exports.LoggerLevel.INFO,
- message,
- className: this.className,
- pid,
- date: dateTime
- };
- if (object)
- state.meta = object;
- currentLogger(msg, state);
- }
- }
-
- error(message, object) {
- if (this.isError() &&
- ((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) ||
- (Object.keys(filteredClasses).length === 0 && classFilters[this.className]))) {
- const dateTime = new Date().getTime();
- const msg = (0, util_1.format)('[%s-%s:%s] %s %s', 'ERROR', this.className, pid, dateTime, message);
- const state = {
- type: exports.LoggerLevel.ERROR,
- message,
- className: this.className,
- pid,
- date: dateTime
- };
- if (object)
- state.meta = object;
- currentLogger(msg, state);
- }
- }
-
- isInfo() {
- return level === exports.LoggerLevel.INFO || level === exports.LoggerLevel.DEBUG;
- }
-
- isError() {
- return level === exports.LoggerLevel.ERROR || level === exports.LoggerLevel.INFO || level === exports.LoggerLevel.DEBUG;
- }
-
- isWarn() {
- return (level === exports.LoggerLevel.ERROR ||
- level === exports.LoggerLevel.WARN ||
- level === exports.LoggerLevel.INFO ||
- level === exports.LoggerLevel.DEBUG);
- }
-
- isDebug() {
- return level === exports.LoggerLevel.DEBUG;
- }
-
- static reset() {
- level = exports.LoggerLevel.ERROR;
- filteredClasses = {};
- }
-
- static currentLogger() {
- return currentLogger;
- }
-
- static setCurrentLogger(logger) {
- if (typeof logger !== 'function') {
- throw new error_1.MongoInvalidArgumentError('Current logger must be a function');
- }
- currentLogger = logger;
- }
-
- static filter(type, values) {
- if (type === 'class' && Array.isArray(values)) {
- filteredClasses = {};
- values.forEach(x => (filteredClasses[x] = true));
- }
- }
-
- static setLevel(newLevel) {
- if (newLevel !== exports.LoggerLevel.INFO &&
- newLevel !== exports.LoggerLevel.ERROR &&
- newLevel !== exports.LoggerLevel.DEBUG &&
- newLevel !== exports.LoggerLevel.WARN) {
- throw new error_1.MongoInvalidArgumentError(`Argument "newLevel" should be one of ${(0, utils_1.enumToString)(exports.LoggerLevel)}`);
- }
- level = newLevel;
- }
- }
- exports.Logger = Logger;
|