diff --git a/src/Enumerables/Enumerable.spec.ts b/src/Enumerables/Enumerable.spec.ts index f279f561..90db5e0c 100644 --- a/src/Enumerables/Enumerable.spec.ts +++ b/src/Enumerables/Enumerable.spec.ts @@ -1,12 +1,15 @@ import { ArrayEnumerable, Enumerable } from '../../src/Enumerables'; -import { getDefaultLogger } from '../../src/Logging/defaultLogger'; - -/* -setDefaultLogger(new ConsoleLogger(console, { - loggingVerbosity: LogLevel.Trace, - useTraceMethodForTraceLogLevel: false -})); -*/ + + +// import ConsoleLogger from '../Logging/ConsoleLogger'; +// import { LogLevel } from '../Logging/_types'; +// import { getDefaultLogger, setDefaultLogger } from '../Logging/utils/defaultLogger'; +// setDefaultLogger(new ConsoleLogger(console, { +// loggingVerbosity: LogLevel.Trace, +// useTraceMethodForTraceLogLevel: false +// })); + +import { getDefaultLogger } from '../Logging/utils/defaultLogger'; const logger = getDefaultLogger(); describe("Enumerable", () => diff --git a/src/Logging/AggregateLogger.ts b/src/Logging/AggregateLogger.ts index 6a9acf53..ec80ebb7 100644 --- a/src/Logging/AggregateLogger.ts +++ b/src/Logging/AggregateLogger.ts @@ -12,103 +12,93 @@ export default class AggregateLogger implements ILogger } - debug(msg: string): void + debug(msg: string, ...args: unknown[]): void { - for (const logger of this.#loggers) logger.debug(msg); + for (const logger of this.#loggers) logger.debug(msg, ...args); } - debugError(err: Error): void; - debugError(err: Error, msg: string): void; - debugError(err: Error, msg?: string) + debugError(err: Error, msg?: string, ...args: unknown[]) { for (const logger of this.#loggers) { if (msg) { - logger.debugError(err, msg); + logger.debugError(err, msg, ...args); } else { - logger.debugError(err); + logger.debugError(err, ...args); } } } - error(msg: string): void + error(msg: string, ...args: unknown[]): void { - for (const logger of this.#loggers) logger.error(msg); + for (const logger of this.#loggers) logger.error(msg, ...args); } - errorError(err: Error): void; - errorError(err: Error, msg: string): void; - errorError(err: Error, msg?: string) + errorError(err: Error, msg?: string, ...args: unknown[]) { for (const logger of this.#loggers) { if (msg) { - logger.errorError(err, msg); + logger.errorError(err, msg, ...args); } else { - logger.errorError(err); + logger.errorError(err, ...args); } } } - info(msg: string): void + info(msg: string, ...args: unknown[]): void { - for (const logger of this.#loggers) logger.info(msg); + for (const logger of this.#loggers) logger.info(msg, ...args); } - infoError(err: Error): void; - infoError(err: Error, msg: string): void; - infoError(err: Error, msg?: string) + infoError(err: Error, msg?: string, ...args: unknown[]) { for (const logger of this.#loggers) { if (msg) { - logger.infoError(err, msg); + logger.infoError(err, msg, ...args); } else { - logger.infoError(err); + logger.infoError(err, ...args); } } } - trace(msg: string): void + trace(msg: string, ...args: unknown[]): void { - for (const logger of this.#loggers) logger.trace(msg); + for (const logger of this.#loggers) logger.trace(msg, ...args); } - traceError(err: Error): void; - traceError(err: Error, msg: string): void; - traceError(err: Error, msg?: string) + traceError(err: Error, msg?: string, ...args: unknown[]) { for (const logger of this.#loggers) { if (msg) { - logger.traceError(err, msg); + logger.traceError(err, msg, ...args); } else { - logger.traceError(err); + logger.traceError(err, ...args); } } } - warn(msg: string): void + warn(msg: string, ...args: unknown[]): void { - for (const logger of this.#loggers) logger.warn(msg); + for (const logger of this.#loggers) logger.warn(msg, ...args); } - warnError(err: Error): void; - warnError(err: Error, msg: string): void; - warnError(err: Error, msg?: string) + warnError(err: Error, msg?: string, ...args: unknown[]) { for (const logger of this.#loggers) { if (msg) { - logger.warnError(err, msg); + logger.warnError(err, msg, ...args); } else { - logger.warnError(err); + logger.warnError(err, ...args); } } } diff --git a/src/Logging/ConsoleLogger.spec.ts b/src/Logging/ConsoleLogger.spec.ts new file mode 100644 index 00000000..21450028 --- /dev/null +++ b/src/Logging/ConsoleLogger.spec.ts @@ -0,0 +1,38 @@ +import { IConsole } from '../IConsole'; +import ConsoleLogger from './ConsoleLogger'; +import { LogLevel } from './_types'; + + + +describe("ConsoleLogger.constructor", () => +{ + it("should return a new ConsoleLogger", () => + { + const actual = new ConsoleLogger(console); + + expect(actual).not.toBeNull(); + }); +}); + +describe("ConsoleLogger.log", () => +{ + it("should always log", () => + { + const consoleImpl: IConsole = { + log: jest.fn(_ => { }) + }; + + const actual = new ConsoleLogger(consoleImpl, { + loggingVerbosity: LogLevel.Debug, + useTraceMethodForTraceLogLevel: true + }); + + actual.debug(""); + actual.error(""); + actual.info(""); + actual.trace(""); + actual.warn(""); + + expect(consoleImpl.log).toHaveBeenCalledTimes(5); + }); +}); \ No newline at end of file diff --git a/src/Logger.spec.ts b/src/Logging/Logger.spec.ts similarity index 96% rename from src/Logger.spec.ts rename to src/Logging/Logger.spec.ts index 960d8022..9ec18b93 100644 --- a/src/Logger.spec.ts +++ b/src/Logging/Logger.spec.ts @@ -1,5 +1,5 @@ -import testLogVerbosity from '../src/Logging/testLogVerbosity'; -import { LogLevel } from '../src/Logging/_types'; +import { LogLevel } from './_types'; +import testLogVerbosity from './utils/testLogVerbosity'; function testLogVerbosityTest(expected: boolean, desiredLevel: LogLevel, loggingVerbosity: LogLevel) { diff --git a/src/Logging/Logger.ts b/src/Logging/Logger.ts index 0d34ac9d..5628c327 100644 --- a/src/Logging/Logger.ts +++ b/src/Logging/Logger.ts @@ -1,9 +1,8 @@ import { ILogger, ILoggerConfig, LogLevel } from './_types'; -import testLogVerbosity from "./testLogVerbosity"; +import testLogVerbosity from "./utils/testLogVerbosity"; import ScopedLogger from "./ScopedLogger"; -import IndentedStringBuilder from "../IO/IndentedStringBuilder"; -import errorToLogMessage from '../errorToLogMessage'; import { IDisposable } from '../Types'; +import errorAndMessageToString from './utils/errorAndMessageToString'; const defaultConfig: ILoggerConfig = { loggingVerbosity: LogLevel.Info, @@ -11,131 +10,64 @@ const defaultConfig: ILoggerConfig = { export default abstract class Logger implements ILogger { - readonly #config: ILoggerConfig + static openLevel = '['; + static closeLevel = ']'; + + readonly #config: ILoggerConfig; constructor(config: ILoggerConfig = defaultConfig) { this.#config = config; } - public debug(msg: string): void + public debug(msg: string, ...args: unknown[]): void { - this.log(LogLevel.Debug, new IndentedStringBuilder(0, msg)); + this.log(LogLevel.Debug, msg, ...args); } - public debugError(err: Error): void; - public debugError(err: Error, msg: string): void - public debugError(err: Error, msg?: string): void + public debugError(err: Error, msg?: string, ...args: unknown[]): void { - const sb = new IndentedStringBuilder(0); - if (msg !== undefined) - { - sb.appendLine(msg); - sb.indent(); - errorToLogMessage(err, sb); - sb.unindent(); - } - else - { - errorToLogMessage(err, sb); - } - this.log(LogLevel.Debug, sb); + this.log(LogLevel.Debug, errorAndMessageToString(msg, err), err, ...args); } - public error(msg: string): void + public error(msg: string, ...args: unknown[]): void { - this.log(LogLevel.Error, new IndentedStringBuilder(0, msg)); + this.log(LogLevel.Error, msg, ...args); } - public errorError(err: Error): void; - public errorError(err: Error, msg: string): void; - public errorError(err: Error, msg?: string): void + public errorError(err: Error, msg?: string, ...args: unknown[]): void { - const sb = new IndentedStringBuilder(0); - if (msg !== undefined) - { - sb.appendLine(msg); - sb.indent(); - errorToLogMessage(err, sb); - sb.unindent(); - } - else - { - errorToLogMessage(err, sb); - } - this.log(LogLevel.Error, sb); + this.log(LogLevel.Error, errorAndMessageToString(msg, err), err, ...args); } - public info(msg: string): void + public info(msg: string, ...args: unknown[]): void { - this.log(LogLevel.Info, new IndentedStringBuilder(0, msg)); + this.log(LogLevel.Info, msg, ...args); } - public infoError(err: Error): void; - public infoError(err: Error, msg: string): void; - public infoError(err: Error, msg?: string): void + public infoError(err: Error, msg?: string, ...args: unknown[]): void { - const sb = new IndentedStringBuilder(0); - if (msg !== undefined) - { - sb.appendLine(msg); - sb.indent(); - errorToLogMessage(err, sb); - sb.unindent(); - } - else - { - errorToLogMessage(err, sb); - } - this.log(LogLevel.Info, sb); + this.log(LogLevel.Info, errorAndMessageToString(msg, err), err, ...args); } - public trace(msg: string): void + public trace(msg: string, ...args: unknown[]): void { - this.log(LogLevel.Trace, new IndentedStringBuilder(0, msg)); + this.log(LogLevel.Trace, msg, ...args); } - public traceError(err: Error): void; - public traceError(err: Error, msg: string): void; - public traceError(err: Error, msg?: string): void + public traceError(err: Error, msg?: string, ...args: unknown[]): void { - const sb = new IndentedStringBuilder(0); - if (msg !== undefined) - { - sb.appendLine(msg); - sb.indent(); - errorToLogMessage(err, sb); - sb.unindent(); - } - else - { - errorToLogMessage(err, sb); - } - this.log(LogLevel.Trace, sb); + this.log(LogLevel.Trace, errorAndMessageToString(msg, err), err, ...args); } - public warn(msg: string): void + public warn(msg: string, ...args: unknown[]): void { - this.log(LogLevel.Warn, new IndentedStringBuilder(0, msg)); + this.log(LogLevel.Warn, msg, ...args); } - public warnError(err: Error): void; - public warnError(err: Error, msg: string): void; - public warnError(err: Error, msg?: string): void + public warnError(err: Error, msg?: string, ...args: unknown[]): void { - const sb = new IndentedStringBuilder(0); - if (msg !== undefined) - { - sb.appendLine(msg); - sb.indent(); - errorToLogMessage(err, sb); - sb.unindent(); - } - else - { - errorToLogMessage(err, sb); - } - this.log(LogLevel.Warn, sb); + this.log(LogLevel.Warn, errorAndMessageToString(msg, err), err, ...args); } public scope(name: string): ILogger & IDisposable @@ -143,45 +75,81 @@ export default abstract class Logger implements ILogger return new ScopedLogger(this, name); } - private log(level: LogLevel, sb: IndentedStringBuilder): void + /** + * Calls the implementation of the logging level + * @param level + * @param msg + * @param args extra objects/values to include with the log message + */ + private log(level: LogLevel, msg: string, ...args: unknown[]): void { if (testLogVerbosity(level, this.#config.loggingVerbosity)) { - const message = `[${level}] ${sb.toString()}`; + const message = `${Logger.openLevel}${level}${Logger.closeLevel} ${msg}`; switch (level) { case LogLevel.Error: - this._errorMethod(message); + this._errorMethod(message, ...args); break; case LogLevel.Warn: - this._warnMethod(message); + this._warnMethod(message, ...args); break; case LogLevel.Info: - this._infoMethod(message); + this._infoMethod(message, ...args); break; case LogLevel.Trace: - this._traceMethod(message); + this._traceMethod(message, ...args); break; case LogLevel.Debug: - this._debugMethod(message); + this._debugMethod(message, ...args); break; default: - this._defaultMethod(message); + this._defaultMethod(message, ...args); break; } } } - protected abstract _errorMethod(message: string): void; - protected abstract _warnMethod(message: string): void; - protected abstract _infoMethod(message: string): void; - protected abstract _traceMethod(message: string): void; - protected abstract _debugMethod(message: string): void; - protected abstract _defaultMethod(message: string): void; + /** + * Implementation for a Log message flagged at the Error log level + * @param msg + * @param args extra objects/values to include with the log message + */ + protected abstract _errorMethod(message: string, ...args: unknown[]): void; + /** + * Implementation for a Log message flagged at the Warning log level + * @param msg + * @param args extra objects/values to include with the log message + */ + protected abstract _warnMethod(message: string, ...args: unknown[]): void; + /** + * Implementation for a Log message flagged at the Informational log level + * @param msg + * @param args extra objects/values to include with the log message + */ + protected abstract _infoMethod(message: string, ...args: unknown[]): void; + /** + * Implementation for a Log message flagged at the Trace log level + * @param msg + * @param args extra objects/values to include with the log message + */ + protected abstract _traceMethod(message: string, ...args: unknown[]): void; + /** + * Implementation for a Log message flagged at the Debug log level + * @param msg + * @param args extra objects/values to include with the log message + */ + protected abstract _debugMethod(message: string, ...args: unknown[]): void; + /** + * Implementation for a Log message not flagged + * @param msg + * @param args extra objects/values to include with the log message + */ + protected abstract _defaultMethod(message: string, ...args: unknown[]): void; } \ No newline at end of file diff --git a/src/Logging/ScopedLogger.ts b/src/Logging/ScopedLogger.ts index b96f2aaf..ea7d8b38 100644 --- a/src/Logging/ScopedLogger.ts +++ b/src/Logging/ScopedLogger.ts @@ -4,6 +4,10 @@ import SR from '../i18n/en/generic.strings.json'; export default class ScopedLogger implements ILogger, IDisposable { + static openScope = '['; + static closeScope = ']'; + + private readonly _scope: string[]; readonly #name: string; readonly #logger: ILogger; @@ -13,76 +17,79 @@ export default class ScopedLogger implements ILogger, IDisposable { this.#logger = logger; this.#name = name; + + if (ScopedLogger.isScopedLogger(logger)) + { + this._scope = [...logger._scope, name]; + } else + { + this._scope = [name]; + } + } + + static isScopedLogger(subject: T | ScopedLogger): subject is ScopedLogger + { + return subject["_scope"] !== undefined; } - debug(msg: string): void + debug(msg: string, ...args: unknown[]): void { this.#ensureNotDisposed(); - this.#logger.debug(this.#prepareMessage(msg)); + this.#logger.debug(this.#prepareMessage(msg), ...args); } - debugError(err: Error): void; - debugError(err: Error, msg: string): void; - debugError(err: Error, msg?: string): void + debugError(err: Error, msg?: string, ...args: unknown[]): void { this.#ensureNotDisposed(); - this.#logger.debugError(err, this.#prepareMessage(msg)); + this.#logger.debugError(err, this.#prepareMessage(msg), ...args); } - error(msg: string): void + error(msg: string, ...args: unknown[]): void { this.#ensureNotDisposed(); - this.#logger.error(this.#prepareMessage(msg)); + this.#logger.error(this.#prepareMessage(msg), ...args); } - errorError(err: Error): void; - errorError(err: Error, msg: string): void; - errorError(err: Error, msg?: string): void + errorError(err: Error, msg?: string, ...args: unknown[]): void { this.#ensureNotDisposed(); - this.#logger.errorError(err, this.#prepareMessage(msg)); + this.#logger.errorError(err, this.#prepareMessage(msg), ...args); } - info(msg: string): void + info(msg: string, ...args: unknown[]): void { this.#ensureNotDisposed(); - this.#logger.info(this.#prepareMessage(msg)); + this.#logger.info(this.#prepareMessage(msg), ...args); } - infoError(err: Error): void; - infoError(err: Error, msg: string): void - infoError(err: Error, msg?: string): void + infoError(err: Error, msg?: string, ...args: unknown[]): void { this.#ensureNotDisposed(); - this.#logger.infoError(err, this.#prepareMessage(msg)); + this.#logger.infoError(err, this.#prepareMessage(msg), ...args); } - trace(msg: string): void + trace(msg: string, ...args: unknown[]): void { this.#ensureNotDisposed(); - this.#logger.trace(this.#prepareMessage(msg)); + this.#logger.trace(this.#prepareMessage(msg), ...args); } - traceError(err: Error): void; - traceError(err: Error, msg: string): void - traceError(err: Error, msg?: string): void + traceError(err: Error, msg?: string, ...args: unknown[]): void { this.#ensureNotDisposed(); - this.#logger.traceError(err, this.#prepareMessage(msg)); + this.#logger.traceError(err, this.#prepareMessage(msg), ...args); } - warn(msg: string): void + warn(msg: string, ...args: unknown[]): void { this.#ensureNotDisposed(); - this.#logger.warn(this.#prepareMessage(msg)); + this.#logger.warn(this.#prepareMessage(msg), ...args); } - warnError(err: Error): void; - warnError(err: Error, msg: string): void - warnError(err: Error, msg?: string): void + warnError(err: Error, msg?: string, ...args: unknown[]): void { this.#ensureNotDisposed(); - this.#logger.warnError(err, this.#prepareMessage(msg)); + this.#logger.warnError(err, this.#prepareMessage(msg), ...args); } scope(name: string): ILogger & IDisposable @@ -90,7 +97,7 @@ export default class ScopedLogger implements ILogger, IDisposable this.#ensureNotDisposed(); return new ScopedLogger(this, name); } - + dispose(): void { this.#ensureNotDisposed(); @@ -99,14 +106,14 @@ export default class ScopedLogger implements ILogger, IDisposable #prepareMessage(msg?: string): string { - return `[${this.#name}]${msg !== undefined ? ' ' : ''}${msg || null}`; + return `${ScopedLogger.openScope}${this.#name}${ScopedLogger.closeScope}${msg !== undefined ? ' ' : ''}${msg || null}`; } - - #ensureNotDisposed():void + + #ensureNotDisposed(): void { if (this.#disposed) { throw new Error(SR.objectIsDisposed); } } -} \ No newline at end of file +} diff --git a/src/Logging/WebApiConsoleLogger.ts b/src/Logging/WebApiConsoleLogger.ts new file mode 100644 index 00000000..54dd48e4 --- /dev/null +++ b/src/Logging/WebApiConsoleLogger.ts @@ -0,0 +1,41 @@ +import { ILogger, ILoggerConfig } from './_types'; +import Logger from './Logger'; + +export default class WebApiConsoleLogger extends Logger implements ILogger +{ + readonly #console: Console; + + constructor(console: Console, config?: ILoggerConfig) + { + super(config); + this.#console = console; + } + + protected _debugMethod(message: string, ...args: unknown[]): void + { + return this.#console.debug(message, ...args); + } + protected _defaultMethod(message: string, ...args: unknown[]): void + { + return this.#console.log(message, ...args); + } + protected _errorMethod(message: string, ...args: unknown[]): void + { + return this.#console.error(message, ...args); + } + protected _infoMethod(message: string, ...args: unknown[]): void + { + return this.#console.info(message, ...args); + } + protected _traceMethod(message: string, ...args: unknown[]): void + { + return this.#console.trace(message, ...args); + } + protected _warnMethod(message: string, ...args: unknown[]): void + { + return this.#console.warn(message, ...args); + } +} + + + diff --git a/src/Logging/_types.ts b/src/Logging/_types.ts index 2a6968f8..b9295b36 100644 --- a/src/Logging/_types.ts +++ b/src/Logging/_types.ts @@ -3,7 +3,7 @@ /** The log level. Used for filtering and tagging logging events */ export enum LogLevel { - /** Debug. Should only be used for logging to help development */ + /** Debug. Should only be used for logging to help development. Debug is the lowest level. */ Debug = "DEBG", /** Error. Used to log out errors and exceptions */ Error = "EROR", @@ -21,81 +21,81 @@ export interface ILogger * Log a message flagged at the Debug log level * @param msg */ - debug(msg: string): void; + debug(msg: string, ...args: unknown[]): void; /** * Log an error flagged at the Debug log level * @param err */ - debugError(err: Error): void; + debugError(err: Error, ...args: unknown[]): void; /** * Log an error and a message flagged at the Debug log level * @param err */ - debugError(err: Error, msg: string): void; + debugError(err: Error, msg: string, ...args: unknown[]): void; /** * Log a message flagged at the Error log level * @param msg */ - error(msg: string): void; + error(msg: string, ...args: unknown[]): void; /** * Log an error flagged at the Error log level * @param err */ - errorError(err: Error): void; + errorError(err: Error, ...args: unknown[]): void; /** * Log an error and a message flagged at the Error log level * @param err */ - errorError(err: Error, msg: string): void; + errorError(err: Error, msg: string, ...args: unknown[]): void; /** * Log a message flagged at the Informational log level * @param msg */ - info(msg: string): void; + info(msg: string, ...args: unknown[]): void; /** * Log an error flagged at the Informational log level * @param err */ - infoError(err: Error): void; + infoError(err: Error, ...args: unknown[]): void; /** * Log an error and a message flagged at the Informational log level * @param err */ - infoError(err: Error, msg: string): void; + infoError(err: Error, msg: string, ...args: unknown[]): void; /** * Log a message flagged at the Trace log level * @param msg */ - trace(msg: string): void; + trace(msg: string, ...args: unknown[]): void; /** * Log an error flagged at the Trace log level * @param err */ - traceError(err: Error): void; + traceError(err: Error, ...args: unknown[]): void; /** * Log an error and a message flagged at the Trace log level * @param err */ - traceError(err: Error, msg: string): void; + traceError(err: Error, msg: string, ...args: unknown[]): void; /** * Log a message flagged at the Warning log level * @param msg */ - warn(msg: string): void; + warn(msg: string, ...args: unknown[]): void; /** * Log an error flagged at the Warning log level * @param err */ - warnError(err: Error): void; + warnError(err: Error, ...args: unknown[]): void; /** * Log an error and a message flagged at the Warning log level * @param err */ - warnError(err: Error, msg: string): void; + warnError(err: Error, msg: string, ...args: unknown[]): void; /** * Rescope the logger with the provided name. Used for tagging the logger. diff --git a/src/Logging/index.ts b/src/Logging/index.ts index 22ac30fe..a406b899 100644 --- a/src/Logging/index.ts +++ b/src/Logging/index.ts @@ -1,9 +1,9 @@ export * from './_types'; -export * from './defaultLogger'; +export * from './utils/defaultLogger'; export { default as Logger } from './Logger'; export { default as AggregateLogger } from './AggregateLogger'; export { default as ScopedLogger } from './ScopedLogger'; export { default as ConsoleLogger } from './ConsoleLogger'; +export { default as WebApiConsoleLogger } from './WebApiConsoleLogger'; -export { default as loggerScope } from './loggerScope'; -export { default as testLogVerbosity } from './testLogVerbosity'; +export { default as testLogVerbosity } from './utils/testLogVerbosity'; diff --git a/src/Logging/defaultLogger.ts b/src/Logging/utils/defaultLogger.ts similarity index 86% rename from src/Logging/defaultLogger.ts rename to src/Logging/utils/defaultLogger.ts index e76e1e75..7446ec09 100644 --- a/src/Logging/defaultLogger.ts +++ b/src/Logging/utils/defaultLogger.ts @@ -1,5 +1,5 @@ -import noop from '../Utilities/noop'; -import { ILogger } from './_types'; +import noop from '../../Utilities/noop'; +import { ILogger } from '../_types'; export const NullLogger: ILogger = Object.freeze({ debug: noop, diff --git a/src/Logging/utils/errorAndMessageToString.ts b/src/Logging/utils/errorAndMessageToString.ts new file mode 100644 index 00000000..21ccda2a --- /dev/null +++ b/src/Logging/utils/errorAndMessageToString.ts @@ -0,0 +1,21 @@ +import { IndentedStringBuilder } from '../../IO'; +import errorToLogMessage from '../../errorToLogMessage'; + +export default function errorAndMessageToString(msg: string | undefined, err: Error): string +{ + const sb = new IndentedStringBuilder(0); + if (msg !== undefined) + { + sb.appendLine(msg); + sb.indent(); + errorToLogMessage(err, sb); + sb.unindent(); + } + + + else + { + errorToLogMessage(err, sb); + } + return sb.toString(); +} diff --git a/src/Logging/testLogVerbosity.ts b/src/Logging/utils/testLogVerbosity.ts similarity index 96% rename from src/Logging/testLogVerbosity.ts rename to src/Logging/utils/testLogVerbosity.ts index eb8ee33c..5408fb7f 100644 --- a/src/Logging/testLogVerbosity.ts +++ b/src/Logging/utils/testLogVerbosity.ts @@ -1,4 +1,4 @@ -import { LogLevel } from './_types'; +import { LogLevel } from '../_types'; /** * Returns true if the desired log level is valid for the currently configured logging verbosity diff --git a/src/index.ts b/src/index.ts index b34e7107..cbf80fbb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -53,3 +53,4 @@ export { default as errorToLogMessage } from './errorToLogMessage'; export { default as serializeError } from './serializeError'; export { default as using } from './using'; export { default as usingAsync } from './usingAsync'; +export { default as loggerScope } from './loggerScope'; diff --git a/src/loggerScope.ts b/src/loggerScope.ts new file mode 100644 index 00000000..36895d53 --- /dev/null +++ b/src/loggerScope.ts @@ -0,0 +1,18 @@ +import { ILogger } from './Logging/_types'; + +/** + * Decorator for setting the logger scope of an ILogger instance + * @param name + */ +export default function loggerScope(name: string): (target: Record, key: string) => void +{ + return (target: Record, key: string) => + { + const logger = target[key] as ILogger; + if (!logger) + { + throw "logger is no set yet"; + } + target[key] = logger.scope(name); + }; +}