diff --git a/.github/stale.yml b/.github/stale.yml index 7a6a571..f767674 100644 --- a/.github/stale.yml +++ b/.github/stale.yml @@ -6,10 +6,10 @@ daysUntilClose: 7 # Issues with these labels will never be considered stale exemptLabels: - - "Type: Security" + - 'Type: Security' # Label to use when marking an issue as stale -staleLabel: "Status: Abandoned" +staleLabel: 'Status: Abandoned' # Comment to post when marking an issue as stale. Set to `false` to disable markComment: > diff --git a/README.md b/README.md index 27cef08..50cc31b 100644 --- a/README.md +++ b/README.md @@ -155,6 +155,17 @@ const logs = ui.logger.getRenderer().getLogs() console.log(logs) ``` +You can also flush all logs by calling `flushLogs`. + +```ts +ui.logger.info('Hello world') + +const logs = ui.logger.getRenderer().getLogs() +console.log(logs.length); // 1 +ui.logger.getRenderer().flushLogs(); +console.log(logs.length); // 0 +``` + ## Logger actions Logger actions are pre-styled logs to display the outcome of an action. For example, the action can be to create/update or delete a file. diff --git a/src/logger/main.ts b/src/logger/main.ts index 1edc877..03ee940 100644 --- a/src/logger/main.ts +++ b/src/logger/main.ts @@ -45,6 +45,10 @@ export class Logger implements RendererContract { return this.getRenderer().getLogs() } + flushLogs(): void { + this.getRenderer().flushLogs() + } + constructor(options: Partial = {}) { const dimOutput = options.dim === undefined ? false : options.dim diff --git a/src/renderers/console.ts b/src/renderers/console.ts index 9c2b4bf..0c87a45 100644 --- a/src/renderers/console.ts +++ b/src/renderers/console.ts @@ -18,6 +18,8 @@ export class ConsoleRenderer implements RendererContract { return [] } + flushLogs(): void {} + log(message: string) { console.log(message) } diff --git a/src/renderers/memory.ts b/src/renderers/memory.ts index f8f6a85..a6a3660 100644 --- a/src/renderers/memory.ts +++ b/src/renderers/memory.ts @@ -19,6 +19,10 @@ export class MemoryRenderer implements RendererContract { return this.#logs } + flushLogs(): void { + this.#logs = [] + } + /** * Log message */ diff --git a/src/types.ts b/src/types.ts index 0b21ef7..1b5de6a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -18,6 +18,7 @@ export { Colors } */ export interface RendererContract { getLogs(): { message: string; stream: 'stdout' | 'stderr' }[] + flushLogs(): void /** * Log a message diff --git a/tests/logger.spec.ts b/tests/logger.spec.ts index f477df2..1a6a5d1 100644 --- a/tests/logger.spec.ts +++ b/tests/logger.spec.ts @@ -474,3 +474,25 @@ test.group('Logger | await', () => { assert.deepEqual(renderer.getLogs(), []) }) }) + +test.group('Logger | flushLogs', () => { + test('flush logs should empty logs array', ({ assert }) => { + const logger = new Logger({}) + const renderer = new MemoryRenderer() + + logger.useRenderer(renderer) + + logger.log('Hello world') + + assert.deepEqual(renderer.getLogs(), [ + { + message: `Hello world`, + stream: 'stdout', + }, + ]) + + logger.flushLogs() + + assert.deepEqual(renderer.getLogs(), []) + }) +}) diff --git a/tsconfig.json b/tsconfig.json index 2039043..ad0cc44 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,4 +4,4 @@ "rootDir": "./", "outDir": "./build" } -} \ No newline at end of file +}