From bba44abf529f665ad47e94e56d90c47fe96fba97 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Sun, 14 Nov 2021 16:24:16 +0530 Subject: [PATCH] Convert console logger to ts --- .../{ConsoleLogger.js => ConsoleLogger.ts} | 44 ++++++++++--------- src/logging/LogItem.ts | 10 ++++- 2 files changed, 32 insertions(+), 22 deletions(-) rename src/logging/{ConsoleLogger.js => ConsoleLogger.ts} (58%) diff --git a/src/logging/ConsoleLogger.js b/src/logging/ConsoleLogger.ts similarity index 58% rename from src/logging/ConsoleLogger.js rename to src/logging/ConsoleLogger.ts index 4d3be73b..5b709bb1 100644 --- a/src/logging/ConsoleLogger.js +++ b/src/logging/ConsoleLogger.ts @@ -14,35 +14,37 @@ See the License for the specific language governing permissions and limitations under the License. */ import {BaseLogger} from "./BaseLogger"; +import type {LogItem, LogItemValues} from "./LogItem"; export class ConsoleLogger extends BaseLogger { - _persistItem(item) { + _persistItem(item: LogItem): void { printToConsole(item); } - export() { + export(): void { throw new Error("Cannot export from ConsoleLogger"); } } const excludedKeysFromTable = ["l", "id"]; -function filterValues(values) { +function filterValues(values: LogItemValues): LogItemValues | null { if (!values) { + // todo: is this check here unnecessary because LogItem will always have values? return null; } return Object.entries(values) .filter(([key]) => !excludedKeysFromTable.includes(key)) - .reduce((obj, [key, value]) => { + .reduce((obj: LogItemValues, [key, value]) => { obj = obj || {}; obj[key] = value; return obj; }, null); } -function printToConsole(item) { +function printToConsole(item: LogItem): void { const label = `${itemCaption(item)} (${item.duration}ms)`; - const filteredValues = filterValues(item._values); - const shouldGroup = item._children || filteredValues; + const filteredValues = filterValues(item.values); + const shouldGroup = item.children || filteredValues; if (shouldGroup) { if (item.error) { console.group(label); @@ -62,8 +64,8 @@ function printToConsole(item) { if (filteredValues) { console.table(filteredValues); } - if (item._children) { - for(const c of item._children) { + if (item.children) { + for(const c of item.children) { printToConsole(c); } } @@ -72,18 +74,18 @@ function printToConsole(item) { } } -function itemCaption(item) { - if (item._values.t === "network") { - return `${item._values.method} ${item._values.url}`; - } else if (item._values.l && typeof item._values.id !== "undefined") { - return `${item._values.l} ${item._values.id}`; - } else if (item._values.l && typeof item._values.status !== "undefined") { - return `${item._values.l} (${item._values.status})`; - } else if (item._values.l && item.error) { - return `${item._values.l} failed`; - } else if (typeof item._values.ref !== "undefined") { - return `ref ${item._values.ref}`; +function itemCaption(item: LogItem): string { + if (item.values.t === "network") { + return `${item.values.method} ${item.values.url}`; + } else if (item.values.l && typeof item.values.id !== "undefined") { + return `${item.values.l} ${item.values.id}`; + } else if (item.values.l && typeof item.values.status !== "undefined") { + return `${item.values.l} (${item.values.status})`; + } else if (item.values.l && item.error) { + return `${item.values.l} failed`; + } else if (typeof item.values.ref !== "undefined") { + return `ref ${item.values.ref}`; } else { - return item._values.l || item._values.type; + return item.values.l || item.values.type; } } diff --git a/src/logging/LogItem.ts b/src/logging/LogItem.ts index 716a3e73..051b0353 100644 --- a/src/logging/LogItem.ts +++ b/src/logging/LogItem.ts @@ -33,7 +33,7 @@ interface ISerializedItem { c?: Array; }; -type LogItemValues = { +export type LogItemValues = { l?: string; t?: string; id?: unknown; @@ -285,4 +285,12 @@ export class LogItem { get logger(): BaseLogger { return this._logger; } + + get values(): LogItemValues { + return this._values; + } + + get children(): Array | null { + return this._children; + } }