Skip to content

Commit 94c549a

Browse files
committed
feat: output logs to vscode output channel
1 parent 650f234 commit 94c549a

1 file changed

Lines changed: 44 additions & 7 deletions

File tree

src/logger.ts

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as vscode from 'vscode'
2+
13
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
24
// @ts-ignore
35
const isServer = typeof window === 'undefined'
@@ -53,6 +55,7 @@ const hslToRgb = (
5355
}
5456

5557
export interface LoggerOptions {
58+
vscodeOutputName: string
5659
label?: string
5760
enableDebug?: boolean
5861
logStorageFlagName?: string
@@ -68,6 +71,8 @@ export class Logger {
6871

6972
color: string | undefined
7073

74+
vscodeOutputChannel: vscode.OutputChannel
75+
7176
private _enableDebug!: boolean
7277

7378
protected get enableDebug() {
@@ -83,14 +88,18 @@ export class Logger {
8388
this._enableDebug = value
8489
}
8590

86-
constructor(optionsOrLabel?: LoggerOptions | string) {
91+
constructor(optionsOrLabel: LoggerOptions | string) {
8792
const options: LoggerOptions =
8893
(typeof optionsOrLabel === 'string'
89-
? { label: optionsOrLabel }
94+
? { label: optionsOrLabel, vscodeOutputName: optionsOrLabel }
9095
: optionsOrLabel) || {}
9196

92-
const { enableDebug, label } = options
97+
const { enableDebug, label, vscodeOutputName } = options
9398

99+
this.vscodeOutputChannel = vscode.window.createOutputChannel(
100+
vscodeOutputName,
101+
'log'
102+
)
94103
this.label = label
95104
this.color = this.calculateColor(label)
96105
this.enableDebug = enableDebug as boolean
@@ -124,10 +133,30 @@ export class Logger {
124133
return `[${this.label}]`
125134
}
126135

127-
private logWithColor = (
128-
method: 'log' | 'warn' | 'error' | 'debug',
129-
...args: any[]
136+
private formatDate(date: Date): string {
137+
return date.toISOString().replace('T', ' ').replace('Z', '')
138+
}
139+
140+
private logToVscodeOutputChannel: typeof this.logWithColor = (
141+
method,
142+
...args
130143
) => {
144+
const level = method === 'log' ? 'info' : method
145+
const vscodeLogContent = [
146+
this.formatDate(new Date()),
147+
`[${level}]`,
148+
...args
149+
]
150+
this.vscodeOutputChannel.appendLine(
151+
vscodeLogContent
152+
.map(arg =>
153+
typeof arg === 'object' ? JSON.stringify(arg, null, 2) : String(arg)
154+
)
155+
.join(' ')
156+
)
157+
}
158+
159+
private logToConsole: typeof this.logWithColor = (method, ...args) => {
131160
if (!this.label) {
132161
;(console as any)[method](...args)
133162
return
@@ -147,6 +176,14 @@ export class Logger {
147176
}
148177
}
149178

179+
private logWithColor = (
180+
method: 'log' | 'warn' | 'error' | 'debug',
181+
...args: any[]
182+
) => {
183+
this.logToVscodeOutputChannel(method, ...args)
184+
this.logToConsole(method, ...args)
185+
}
186+
150187
// Check if logging should occur
151188
shouldLog = () => this.enableDebug
152189

@@ -181,4 +218,4 @@ export class Logger {
181218
}
182219
}
183220

184-
export const logger = new Logger('aide')
221+
export const logger = new Logger('Aide')

0 commit comments

Comments
 (0)