123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
-
-
- const instances = [];
-
- var LogLevel;
- (function (LogLevel) {
- LogLevel[LogLevel["DEBUG"] = 0] = "DEBUG";
- LogLevel[LogLevel["VERBOSE"] = 1] = "VERBOSE";
- LogLevel[LogLevel["INFO"] = 2] = "INFO";
- LogLevel[LogLevel["WARN"] = 3] = "WARN";
- LogLevel[LogLevel["ERROR"] = 4] = "ERROR";
- LogLevel[LogLevel["SILENT"] = 5] = "SILENT";
- })(LogLevel || (LogLevel = {}));
- const levelStringToEnum = {
- 'debug': LogLevel.DEBUG,
- 'verbose': LogLevel.VERBOSE,
- 'info': LogLevel.INFO,
- 'warn': LogLevel.WARN,
- 'error': LogLevel.ERROR,
- 'silent': LogLevel.SILENT
- };
-
- const defaultLogLevel = LogLevel.INFO;
-
- const ConsoleMethod = {
- [LogLevel.DEBUG]: 'log',
- [LogLevel.VERBOSE]: 'log',
- [LogLevel.INFO]: 'info',
- [LogLevel.WARN]: 'warn',
- [LogLevel.ERROR]: 'error'
- };
-
- const defaultLogHandler = (instance, logType, ...args) => {
- if (logType < instance.logLevel) {
- return;
- }
- const now = new Date().toISOString();
- const method = ConsoleMethod[logType];
- if (method) {
- console[method](`[${now}] ${instance.name}:`, ...args);
- }
- else {
- throw new Error(`Attempted to log a message with an invalid logType (value: ${logType})`);
- }
- };
- class Logger {
-
-
- constructor(name) {
- this.name = name;
-
-
- this._logLevel = defaultLogLevel;
-
-
- this._logHandler = defaultLogHandler;
-
-
- this._userLogHandler = null;
-
-
- instances.push(this);
- }
- get logLevel() {
- return this._logLevel;
- }
- set logLevel(val) {
- if (!(val in LogLevel)) {
- throw new TypeError(`Invalid value "${val}" assigned to \`logLevel\``);
- }
- this._logLevel = val;
- }
-
- setLogLevel(val) {
- this._logLevel = typeof val === 'string' ? levelStringToEnum[val] : val;
- }
- get logHandler() {
- return this._logHandler;
- }
- set logHandler(val) {
- if (typeof val !== 'function') {
- throw new TypeError('Value assigned to `logHandler` must be a function');
- }
- this._logHandler = val;
- }
- get userLogHandler() {
- return this._userLogHandler;
- }
- set userLogHandler(val) {
- this._userLogHandler = val;
- }
-
-
- debug(...args) {
- this._userLogHandler && this._userLogHandler(this, LogLevel.DEBUG, ...args);
- this._logHandler(this, LogLevel.DEBUG, ...args);
- }
- log(...args) {
- this._userLogHandler &&
- this._userLogHandler(this, LogLevel.VERBOSE, ...args);
- this._logHandler(this, LogLevel.VERBOSE, ...args);
- }
- info(...args) {
- this._userLogHandler && this._userLogHandler(this, LogLevel.INFO, ...args);
- this._logHandler(this, LogLevel.INFO, ...args);
- }
- warn(...args) {
- this._userLogHandler && this._userLogHandler(this, LogLevel.WARN, ...args);
- this._logHandler(this, LogLevel.WARN, ...args);
- }
- error(...args) {
- this._userLogHandler && this._userLogHandler(this, LogLevel.ERROR, ...args);
- this._logHandler(this, LogLevel.ERROR, ...args);
- }
- }
- function setLogLevel(level) {
- instances.forEach(inst => {
- inst.setLogLevel(level);
- });
- }
- function setUserLogHandler(logCallback, options) {
- for (const instance of instances) {
- let customLogLevel = null;
- if (options && options.level) {
- customLogLevel = levelStringToEnum[options.level];
- }
- if (logCallback === null) {
- instance.userLogHandler = null;
- }
- else {
- instance.userLogHandler = (instance, level, ...args) => {
- const message = args
- .map(arg => {
- if (arg == null) {
- return null;
- }
- else if (typeof arg === 'string') {
- return arg;
- }
- else if (typeof arg === 'number' || typeof arg === 'boolean') {
- return arg.toString();
- }
- else if (arg instanceof Error) {
- return arg.message;
- }
- else {
- try {
- return JSON.stringify(arg);
- }
- catch (ignored) {
- return null;
- }
- }
- })
- .filter(arg => arg)
- .join(' ');
- if (level >= (customLogLevel !== null && customLogLevel !== void 0 ? customLogLevel : instance.logLevel)) {
- logCallback({
- level: LogLevel[level].toLowerCase(),
- message,
- args,
- type: instance.name
- });
- }
- };
- }
- }
- }
-
- export { LogLevel, Logger, setLogLevel, setUserLogHandler };
|