implement latest api changes in null logger, and add ensureLogItem

to ensure log item either by taking an existing one or taking one
from the null logger
This commit is contained in:
Bruno Windels 2021-03-15 12:55:46 +01:00
parent 7ba979eee6
commit 744ac6b885
2 changed files with 52 additions and 4 deletions

View file

@ -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;
}

16
src/logging/utils.js Normal file
View file

@ -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;
}