support referencing other log items that are detached (fire & forget)

for async tasks that are not awaited
This commit is contained in:
Bruno Windels 2021-02-23 15:27:55 +01:00
parent b2621b3001
commit ed1b37d251
2 changed files with 55 additions and 10 deletions

View file

@ -24,13 +24,39 @@ export class BaseLogger {
} }
log(labelOrValues, logLevel = LogLevel.Info) { log(labelOrValues, logLevel = LogLevel.Info) {
const item = new LogItem(labelOrValues, logLevel, null, this._platform.clock); const item = new LogItem(labelOrValues, logLevel, null, this);
item._end = item._start; item._end = item._start;
this._persistItem(item.serialize(null)); this._persistItem(item.serialize(null));
} }
run(labelOrValues, callback, logLevel = LogLevel.Info, filterCreator = null) { wrapOrRun(item, labelOrValues, callback, logLevel = null, filterCreator = null) {
const item = new LogItem(labelOrValues, logLevel, null, this._platform.clock); if (item) {
return item.wrap(labelOrValues, callback, logLevel, filterCreator);
} else {
return this.run(labelOrValues, callback, logLevel, filterCreator);
}
}
runDetached(labelOrValues, callback, logLevel = null, filterCreator = null) {
if (logLevel === null) {
logLevel = LogLevel.Info;
}
const item = new LogItem(labelOrValues, logLevel, null, this);
const refId = Math.round(this._platform.random() * Number.MAX_SAFE_INTEGER);
item.set("refId", refId);
this._run(item, callback, logLevel, filterCreator, false /* don't throw, nobody is awaiting */);
return item;
}
run(labelOrValues, callback, logLevel = null, filterCreator = null) {
if (logLevel === null) {
logLevel = LogLevel.Info;
}
const item = new LogItem(labelOrValues, logLevel, null, this);
return this._run(item, callback, logLevel, filterCreator, true);
}
_run(item, callback, logLevel, filterCreator, shouldThrow) {
this._openItems.add(item); this._openItems.add(item);
const finishItem = () => { const finishItem = () => {
@ -64,7 +90,9 @@ export class BaseLogger {
return promiseResult; return promiseResult;
}, err => { }, err => {
finishItem(); finishItem();
throw err; if (shouldThrow) {
throw err;
}
}); });
} else { } else {
finishItem(); finishItem();
@ -72,7 +100,9 @@ export class BaseLogger {
} }
} catch (err) { } catch (err) {
finishItem(); finishItem();
throw err; if (shouldThrow) {
throw err;
}
} }
} }
@ -106,4 +136,8 @@ export class BaseLogger {
get level() { get level() {
return LogLevel; return LogLevel;
} }
_now() {
return this._platform.clock.now();
}
} }

View file

@ -17,9 +17,9 @@ limitations under the License.
import {LogLevel, LogFilter} from "./LogFilter.js"; import {LogLevel, LogFilter} from "./LogFilter.js";
export class LogItem { export class LogItem {
constructor(labelOrValues, logLevel, filterCreator, clock) { constructor(labelOrValues, logLevel, filterCreator, logger) {
this._clock = clock; this._logger = logger;
this._start = clock.now(); this._start = logger._now();
this._end = null; this._end = null;
// (l)abel // (l)abel
this._values = typeof labelOrValues === "string" ? {l: labelOrValues} : labelOrValues; this._values = typeof labelOrValues === "string" ? {l: labelOrValues} : labelOrValues;
@ -29,6 +29,14 @@ export class LogItem {
this._filterCreator = filterCreator; this._filterCreator = filterCreator;
} }
runDetached(labelOrValues, callback, logLevel, filterCreator) {
return this._logger.runDetached(labelOrValues, callback, logLevel, filterCreator);
}
wrapDetached(labelOrValues, callback, logLevel, filterCreator) {
this.refDetached(this.runDetached(labelOrValues, callback, logLevel, filterCreator));
}
/** /**
* Creates a new child item and runs it in `callback`. * Creates a new child item and runs it in `callback`.
*/ */
@ -70,6 +78,9 @@ export class LogItem {
log(labelOrValues, logLevel = null) { log(labelOrValues, logLevel = null) {
const item = this.child(labelOrValues, logLevel, null); const item = this.child(labelOrValues, logLevel, null);
item.end = item.start; item.end = item.start;
refDetached(logItem, logLevel = null) {
return this.log({ref: logItem._values.refId}, logLevel);
} }
set(key, value) { set(key, value) {
@ -177,7 +188,7 @@ export class LogItem {
c.finish(); c.finish();
} }
} }
this._end = this._clock.now(); this._end = this._logger._now();
} }
} }
@ -200,7 +211,7 @@ export class LogItem {
if (!logLevel) { if (!logLevel) {
logLevel = this.logLevel || LogLevel.Info; logLevel = this.logLevel || LogLevel.Info;
} }
const item = new LogItem(labelOrValues, logLevel, filterCreator, this._clock); const item = new LogItem(labelOrValues, logLevel, filterCreator, this._logger);
if (this._children === null) { if (this._children === null) {
this._children = []; this._children = [];
} }