add option to anonymize logged values

This commit is contained in:
Bruno Windels 2021-02-12 16:08:07 +01:00
parent bbab1e9ecc
commit c0a03858eb
3 changed files with 18 additions and 11 deletions

View File

@ -18,14 +18,15 @@ import {LogItem} from "./LogItem.js";
import {LogLevel} from "./LogLevel.js";
export class BaseLogger {
constructor(platform, baseLogLevel) {
constructor({platform, baseLogLevel, anonymize}) {
this._openItems = new Set();
this._platform = platform;
this._anonymize = anonymize;
this._baseLogLevel = 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 serialized = item.serialize(this._baseLogLevel);
@ -73,7 +74,7 @@ export class BaseLogger {
async export() {
throw new Error("not implemented");
}
// expose log level without needing
get level() {
return LogLevel;

View File

@ -25,8 +25,9 @@ import {
import {BaseLogger} from "./BaseLogger.js";
export class IDBLogger extends BaseLogger {
constructor({name, platform, flushInterval = 2 * 60 * 1000, limit = 3000}) {
super(platform);
constructor(options) {
super(options);
const {name, flushInterval = 2 * 60 * 1000, limit = 3000} = options;
this._name = name;
this._limit = limit;
// does not get loaded from idb on startup as we only use it to
@ -175,7 +176,7 @@ class IDBLogExport {
items: this._items
};
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");
return blob;
}

View File

@ -17,8 +17,9 @@ limitations under the License.
import {LogLevel} from "./LogLevel.js";
export class LogItem {
constructor(labelOrValues, logLevel, platform) {
constructor(labelOrValues, logLevel, platform, anonymize) {
this._platform = platform;
this._anonymize = anonymize;
this._start = platform.clock.now();
this._end = null;
this._values = typeof labelOrValues === "string" ? {label: labelOrValues} : labelOrValues;
@ -56,8 +57,12 @@ export class LogItem {
}
anonymize(value) {
const buffer = this._platform.crypto.digest("SHA-256", value);
return this._platform.base64.encode(buffer);
if (this._anonymize) {
const buffer = this._platform.crypto.digest("SHA-256", value);
return this._platform.encoding.base64.encode(buffer);
} else {
return value;
}
}
serialize(logLevel) {
@ -144,7 +149,7 @@ export class LogItem {
this._end = this._platform.clock.now();
}
}
// expose log level without needing
get level() {
return LogLevel;
@ -160,7 +165,7 @@ export class LogItem {
if (this._end !== null) {
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);
return item;
}