@@ -19,6 +19,37 @@ import {
19
19
* configured LogNotificationHandler to the LSP client.
20
20
*/
21
21
class LSPLogger implements LoggerInterface {
22
+ /**
23
+ * Formats a timestamp in the format [5:46:20 AM]
24
+ * @returns Formatted timestamp string
25
+ */
26
+ private formatTimestamp ( ) : string {
27
+ const now = new Date ( ) ;
28
+ const timeString = now . toLocaleTimeString ( 'en-US' , {
29
+ hour : 'numeric' ,
30
+ minute : '2-digit' ,
31
+ second : '2-digit' ,
32
+ hour12 : true ,
33
+ } ) ;
34
+ return `[${ timeString } ]` ;
35
+ }
36
+
37
+ /**
38
+ * Formats a message type in the format [INFO]
39
+ * @param messageType The message type to format
40
+ * @returns Formatted message type string
41
+ */
42
+ private formatMessageType ( messageType : LogMessageType ) : string {
43
+ const typeMap : Record < LogMessageType , string > = {
44
+ debug : '[DEBUG]' ,
45
+ info : '[INFO]' ,
46
+ log : '[LOG]' ,
47
+ warning : '[WARNING]' ,
48
+ error : '[ERROR]' ,
49
+ } ;
50
+ return typeMap [ messageType ] || `[${ messageType . toUpperCase ( ) } ]` ;
51
+ }
52
+
22
53
/**
23
54
* Logs a message with the specified type.
24
55
* @param messageType The LSP message type.
@@ -33,29 +64,31 @@ class LSPLogger implements LoggerInterface {
33
64
return ;
34
65
}
35
66
36
- const message =
67
+ const rawMessage =
37
68
typeof messageOrProvider === 'function'
38
69
? messageOrProvider ( )
39
70
: messageOrProvider ;
40
- const handler = getLogNotificationHandler ( ) ;
41
71
42
- // Add ISO timestamp and log level to every message
43
- const timestamp = new Date ( ) . toISOString ( ) ;
44
- const logLevel = messageType . toUpperCase ( ) ;
45
- const messageWithTimestamp = `[${ timestamp } ] [${ logLevel } ] ${ message } ` ;
72
+ // Format the message with timestamp and message type prefix
73
+ const timestamp = this . formatTimestamp ( ) ;
74
+ const typePrefix = this . formatMessageType ( messageType ) ;
75
+ const message = `${ timestamp } ${ typePrefix } ${ rawMessage } ` ;
76
+
77
+ const handler = getLogNotificationHandler ( ) ;
46
78
79
+ // Send the formatted message to the LSP client
47
80
if ( handler && typeof handler . sendLogMessage === 'function' ) {
48
81
// For backward compatibility, map debug to log for older LSP clients
49
82
const mappedType = messageType === 'debug' ? 'log' : messageType ;
50
83
handler . sendLogMessage ( {
51
84
type : mappedType ,
52
- message : messageWithTimestamp ,
85
+ message,
53
86
} ) ;
54
87
} else {
55
88
const fallbackType = messageType . toString ( ) ;
56
89
console . warn (
57
90
'[LSPLogger] LogNotificationHandler not available or invalid. ' +
58
- `Fallback log (${ fallbackType } ): ${ messageWithTimestamp } ` ,
91
+ `Fallback log (${ fallbackType } ): ${ message } ` ,
59
92
) ;
60
93
}
61
94
}
0 commit comments