Skip to content

Commit 91566bb

Browse files
authored
fix: [@W-18962100@] fix client config for trace server (#92)
* fix: separate logger channel from extension channel * chore: fix tests * chore: setup nls support for package text * chore: config alignment fixes * chore: simplify applying client config to server * chore: where is trace * chore: let vscode client fully control output * chore: fix missing labels
1 parent 04498ef commit 91566bb

36 files changed

+1757
-677
lines changed

packages/apex-ls-node/src/index.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,24 @@ import {
4040
setLogNotificationHandler,
4141
getLogger,
4242
setLoggerFactory,
43+
setLogLevel,
4344
} from '@salesforce/apex-lsp-logging';
4445

4546
import { NodeLogNotificationHandler } from './utils/NodeLogNotificationHandler';
4647
import { LSPLoggerFactory } from './utils/LSPLoggerFactory';
4748
import { NodeFileSystemApexStorage } from './storage/NodeFileSystemApexStorage';
4849
import { createNodeApexLibAdapter } from './utils/NodeApexLibAdapter';
4950

51+
/**
52+
* Interface for server initialization options
53+
*/
54+
interface ApexServerInitializationOptions {
55+
logLevel?: string;
56+
enableDocumentSymbols?: boolean;
57+
trace?: string;
58+
[key: string]: any;
59+
}
60+
5061
export function startServer() {
5162
// Create a connection for the server based on command line arguments
5263
let connection: Connection;
@@ -95,6 +106,17 @@ export function startServer() {
95106
connection.onInitialize((params: InitializeParams): InitializeResult => {
96107
logger.info('Apex Language Server initializing...');
97108

109+
// Extract and set log level from initialization options
110+
const initOptions = params.initializationOptions as
111+
| ApexServerInitializationOptions
112+
| undefined;
113+
const logLevel = initOptions?.logLevel || 'error';
114+
115+
logger.info(`Setting log level to: ${logLevel}`);
116+
117+
// Set the log level in the logging system
118+
setLogLevel(logLevel);
119+
98120
// Process initialization parameters and settings
99121
configurationManager.processInitializeParams(params);
100122

@@ -136,9 +158,6 @@ export function startServer() {
136158
// Register for configuration changes
137159
configurationManager.registerForConfigurationChanges();
138160

139-
// Request initial configuration from client
140-
configurationManager.requestConfiguration();
141-
142161
// Register the apexlib/resolve request handler
143162
connection.onRequest('apexlib/resolve', async (params) => {
144163
logger.debug(

packages/apex-ls-node/src/utils/LSPLoggerFactory.ts

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,37 @@ import {
1919
* configured LogNotificationHandler to the LSP client.
2020
*/
2121
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+
2253
/**
2354
* Logs a message with the specified type.
2455
* @param messageType The LSP message type.
@@ -33,29 +64,31 @@ class LSPLogger implements LoggerInterface {
3364
return;
3465
}
3566

36-
const message =
67+
const rawMessage =
3768
typeof messageOrProvider === 'function'
3869
? messageOrProvider()
3970
: messageOrProvider;
40-
const handler = getLogNotificationHandler();
4171

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();
4678

79+
// Send the formatted message to the LSP client
4780
if (handler && typeof handler.sendLogMessage === 'function') {
4881
// For backward compatibility, map debug to log for older LSP clients
4982
const mappedType = messageType === 'debug' ? 'log' : messageType;
5083
handler.sendLogMessage({
5184
type: mappedType,
52-
message: messageWithTimestamp,
85+
message,
5386
});
5487
} else {
5588
const fallbackType = messageType.toString();
5689
console.warn(
5790
'[LSPLogger] LogNotificationHandler not available or invalid. ' +
58-
`Fallback log (${fallbackType}): ${messageWithTimestamp}`,
91+
`Fallback log (${fallbackType}): ${message}`,
5992
);
6093
}
6194
}

packages/apex-ls-node/test/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ jest.mock('@salesforce/apex-lsp-logging', () => ({
247247
setLogNotificationHandler: jest.fn(),
248248
getLogger: () => mockLogger,
249249
setLoggerFactory: jest.fn(),
250+
setLogLevel: jest.fn(),
250251
LogLevel: {
251252
Error: 'ERROR',
252253
Warn: 'WARN',

0 commit comments

Comments
 (0)