2021-03-01 19:31:43 +05:30
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2021-11-10 12:36:56 +05:30
|
|
|
import {BaseLogger} from "./BaseLogger";
|
2021-11-15 17:29:08 +05:30
|
|
|
import type {ILogItem, LogItemValues} from "./LogItem";
|
2021-03-01 19:31:43 +05:30
|
|
|
|
|
|
|
export class ConsoleLogger extends BaseLogger {
|
2021-11-15 17:29:08 +05:30
|
|
|
_persistItem(item: ILogItem): void {
|
2021-03-01 19:31:43 +05:30
|
|
|
printToConsole(item);
|
|
|
|
}
|
2021-11-09 13:53:07 +05:30
|
|
|
|
2021-11-14 16:24:16 +05:30
|
|
|
export(): void {
|
2021-11-09 13:53:07 +05:30
|
|
|
throw new Error("Cannot export from ConsoleLogger");
|
|
|
|
}
|
2021-03-01 19:31:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
const excludedKeysFromTable = ["l", "id"];
|
2021-11-14 16:24:16 +05:30
|
|
|
function filterValues(values: LogItemValues): LogItemValues | null {
|
2021-03-01 19:31:43 +05:30
|
|
|
if (!values) {
|
2021-11-14 16:24:16 +05:30
|
|
|
// todo: is this check here unnecessary because LogItem will always have values?
|
2021-03-01 19:31:43 +05:30
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return Object.entries(values)
|
|
|
|
.filter(([key]) => !excludedKeysFromTable.includes(key))
|
2021-11-14 16:24:16 +05:30
|
|
|
.reduce((obj: LogItemValues, [key, value]) => {
|
2021-03-01 19:31:43 +05:30
|
|
|
obj = obj || {};
|
|
|
|
obj[key] = value;
|
|
|
|
return obj;
|
|
|
|
}, null);
|
|
|
|
}
|
|
|
|
|
2021-11-15 17:29:08 +05:30
|
|
|
function printToConsole(item: ILogItem): void {
|
2021-03-01 19:31:43 +05:30
|
|
|
const label = `${itemCaption(item)} (${item.duration}ms)`;
|
2021-11-14 16:24:16 +05:30
|
|
|
const filteredValues = filterValues(item.values);
|
|
|
|
const shouldGroup = item.children || filteredValues;
|
2021-03-01 19:31:43 +05:30
|
|
|
if (shouldGroup) {
|
|
|
|
if (item.error) {
|
|
|
|
console.group(label);
|
|
|
|
} else {
|
|
|
|
console.groupCollapsed(label);
|
|
|
|
}
|
|
|
|
if (item.error) {
|
|
|
|
console.error(item.error);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (item.error) {
|
|
|
|
console.error(item.error);
|
|
|
|
} else {
|
|
|
|
console.log(label);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (filteredValues) {
|
|
|
|
console.table(filteredValues);
|
|
|
|
}
|
2021-11-14 16:24:16 +05:30
|
|
|
if (item.children) {
|
|
|
|
for(const c of item.children) {
|
2021-03-01 19:31:43 +05:30
|
|
|
printToConsole(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (shouldGroup) {
|
|
|
|
console.groupEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 17:29:08 +05:30
|
|
|
function itemCaption(item: ILogItem): string {
|
2021-11-14 16:24:16 +05:30
|
|
|
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}`;
|
2021-03-01 19:31:43 +05:30
|
|
|
} else {
|
2021-11-14 16:24:16 +05:30
|
|
|
return item.values.l || item.values.type;
|
2021-03-01 19:31:43 +05:30
|
|
|
}
|
|
|
|
}
|