Skip to content

Commit

Permalink
feat(logging): Update stopwatch logging to include nested processes
Browse files Browse the repository at this point in the history
  • Loading branch information
sullivanpj committed Dec 9, 2023
1 parent 3a94db4 commit b1f9c85
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions packages/logging/src/storm-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class StormLog implements IStormLog {
#logLevel: LogLevel;
#logLevelLabel: LogLevelLabel;

#processes: Map<string, StormTime> = new Map();
#processes: Array<{ name: string; startedAt: StormTime }> = [];

/**
* The Singleton's constructor should always be private to prevent direct
Expand Down Expand Up @@ -385,7 +385,8 @@ export class StormLog implements IStormLog {
*/
public start(name: string) {
if (this.#logLevel >= LogLevel.INFO) {
this.#processes.set(name, StormTime.current());
this.#processes.push({ name, startedAt: StormTime.current() });
this.#logger.info(`▶️ Starting process: ${this.#processes.join(" > ")}`);
}
}

Expand All @@ -404,25 +405,42 @@ export class StormLog implements IStormLog {
this.warn("No name or start time was provided to the stopwatch method");
return;
}
if (!startTime && !this.#processes.has(name!)) {
if (!startTime && !this.#processes.some(item => item.name === name)) {
this.warn(
`No start time was provided and the ${name} process was never started`
);
return;
}
if (name && this.#processes.has(name)) {
startTime = this.#processes.get(name);
if (name && this.#processes.some(item => item.name === name)) {
startTime = this.#processes.find(item => item.name === name)?.startedAt;
}

const message = `The ${name ? ` ${name}` : ""} process took ${formatSince(
startTime!.since()
)} to complete`;
let process = name;
if (
this.#processes.length > 0 &&
process &&
this.#processes.some(item => item.name === process)
) {
process = this.#processes
.slice(
0,
this.#processes.findIndex(item => item.name === process)
)
.join(" > ");
}

const message = `${
process ? `Completed process: ${process}` : "The process has completed"
} \n\n⏱️ Process took ${formatSince(startTime!.since())} to complete`;

this.#logger.info(`⏱️ ${message}`);
this.#logger.info(message);
Promise.all(this.#additionalLoggers.map(logger => logger.info(message)));

if (name && this.#processes.has(name)) {
this.#processes.delete(name);
if (name && this.#processes.some(item => item.name === name)) {
const index = this.#processes.findLastIndex(item => item.name === name);
if (index) {
this.#processes.splice(index, 1);
}
}
}

Expand Down

0 comments on commit b1f9c85

Please sign in to comment.