diff --git a/src/logging/NullLogger.js b/src/logging/NullLogger.js index 0842e4c0..b6e47d59 100644 --- a/src/logging/NullLogger.js +++ b/src/logging/NullLogger.js @@ -15,16 +15,31 @@ limitations under the License. */ import {LogLevel} from "./LogFilter.js"; -// TODO: add missing methods +function noop () {} + +export const Instance = new NullLogger(); + export class NullLogger { constructor() { - this._item = new NullLogItem(); + this.item = new NullLogItem(); } log() {} run(_, callback) { - return callback(this._item); + return callback(this.item); + } + + wrapOrRun(item, _, callback) { + if (item) { + item.wrap(null, callback); + } else { + this.run(null, callback); + } + } + + runDetached(_, callback) { + new Promise(r => r(callback(this.item))).then(noop, noop); } async export() { @@ -42,12 +57,29 @@ class NullLogItem { } log() {} set() {} - anonymize() {} + + runDetached(_, callback) { + new Promise(r => r(callback(this))).then(noop, noop); + } + + wrapDetached(_, callback) { + return this.refDetached(null, callback); + } + + run(callback) { + return callback(this); + } + + refDetached() {} get level() { return LogLevel; } + get duration() { + return 0; + } + catch(err) { return err; } diff --git a/src/logging/utils.js b/src/logging/utils.js new file mode 100644 index 00000000..3da9aa5d --- /dev/null +++ b/src/logging/utils.js @@ -0,0 +1,16 @@ +// these are helper functions if you can't assume you always have a log item (e.g. some code paths call with one set, others don't) +// if you know you always have a log item, better to use the methods on the log item than these utility functions. + +import {Instance as NullLoggerInstance} from "./NullLogger.js"; + +export function wrapOrRunNullLogger(logItem, labelOrValues, callback, logLevel = null, filterCreator = null) { + if (logItem) { + return logItem.wrap(logItem, labelOrValues, callback, logLevel, filterCreator); + } else { + return NullLoggerInstance.run(null, callback); + } +} + +export function ensureLogItem(logItem) { + return logItem || NullLoggerInstance.item; +}