Skip to content

Commit cebfabd

Browse files
authored
Merge pull request #61 from Cyenoch/master
feat: output logs to vscode output channel
2 parents 650f234 + 954fd48 commit cebfabd

2 files changed

Lines changed: 52 additions & 10 deletions

File tree

.vscode/settings.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
{
22
"i18n-ally.pathMatcher": "package.nls.{locale}.json",
33
"i18n-ally.keystyle": "flat",
4-
"i18n-ally.localesPaths": ["."],
5-
"i18n-ally.enabledFrameworks": ["vscode", "react"],
4+
"i18n-ally.localesPaths": [
5+
"."
6+
],
7+
"i18n-ally.enabledFrameworks": [
8+
"vscode",
9+
"react"
10+
],
611
"i18n-ally.dirStructure": "file",
712
// Enable eslint for all supported languages
813
"eslint.validate": [
@@ -29,6 +34,7 @@
2934
"typescript.enablePromptUseWorkspaceTsdk": true, // For security reasons it's require that users opt into using the workspace version of typescript
3035
"typescript.preferences.preferTypeOnlyAutoImports": true, // Prefer type-only imports
3136
"testing.openTesting": "neverOpen", // Don't open the testing view automatically when running tests
37+
"prettier.enable": true, // enable prettier for those who disabled it for some reason
3238
"prettier.ignorePath": ".gitignore", // Don't run prettier for files listed in .gitignore
3339
"editor.defaultFormatter": "esbenp.prettier-vscode",
3440
"editor.formatOnPaste": false,
@@ -40,7 +46,6 @@
4046
// "typescript.inlayHints.propertyDeclarationTypes.enabled": true,
4147
"typescript.inlayHints.parameterTypes.enabled": true,
4248
// "typescript.inlayHints.functionLikeReturnTypes.enabled": true,
43-
4449
"workbench.colorCustomizations": {
4550
"activityBar.background": "#0E3140",
4651
"titleBar.activeBackground": "#14455A",

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)