forked from mystiq/hydrogen-web
add option to anonymize logged values
This commit is contained in:
parent
bbab1e9ecc
commit
c0a03858eb
3 changed files with 18 additions and 11 deletions
|
@ -18,14 +18,15 @@ import {LogItem} from "./LogItem.js";
|
||||||
import {LogLevel} from "./LogLevel.js";
|
import {LogLevel} from "./LogLevel.js";
|
||||||
|
|
||||||
export class BaseLogger {
|
export class BaseLogger {
|
||||||
constructor(platform, baseLogLevel) {
|
constructor({platform, baseLogLevel, anonymize}) {
|
||||||
this._openItems = new Set();
|
this._openItems = new Set();
|
||||||
this._platform = platform;
|
this._platform = platform;
|
||||||
|
this._anonymize = anonymize;
|
||||||
this._baseLogLevel = baseLogLevel;
|
this._baseLogLevel = baseLogLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
wrapLog(labelOrValues, callback, logLevel = this._baseLogLevel) {
|
wrapLog(labelOrValues, callback, logLevel = this._baseLogLevel) {
|
||||||
const item = new LogItem(labelOrValues, logLevel, this._platform);
|
const item = new LogItem(labelOrValues, logLevel, this._platform, this._anonymize);
|
||||||
|
|
||||||
const finishItem = () => {
|
const finishItem = () => {
|
||||||
const serialized = item.serialize(this._baseLogLevel);
|
const serialized = item.serialize(this._baseLogLevel);
|
||||||
|
|
|
@ -25,8 +25,9 @@ import {
|
||||||
import {BaseLogger} from "./BaseLogger.js";
|
import {BaseLogger} from "./BaseLogger.js";
|
||||||
|
|
||||||
export class IDBLogger extends BaseLogger {
|
export class IDBLogger extends BaseLogger {
|
||||||
constructor({name, platform, flushInterval = 2 * 60 * 1000, limit = 3000}) {
|
constructor(options) {
|
||||||
super(platform);
|
super(options);
|
||||||
|
const {name, flushInterval = 2 * 60 * 1000, limit = 3000} = options;
|
||||||
this._name = name;
|
this._name = name;
|
||||||
this._limit = limit;
|
this._limit = limit;
|
||||||
// does not get loaded from idb on startup as we only use it to
|
// does not get loaded from idb on startup as we only use it to
|
||||||
|
@ -175,7 +176,7 @@ class IDBLogExport {
|
||||||
items: this._items
|
items: this._items
|
||||||
};
|
};
|
||||||
const json = JSON.stringify(log);
|
const json = JSON.stringify(log);
|
||||||
const buffer = this._platform.utf8.encode(json);
|
const buffer = this._platform.encoding.utf8.encode(json);
|
||||||
const blob = this._platform.createBlob(buffer, "application/json");
|
const blob = this._platform.createBlob(buffer, "application/json");
|
||||||
return blob;
|
return blob;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,8 +17,9 @@ limitations under the License.
|
||||||
import {LogLevel} from "./LogLevel.js";
|
import {LogLevel} from "./LogLevel.js";
|
||||||
|
|
||||||
export class LogItem {
|
export class LogItem {
|
||||||
constructor(labelOrValues, logLevel, platform) {
|
constructor(labelOrValues, logLevel, platform, anonymize) {
|
||||||
this._platform = platform;
|
this._platform = platform;
|
||||||
|
this._anonymize = anonymize;
|
||||||
this._start = platform.clock.now();
|
this._start = platform.clock.now();
|
||||||
this._end = null;
|
this._end = null;
|
||||||
this._values = typeof labelOrValues === "string" ? {label: labelOrValues} : labelOrValues;
|
this._values = typeof labelOrValues === "string" ? {label: labelOrValues} : labelOrValues;
|
||||||
|
@ -56,8 +57,12 @@ export class LogItem {
|
||||||
}
|
}
|
||||||
|
|
||||||
anonymize(value) {
|
anonymize(value) {
|
||||||
const buffer = this._platform.crypto.digest("SHA-256", value);
|
if (this._anonymize) {
|
||||||
return this._platform.base64.encode(buffer);
|
const buffer = this._platform.crypto.digest("SHA-256", value);
|
||||||
|
return this._platform.encoding.base64.encode(buffer);
|
||||||
|
} else {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
serialize(logLevel) {
|
serialize(logLevel) {
|
||||||
|
@ -160,7 +165,7 @@ export class LogItem {
|
||||||
if (this._end !== null) {
|
if (this._end !== null) {
|
||||||
throw new Error("item is finished");
|
throw new Error("item is finished");
|
||||||
}
|
}
|
||||||
const item = new LogItem(labelOrValues, logLevel, this._platform);
|
const item = new LogItem(labelOrValues, logLevel, this._platform, this._anonymize);
|
||||||
this._children.push(item);
|
this._children.push(item);
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue