Skip to content

Commit

Permalink
feat(logger): add flushLogs method to tests (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton authored Feb 16, 2024
1 parent b9d42b5 commit e033529
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .github/stale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: >
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 4 additions & 0 deletions src/logger/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export class Logger implements RendererContract {
return this.getRenderer().getLogs()
}

flushLogs(): void {
this.getRenderer().flushLogs()
}

constructor(options: Partial<LoggerOptions> = {}) {
const dimOutput = options.dim === undefined ? false : options.dim

Expand Down
2 changes: 2 additions & 0 deletions src/renderers/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export class ConsoleRenderer implements RendererContract {
return []
}

flushLogs(): void {}

log(message: string) {
console.log(message)
}
Expand Down
4 changes: 4 additions & 0 deletions src/renderers/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export class MemoryRenderer implements RendererContract {
return this.#logs
}

flushLogs(): void {
this.#logs = []
}

/**
* Log message
*/
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export { Colors }
*/
export interface RendererContract {
getLogs(): { message: string; stream: 'stdout' | 'stderr' }[]
flushLogs(): void

/**
* Log a message
Expand Down
22 changes: 22 additions & 0 deletions tests/logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(), [])
})
})
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"rootDir": "./",
"outDir": "./build"
}
}
}

0 comments on commit e033529

Please sign in to comment.