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.
*/
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;
}
}

View file

@ -33,7 +33,7 @@ interface ISerializedItem {
c?: Array<ISerializedItem>;
};
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<LogItem> | null {
return this._children;
}
}