From c0a03858ebc14f98a8cd1ca19fd0b61d9fd5f563 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 12 Feb 2021 16:08:07 +0100 Subject: [PATCH] add option to anonymize logged values --- src/logging/BaseLogger.js | 7 ++++--- src/logging/IDBLogger.js | 7 ++++--- src/logging/LogItem.js | 15 ++++++++++----- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/logging/BaseLogger.js b/src/logging/BaseLogger.js index 772b2c62..6b2f433a 100644 --- a/src/logging/BaseLogger.js +++ b/src/logging/BaseLogger.js @@ -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; diff --git a/src/logging/IDBLogger.js b/src/logging/IDBLogger.js index c3a2ac1f..4ea746ac 100644 --- a/src/logging/IDBLogger.js +++ b/src/logging/IDBLogger.js @@ -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; } diff --git a/src/logging/LogItem.js b/src/logging/LogItem.js index 7fb659ee..a63aacbb 100644 --- a/src/logging/LogItem.js +++ b/src/logging/LogItem.js @@ -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; }