From ca348b2ff61905285b1831bb3e56352265926a18 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Wed, 30 Mar 2022 18:15:56 -0500 Subject: [PATCH] Add Trace logging level; bump version --- package.json | 2 +- src/service/Logging.ts | 11 ++++++++++- src/support/bus/Bus.ts | 1 - src/util/error/DebuggingTraceIsNotAnError.ts | 15 +++++++++++++++ src/util/index.ts | 1 + src/util/logging/Logger.ts | 2 ++ src/util/logging/types.ts | 1 + 7 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 src/util/error/DebuggingTraceIsNotAnError.ts diff --git a/package.json b/package.json index 333ce49..fb14b69 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@extollo/lib", - "version": "0.9.9", + "version": "0.9.10", "description": "The framework library that lifts up your code.", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/src/service/Logging.ts b/src/service/Logging.ts index 67f5ed8..6ce8d3e 100644 --- a/src/service/Logging.ts +++ b/src/service/Logging.ts @@ -1,4 +1,4 @@ -import {Logger, LoggingLevel, LogMessage} from '../util' +import {DebuggingTraceIsNotAnError, Logger, LoggingLevel, LogMessage} from '../util' import {Singleton} from '../di' /** @@ -109,6 +109,10 @@ export class Logging { this.writeLog(LoggingLevel.Verbose, output, force) } + public trace(output: unknown, force = false): void { + this.writeLog(LoggingLevel.Trace, output, force) + } + /** * Helper function to write the given output, at the given logging level, to * all of the registered loggers. @@ -119,10 +123,15 @@ export class Logging { */ protected writeLog(level: LoggingLevel, output: unknown, force = false): void { const message = this.buildMessage(level, output) + const trace = DebuggingTraceIsNotAnError.getTrace() + const traceMessage = this.buildMessage(LoggingLevel.Trace, trace) if ( this.currentLevel >= level || force ) { for ( const logger of this.registeredLoggers ) { try { logger.write(message) + if ( this.currentLevel >= LoggingLevel.Trace ) { + logger.write(traceMessage) + } } catch (e) { console.error('logging error', e) // eslint-disable-line no-console } diff --git a/src/support/bus/Bus.ts b/src/support/bus/Bus.ts index 5ae2efd..da43c5c 100644 --- a/src/support/bus/Bus.ts +++ b/src/support/bus/Bus.ts @@ -200,7 +200,6 @@ export class Bus extends Unit implements EventBus< if ( Array.isArray(config.connectors) ) { for ( const connector of (config.connectors as BusConnectorConfig[]) ) { this.logging.verbose(`Creating bus connection of type: ${connector.type}`) - this.logging.verbose(new Error().stack) if ( connector.type === 'redis' ) { await this.connect(this.make(RedisBus)) } diff --git a/src/util/error/DebuggingTraceIsNotAnError.ts b/src/util/error/DebuggingTraceIsNotAnError.ts new file mode 100644 index 0000000..54c11fb --- /dev/null +++ b/src/util/error/DebuggingTraceIsNotAnError.ts @@ -0,0 +1,15 @@ +/** + * Error class used to generate debug stack traces, not thrown. + */ +export class DebuggingTraceIsNotAnError extends Error { + static getTrace(): string { + const oldLimit = Error.stackTraceLimit + if ( oldLimit < 50 ) { + Error.stackTraceLimit = 50 + } + + const trace = (new this()).stack || String(new this()) + Error.stackTraceLimit = oldLimit + return trace + } +} diff --git a/src/util/index.ts b/src/util/index.ts index 8504156..651db2f 100644 --- a/src/util/index.ts +++ b/src/util/index.ts @@ -15,6 +15,7 @@ export * from './const/http' export * from './error/ErrorWithContext' export * from './error/MethodNotSupportedError' +export * from './error/DebuggingTraceIsNotAnError' export * from './logging/Logger' export * from './logging/StandardLogger' diff --git a/src/util/logging/Logger.ts b/src/util/logging/Logger.ts index eea9b87..f350627 100644 --- a/src/util/logging/Logger.ts +++ b/src/util/logging/Logger.ts @@ -44,6 +44,8 @@ export abstract class Logger { return color.cyan(' debug') case LoggingLevel.Verbose: return color.gray('verbose') + case LoggingLevel.Trace: + return color.dim(' trace') case LoggingLevel.Silent: return color.gray(' silent') } diff --git a/src/util/logging/types.ts b/src/util/logging/types.ts index fa7fbe1..f7fe851 100644 --- a/src/util/logging/types.ts +++ b/src/util/logging/types.ts @@ -9,6 +9,7 @@ enum LoggingLevel { Info = 4, Debug = 5, Verbose = 6, + Trace = 7, } /**