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"; 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);
@ -73,7 +74,7 @@ export class BaseLogger {
async export() { async export() {
throw new Error("not implemented"); throw new Error("not implemented");
} }
// expose log level without needing // expose log level without needing
get level() { get level() {
return LogLevel; return LogLevel;

View file

@ -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;
} }

View file

@ -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) {
@ -144,7 +149,7 @@ export class LogItem {
this._end = this._platform.clock.now(); this._end = this._platform.clock.now();
} }
} }
// expose log level without needing // expose log level without needing
get level() { get level() {
return LogLevel; return 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;
} }