Convert console logger to ts

This commit is contained in:
RMidhunSuresh 2021-11-14 16:24:16 +05:30
parent 39d0708cca
commit bba44abf52
2 changed files with 32 additions and 22 deletions

View file

@ -14,35 +14,37 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import {BaseLogger} from "./BaseLogger"; import {BaseLogger} from "./BaseLogger";
import type {LogItem, LogItemValues} from "./LogItem";
export class ConsoleLogger extends BaseLogger { export class ConsoleLogger extends BaseLogger {
_persistItem(item) { _persistItem(item: LogItem): void {
printToConsole(item); printToConsole(item);
} }
export() { export(): void {
throw new Error("Cannot export from ConsoleLogger"); throw new Error("Cannot export from ConsoleLogger");
} }
} }
const excludedKeysFromTable = ["l", "id"]; const excludedKeysFromTable = ["l", "id"];
function filterValues(values) { function filterValues(values: LogItemValues): LogItemValues | null {
if (!values) { if (!values) {
// todo: is this check here unnecessary because LogItem will always have values?
return null; return null;
} }
return Object.entries(values) return Object.entries(values)
.filter(([key]) => !excludedKeysFromTable.includes(key)) .filter(([key]) => !excludedKeysFromTable.includes(key))
.reduce((obj, [key, value]) => { .reduce((obj: LogItemValues, [key, value]) => {
obj = obj || {}; obj = obj || {};
obj[key] = value; obj[key] = value;
return obj; return obj;
}, null); }, null);
} }
function printToConsole(item) { function printToConsole(item: LogItem): void {
const label = `${itemCaption(item)} (${item.duration}ms)`; const label = `${itemCaption(item)} (${item.duration}ms)`;
const filteredValues = filterValues(item._values); const filteredValues = filterValues(item.values);
const shouldGroup = item._children || filteredValues; const shouldGroup = item.children || filteredValues;
if (shouldGroup) { if (shouldGroup) {
if (item.error) { if (item.error) {
console.group(label); console.group(label);
@ -62,8 +64,8 @@ function printToConsole(item) {
if (filteredValues) { if (filteredValues) {
console.table(filteredValues); console.table(filteredValues);
} }
if (item._children) { if (item.children) {
for(const c of item._children) { for(const c of item.children) {
printToConsole(c); printToConsole(c);
} }
} }
@ -72,18 +74,18 @@ function printToConsole(item) {
} }
} }
function itemCaption(item) { function itemCaption(item: LogItem): string {
if (item._values.t === "network") { if (item.values.t === "network") {
return `${item._values.method} ${item._values.url}`; return `${item.values.method} ${item.values.url}`;
} else if (item._values.l && typeof item._values.id !== "undefined") { } else if (item.values.l && typeof item.values.id !== "undefined") {
return `${item._values.l} ${item._values.id}`; return `${item.values.l} ${item.values.id}`;
} else if (item._values.l && typeof item._values.status !== "undefined") { } else if (item.values.l && typeof item.values.status !== "undefined") {
return `${item._values.l} (${item._values.status})`; return `${item.values.l} (${item.values.status})`;
} else if (item._values.l && item.error) { } else if (item.values.l && item.error) {
return `${item._values.l} failed`; return `${item.values.l} failed`;
} else if (typeof item._values.ref !== "undefined") { } else if (typeof item.values.ref !== "undefined") {
return `ref ${item._values.ref}`; return `ref ${item.values.ref}`;
} else { } else {
return item._values.l || item._values.type; return item.values.l || item.values.type;
} }
} }

View file

@ -33,7 +33,7 @@ interface ISerializedItem {
c?: Array<ISerializedItem>; c?: Array<ISerializedItem>;
}; };
type LogItemValues = { export type LogItemValues = {
l?: string; l?: string;
t?: string; t?: string;
id?: unknown; id?: unknown;
@ -285,4 +285,12 @@ export class LogItem {
get logger(): BaseLogger { get logger(): BaseLogger {
return this._logger; return this._logger;
} }
get values(): LogItemValues {
return this._values;
}
get children(): Array<LogItem> | null {
return this._children;
}
} }