forked from mystiq/hydrogen-web
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:
parent
7ba979eee6
commit
744ac6b885
2 changed files with 52 additions and 4 deletions
|
@ -15,16 +15,31 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
import {LogLevel} from "./LogFilter.js";
|
import {LogLevel} from "./LogFilter.js";
|
||||||
|
|
||||||
// TODO: add missing methods
|
function noop () {}
|
||||||
|
|
||||||
|
export const Instance = new NullLogger();
|
||||||
|
|
||||||
export class NullLogger {
|
export class NullLogger {
|
||||||
constructor() {
|
constructor() {
|
||||||
this._item = new NullLogItem();
|
this.item = new NullLogItem();
|
||||||
}
|
}
|
||||||
|
|
||||||
log() {}
|
log() {}
|
||||||
|
|
||||||
run(_, callback) {
|
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() {
|
async export() {
|
||||||
|
@ -42,12 +57,29 @@ class NullLogItem {
|
||||||
}
|
}
|
||||||
log() {}
|
log() {}
|
||||||
set() {}
|
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() {
|
get level() {
|
||||||
return LogLevel;
|
return LogLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get duration() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
catch(err) {
|
catch(err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
16
src/logging/utils.js
Normal file
16
src/logging/utils.js
Normal 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;
|
||||||
|
}
|
Loading…
Reference in a new issue