Make properties in LogItem optional, not null

This commit is contained in:
RMidhunSuresh 2021-11-15 19:17:49 +05:30
parent 7097ba07d1
commit ba5f2032ba
2 changed files with 15 additions and 18 deletions

View file

@ -36,12 +36,12 @@ export interface ILogItem {
logger: any; logger: any;
level: typeof LogLevel; level: typeof LogLevel;
duration?: number; duration?: number;
end?: number | null; end?: number;
start?: number; start?: number;
logLevel: LogLevel; logLevel: LogLevel;
children: Array<ILogItem> | null; children?: Array<ILogItem>;
values: LogItemValues; values: LogItemValues;
error: Error | null; error?: Error;
wrap(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): unknown; wrap(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): unknown;
log(labelOrValues: LabelOrValues, logLevel?: LogLevel): void; log(labelOrValues: LabelOrValues, logLevel?: LogLevel): void;
set(key: string | object, value: unknown): void; set(key: string | object, value: unknown): void;
@ -73,22 +73,19 @@ export type LogCallback = (item: ILogItem) => unknown;
export class LogItem implements ILogItem { export class LogItem implements ILogItem {
public readonly start: number; public readonly start: number;
public logLevel: LogLevel; public logLevel: LogLevel;
public error: Error | null; public error?: Error;
public end: number | null; public end?: number;
private _values: LogItemValues; private _values: LogItemValues;
private _logger: BaseLogger; private _logger: BaseLogger;
private _filterCreator?: FilterCreator; private _filterCreator?: FilterCreator;
private _children: Array<LogItem> | null; private _children?: Array<LogItem>;
constructor(labelOrValues: LabelOrValues, logLevel: LogLevel, logger: BaseLogger, filterCreator?: FilterCreator) { constructor(labelOrValues: LabelOrValues, logLevel: LogLevel, logger: BaseLogger, filterCreator?: FilterCreator) {
this._logger = logger; this._logger = logger;
this.start = logger._now(); this.start = logger._now();
this.end = null;
// (l)abel // (l)abel
this._values = typeof labelOrValues === "string" ? {l: labelOrValues} : labelOrValues; this._values = typeof labelOrValues === "string" ? {l: labelOrValues} : labelOrValues;
this.error = null;
this.logLevel = logLevel; this.logLevel = logLevel;
this._children = null;
this._filterCreator = filterCreator; this._filterCreator = filterCreator;
} }
@ -183,7 +180,7 @@ export class LogItem implements ILogItem {
} }
} }
let children: Array<ISerializedItem> | null = null; let children: Array<ISerializedItem> | null = null;
if (this._children !== null) { if (this._children) {
children = this._children.reduce((array: Array<ISerializedItem>, c) => { children = this._children.reduce((array: Array<ISerializedItem>, c) => {
const s = c.serialize(filter, this.start, false); const s = c.serialize(filter, this.start, false);
if (s) { if (s) {
@ -241,7 +238,7 @@ export class LogItem implements ILogItem {
* @return {[type]} [description] * @return {[type]} [description]
*/ */
run(callback: LogCallback): unknown { run(callback: LogCallback): unknown {
if (this.end !== null) { if (this.end) {
console.trace("log item is finished, additional logs will likely not be recorded"); console.trace("log item is finished, additional logs will likely not be recorded");
} }
let result: unknown; let result: unknown;
@ -268,8 +265,8 @@ export class LogItem implements ILogItem {
* @internal shouldn't typically be called by hand. allows to force finish if a promise is still running when closing the app * @internal shouldn't typically be called by hand. allows to force finish if a promise is still running when closing the app
*/ */
finish(): void { finish(): void {
if (this.end === null) { if (!this.end) {
if (this._children !== null) { if (this._children) {
for(const c of this._children) { for(const c of this._children) {
c.finish(); c.finish();
} }
@ -291,14 +288,14 @@ export class LogItem implements ILogItem {
} }
child(labelOrValues: LabelOrValues, logLevel?: LogLevel, filterCreator?: FilterCreator): ILogItem { child(labelOrValues: LabelOrValues, logLevel?: LogLevel, filterCreator?: FilterCreator): ILogItem {
if (this.end !== null) { if (this.end) {
console.trace("log item is finished, additional logs will likely not be recorded"); console.trace("log item is finished, additional logs will likely not be recorded");
} }
if (!logLevel) { if (!logLevel) {
logLevel = this.logLevel || LogLevel.Info; logLevel = this.logLevel || LogLevel.Info;
} }
const item = new LogItem(labelOrValues, logLevel, this._logger, filterCreator); const item = new LogItem(labelOrValues, logLevel, this._logger, filterCreator);
if (this._children === null) { if (!this._children) {
this._children = []; this._children = [];
} }
this._children.push(item); this._children.push(item);
@ -313,7 +310,7 @@ export class LogItem implements ILogItem {
return this._values; return this._values;
} }
get children(): Array<LogItem> | null { get children(): Array<LogItem> | undefined {
return this._children; return this._children;
} }
} }

View file

@ -52,9 +52,9 @@ export class NullLogger {
export class NullLogItem implements ILogItem { export class NullLogItem implements ILogItem {
public readonly logger: NullLogger; public readonly logger: NullLogger;
public readonly logLevel: LogLevel; public readonly logLevel: LogLevel;
public children: Array<ILogItem> | null = null; public children?: Array<ILogItem>;
public values: LogItemValues; public values: LogItemValues;
public error: Error | null = null; public error?: Error;
constructor(logger: NullLogger) { constructor(logger: NullLogger) {
this.logger = logger; this.logger = logger;