hydrogen-web/src/logging/LogItem.js

173 lines
5 KiB
JavaScript
Raw Normal View History

2021-02-12 01:37:18 +05:30
/*
Copyright 2020 Bruno Windels <bruno@windels.cloud>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import {LogLevel} from "./LogLevel.js";
2021-02-12 01:37:18 +05:30
export class LogItem {
2021-02-12 20:38:07 +05:30
constructor(labelOrValues, logLevel, platform, anonymize) {
2021-02-12 17:34:05 +05:30
this._platform = platform;
2021-02-12 20:38:07 +05:30
this._anonymize = anonymize;
2021-02-12 17:34:05 +05:30
this._start = platform.clock.now();
2021-02-12 01:37:18 +05:30
this._end = null;
2021-02-12 17:34:05 +05:30
this._values = typeof labelOrValues === "string" ? {label: labelOrValues} : labelOrValues;
2021-02-12 01:37:18 +05:30
this._error = null;
this._children = [];
this._logLevel = logLevel;
}
2021-02-12 17:34:05 +05:30
/**
* Creates a new child item and runs it in `callback`.
*/
wrap(labelOrValues, callback, logLevel = this._logLevel) {
const item = this._createChild(labelOrValues, logLevel);
return item.run(callback);
}
/**
* Creates a new child item that finishes immediately
* and can hence not be modified anymore.
*
* Hence, the child item is not returned.
*/
log(labelOrValues, logLevel = this._logLevel) {
const item = this._createChild(labelOrValues, logLevel);
item.end = item.start;
2021-02-12 01:37:18 +05:30
}
set(key, value) {
if(typeof key === "object") {
const values = key;
Object.assign(this._values, values);
} else {
this._values[key] = value;
}
}
2021-02-12 17:34:05 +05:30
anonymize(value) {
2021-02-12 20:38:07 +05:30
if (this._anonymize) {
const buffer = this._platform.crypto.digest("SHA-256", value);
return this._platform.encoding.base64.encode(buffer);
} else {
return value;
}
2021-02-12 01:37:18 +05:30
}
serialize(logLevel) {
const children = this._children.reduce((array, c) => {
const s = c.serialize(logLevel);
if (s) {
array = array || [];
array.push(s);
}
return array;
}, null);
// neither our children or us have a loglevel high enough, bail out.
if (!children && this._logLevel < logLevel) {
return null;
}
let error = null;
2021-02-12 01:37:18 +05:30
if (this._error) {
error = {
message: this._error.message,
stack: this._error.stack,
};
}
return {
start: this._start,
end: this._end,
values: this._values,
error,
children,
2021-02-12 01:37:18 +05:30
logLevel: this._logLevel
};
}
2021-02-12 17:34:05 +05:30
/**
* You probably want to use `wrap` instead of this.
*
* Runs a callback passing this log item,
* recording the timing and any error.
*
* callback can return a Promise.
*
* Can only be called once, and will throw after.
*
* @param {Function} callback [description]
* @return {[type]} [description]
*/
run(callback) {
if (this._end !== null) {
throw new Error("item is finished");
}
let result;
2021-02-12 01:37:18 +05:30
try {
2021-02-12 17:34:05 +05:30
result = callback(this);
if (result instanceof Promise) {
return result.then(promiseResult => {
this.finish();
2021-02-12 17:34:05 +05:30
return promiseResult;
}, err => {
this._catch(err);
this.finish();
2021-02-12 17:34:05 +05:30
throw err;
});
} else {
this.finish();
2021-02-12 17:34:05 +05:30
return result;
}
2021-02-12 01:37:18 +05:30
} catch (err) {
2021-02-12 17:34:05 +05:30
this._catch(err);
this.finish();
2021-02-12 01:37:18 +05:30
throw err;
}
}
2021-02-12 17:34:05 +05:30
/**
* finished the item, recording the end time. After finishing, an item can't be modified anymore as it will be persisted.
* @internal shouldn't typically be called by hand. allows to force finish if a promise is still running when closing the app
*/
finish() {
if (this._end === null) {
for(const c of this._children) {
c.finish();
}
this._end = this._platform.clock.now();
}
}
2021-02-12 20:38:07 +05:30
// expose log level without needing
get level() {
return LogLevel;
}
2021-02-12 17:34:05 +05:30
_catch(err) {
this._error = err;
this._logLevel = LogLevel.Error;
2021-02-12 17:34:05 +05:30
console.error(`log item ${this.values.label} failed: ${err.message}:\n${err.stack}`);
}
_createChild(labelOrValues, logLevel) {
if (this._end !== null) {
throw new Error("item is finished");
}
2021-02-12 20:38:07 +05:30
const item = new LogItem(labelOrValues, logLevel, this._platform, this._anonymize);
2021-02-12 17:34:05 +05:30
this._children.push(item);
return item;
}
2021-02-12 01:37:18 +05:30
}