Skip to content

Commit

Permalink
fix: improve logging stability for complex objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jharvey10 committed Oct 26, 2023
1 parent df495d2 commit 2646d66
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
14 changes: 12 additions & 2 deletions src/main/core/log/loggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,24 @@ import { type Logger } from './logger.js'
* Abstract class which enforces that all child classes define a logger instance.
*/
export class Loggable {
protected logger: Logger
// JS-private notation is used to make this field non-enumerable
readonly #logger: Logger

/**
* Gets the logger instance associated with this Loggable.
*
* @returns The logger instance.
*/
protected get logger() {
return this.#logger
}

/**
* Constructs a loggable class.
*
* @param logger - The logger to use during logging.
*/
public constructor(logger: Logger) {
this.logger = logger
this.#logger = logger
}
}
8 changes: 5 additions & 3 deletions src/main/core/log/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ const DRAIN_INTERVAL = 250 // ms
export class Logger {
private readonly filePath: string
private readonly buffer: string[]
private readonly drainer: NodeJS.Timeout

// JS-private notation is used to make this field non-enumerable
readonly #drainer: NodeJS.Timeout

/**
* Constructs a Logger instance.
Expand All @@ -28,7 +30,7 @@ export class Logger {
public constructor(filePath: string) {
this.filePath = filePath
this.buffer = []
this.drainer = setInterval(() => {
this.#drainer = setInterval(() => {
void this.flush()
}, DRAIN_INTERVAL)

Expand All @@ -48,7 +50,7 @@ export class Logger {
* Flushes the logger and clears the log draining interval.
*/
public async close() {
clearInterval(this.drainer)
clearInterval(this.#drainer)
await this.flush()
}

Expand Down

0 comments on commit 2646d66

Please sign in to comment.