@@ -7,6 +7,7 @@ import * as vscode from 'vscode'
7
7
import Transport from 'winston-transport'
8
8
import globals from '../extensionGlobals'
9
9
import { removeAnsi } from '../utilities/textUtilities'
10
+ import { LogLevel } from './logger'
10
11
11
12
export const MESSAGE = Symbol . for ( 'message' ) // eslint-disable-line @typescript-eslint/naming-convention
12
13
@@ -19,6 +20,8 @@ interface LogEntry {
19
20
20
21
export class OutputChannelTransport extends Transport {
21
22
private readonly outputChannel : Pick < vscode . OutputChannel , 'append' | 'appendLine' >
23
+ // True if `outputChannel` is a `vscode.LogOutputChannel`.
24
+ private readonly isLogChan : boolean
22
25
private readonly stripAnsi : boolean
23
26
24
27
public constructor (
@@ -32,16 +35,46 @@ export class OutputChannelTransport extends Transport {
32
35
33
36
this . outputChannel = options . outputChannel
34
37
this . stripAnsi = options . stripAnsi ?? false
38
+
39
+ const c = this . outputChannel
40
+ this . isLogChan = ! ! ( ( c as any ) . info && ( c as any ) . debug && ( c as any ) . warn && ( c as any ) . error )
41
+ // Else: we got `vscode.debug.activeDebugConsole` which does not yet implement `vscode.LogOutputChannel`.
35
42
}
36
43
37
44
public override log ( info : LogEntry , next : ( ) => void ) : void {
38
45
globals . clock . setImmediate ( ( ) => {
39
- const msg = this . stripAnsi ? removeAnsi ( info [ MESSAGE ] ) : info [ MESSAGE ]
46
+ if ( this . isLogChan ) {
47
+ const c = this . outputChannel as vscode . LogOutputChannel
48
+ // Example input:
49
+ // message: 'Preparing to debug locally: Lambda "index.handler"'
50
+ // raw: true
51
+ // Symbol(level): 'info'
52
+ // Symbol(message): '2024-01-16 08:54:30 [INFO]: Preparing to debug locally: Lambda "index.handler"'
53
+ // Symbol(splat): (1) [{…}]
54
+ // timestamp: '2024-01-16 08:54:30'
55
+ // We want the "raw" message without the frontmatter, because
56
+ // `vscode.LogOutputChannel` presents its own timestamp + loglevel.
57
+ const raw = this . stripAnsi ? removeAnsi ( info . message ) : info . message
58
+ // Avoid extra line breaks.
59
+ const msg = raw . trim ( )
40
60
41
- if ( info . raw ) {
42
- this . outputChannel . append ( msg )
61
+ const loglevel = info . level as LogLevel
62
+ if ( loglevel === 'error' ) {
63
+ c . error ( msg )
64
+ } else if ( loglevel === 'warn' ) {
65
+ c . warn ( msg )
66
+ } else if ( loglevel === 'debug' || loglevel === 'verbose' ) {
67
+ c . debug ( msg )
68
+ } else {
69
+ c . info ( msg )
70
+ }
43
71
} else {
44
- this . outputChannel . appendLine ( msg )
72
+ const msg = this . stripAnsi ? removeAnsi ( info [ MESSAGE ] ) : info [ MESSAGE ]
73
+ if ( info . raw ) {
74
+ this . outputChannel . append ( msg )
75
+ } else {
76
+ this . outputChannel . appendLine ( msg )
77
+ }
45
78
}
46
79
47
80
this . emit ( 'logged' , info )
0 commit comments