Create alias for LogLevel | null

This commit is contained in:
RMidhunSuresh 2021-11-10 14:29:23 +05:30
parent 142d3ef543
commit 0b4eca4724
3 changed files with 15 additions and 13 deletions

View file

@ -16,7 +16,7 @@ limitations under the License.
*/ */
import {LogItem, LabelOrValues, FilterCreator, LogCallback} from "./LogItem"; import {LogItem, LabelOrValues, FilterCreator, LogCallback} from "./LogItem";
import {LogLevel, LogFilter} from "./LogFilter"; import {LogLevel, LogFilter, LogLevelOrNull} from "./LogFilter";
import {Platform} from "../platform/web/Platform.js"; import {Platform} from "../platform/web/Platform.js";
@ -28,14 +28,14 @@ export abstract class BaseLogger {
this._platform = platform; this._platform = platform;
} }
log(labelOrValues: LabelOrValues, logLevel: number = LogLevel.Info) { log(labelOrValues: LabelOrValues, logLevel: LogLevel = LogLevel.Info) {
const item = new LogItem(labelOrValues, logLevel, null, this); const item = new LogItem(labelOrValues, logLevel, null, this);
item.end = item.start; item.end = item.start;
this._persistItem(item, null, false); this._persistItem(item, null, false);
} }
/** if item is a log item, wrap the callback in a child of it, otherwise start a new root log item. */ /** if item is a log item, wrap the callback in a child of it, otherwise start a new root log item. */
wrapOrRun(item: LogItem, labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevel = null, filterCreator: FilterCreator = null) { wrapOrRun(item: LogItem, labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevelOrNull = null, filterCreator: FilterCreator = null) {
if (item) { if (item) {
return item.wrap(labelOrValues, callback, logLevel, filterCreator); return item.wrap(labelOrValues, callback, logLevel, filterCreator);
} else { } else {
@ -48,7 +48,7 @@ export abstract class BaseLogger {
Useful to pair with LogItem.refDetached. Useful to pair with LogItem.refDetached.
@return {LogItem} the log item added, useful to pass to LogItem.refDetached */ @return {LogItem} the log item added, useful to pass to LogItem.refDetached */
runDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevel = null, filterCreator: FilterCreator = null) { runDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevelOrNull = null, filterCreator: FilterCreator = null) {
if (logLevel === null) { if (logLevel === null) {
logLevel = LogLevel.Info; logLevel = LogLevel.Info;
} }
@ -60,7 +60,7 @@ export abstract class BaseLogger {
/** run a callback wrapped in a log operation. /** run a callback wrapped in a log operation.
Errors and duration are transparently logged, also for async operations. Errors and duration are transparently logged, also for async operations.
Whatever the callback returns is returned here. */ Whatever the callback returns is returned here. */
run(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevel = null, filterCreator: FilterCreator = null) { run(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevelOrNull = null, filterCreator: FilterCreator = null) {
if (logLevel === null) { if (logLevel === null) {
logLevel = LogLevel.Info; logLevel = LogLevel.Info;
} }
@ -131,7 +131,7 @@ export abstract class BaseLogger {
this._openItems.clear(); this._openItems.clear();
} }
abstract _persistItem(item: LogItem, filter?: LogFilter, forced?: boolean): void; abstract _persistItem(item: LogItem, filter?: LogFilter | null, forced?: boolean): void;
abstract export(): void; abstract export(): void;

View file

@ -26,6 +26,8 @@ export enum LogLevel {
Off Off
} }
export type LogLevelOrNull = LogLevel | null;
export class LogFilter { export class LogFilter {
constructor(parentFilter) { constructor(parentFilter) {
this._parentFilter = parentFilter; this._parentFilter = parentFilter;

View file

@ -16,7 +16,7 @@ limitations under the License.
*/ */
import {BaseLogger} from "./BaseLogger"; import {BaseLogger} from "./BaseLogger";
import {LogLevel, LogFilter} from "./LogFilter"; import {LogLevel, LogLevelOrNull, LogFilter} from "./LogFilter";
type LogItemWithLabel = { type LogItemWithLabel = {
l: string; l: string;
@ -64,18 +64,18 @@ export class LogItem {
} }
/** start a new root log item and run it detached mode, see BaseLogger.runDetached */ /** start a new root log item and run it detached mode, see BaseLogger.runDetached */
runDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevel, filterCreator: FilterCreator) { runDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevelOrNull, filterCreator: FilterCreator) {
return this._logger.runDetached(labelOrValues, callback, logLevel, filterCreator); return this._logger.runDetached(labelOrValues, callback, logLevel, filterCreator);
} }
/** start a new detached root log item and log a reference to it from this item */ /** start a new detached root log item and log a reference to it from this item */
wrapDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevel, filterCreator: FilterCreator) { wrapDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevelOrNull, filterCreator: FilterCreator) {
this.refDetached(this.runDetached(labelOrValues, callback, logLevel, filterCreator)); this.refDetached(this.runDetached(labelOrValues, callback, logLevel, filterCreator));
} }
/** logs a reference to a different log item, usually obtained from runDetached. /** logs a reference to a different log item, usually obtained from runDetached.
This is useful if the referenced operation can't be awaited. */ This is useful if the referenced operation can't be awaited. */
refDetached(logItem: LogItem, logLevel: LogLevel | null = null) { refDetached(logItem: LogItem, logLevel: LogLevelOrNull = null) {
logItem.ensureRefId(); logItem.ensureRefId();
return this.log({ref: logItem._values.refId as number}, logLevel); return this.log({ref: logItem._values.refId as number}, logLevel);
} }
@ -89,7 +89,7 @@ export class LogItem {
/** /**
* Creates a new child item and runs it in `callback`. * Creates a new child item and runs it in `callback`.
*/ */
wrap(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevel | null = null, filterCreator: FilterCreator = null) { wrap(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevelOrNull = null, filterCreator: FilterCreator = null) {
const item = this.child(labelOrValues, logLevel, filterCreator); const item = this.child(labelOrValues, logLevel, filterCreator);
return item.run(callback); return item.run(callback);
} }
@ -127,7 +127,7 @@ export class LogItem {
* *
* Hence, the child item is not returned. * Hence, the child item is not returned.
*/ */
log(labelOrValues: LabelOrValues, logLevel: LogLevel | null = null) { log(labelOrValues: LabelOrValues, logLevel: LogLevelOrNull = null) {
const item = this.child(labelOrValues, logLevel, null); const item = this.child(labelOrValues, logLevel, null);
item.end = item.start; item.end = item.start;
} }
@ -270,7 +270,7 @@ export class LogItem {
return err; return err;
} }
child(labelOrValues: LabelOrValues, logLevel: LogLevel | null, filterCreator: FilterCreator) { child(labelOrValues: LabelOrValues, logLevel: LogLevelOrNull, filterCreator: FilterCreator) {
if (this.end !== null) { if (this.end !== null) {
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");
} }