Skip to content

Commit

Permalink
feat: output logs to vscode output channel
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyenoch committed Jul 25, 2024
1 parent 650f234 commit 94c549a
Showing 1 changed file with 44 additions and 7 deletions.
51 changes: 44 additions & 7 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as vscode from 'vscode'

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const isServer = typeof window === 'undefined'
Expand Down Expand Up @@ -53,6 +55,7 @@ const hslToRgb = (
}

export interface LoggerOptions {
vscodeOutputName: string
label?: string
enableDebug?: boolean
logStorageFlagName?: string
Expand All @@ -68,6 +71,8 @@ export class Logger {

color: string | undefined

vscodeOutputChannel: vscode.OutputChannel

private _enableDebug!: boolean

protected get enableDebug() {
Expand All @@ -83,14 +88,18 @@ export class Logger {
this._enableDebug = value
}

constructor(optionsOrLabel?: LoggerOptions | string) {
constructor(optionsOrLabel: LoggerOptions | string) {
const options: LoggerOptions =
(typeof optionsOrLabel === 'string'
? { label: optionsOrLabel }
? { label: optionsOrLabel, vscodeOutputName: optionsOrLabel }
: optionsOrLabel) || {}

const { enableDebug, label } = options
const { enableDebug, label, vscodeOutputName } = options

this.vscodeOutputChannel = vscode.window.createOutputChannel(
vscodeOutputName,
'log'
)
this.label = label
this.color = this.calculateColor(label)
this.enableDebug = enableDebug as boolean
Expand Down Expand Up @@ -124,10 +133,30 @@ export class Logger {
return `[${this.label}]`
}

private logWithColor = (
method: 'log' | 'warn' | 'error' | 'debug',
...args: any[]
private formatDate(date: Date): string {
return date.toISOString().replace('T', ' ').replace('Z', '')
}

private logToVscodeOutputChannel: typeof this.logWithColor = (
method,
...args
) => {
const level = method === 'log' ? 'info' : method
const vscodeLogContent = [
this.formatDate(new Date()),
`[${level}]`,
...args
]
this.vscodeOutputChannel.appendLine(
vscodeLogContent
.map(arg =>
typeof arg === 'object' ? JSON.stringify(arg, null, 2) : String(arg)
)
.join(' ')
)
}

private logToConsole: typeof this.logWithColor = (method, ...args) => {
if (!this.label) {
;(console as any)[method](...args)
return
Expand All @@ -147,6 +176,14 @@ export class Logger {
}
}

private logWithColor = (
method: 'log' | 'warn' | 'error' | 'debug',
...args: any[]
) => {
this.logToVscodeOutputChannel(method, ...args)
this.logToConsole(method, ...args)
}

// Check if logging should occur
shouldLog = () => this.enableDebug

Expand Down Expand Up @@ -181,4 +218,4 @@ export class Logger {
}
}

export const logger = new Logger('aide')
export const logger = new Logger('Aide')

0 comments on commit 94c549a

Please sign in to comment.