I am attempting to develop an extension with a language-server for VSCode. I am trying to figure out how to write text to log from language-server part of the extension. console.log produces nothing
6 Answers
Just as an update, you can use vscode.window.createOutputChannel
to create the output container and then write to it with the appendLine
method.
//Create output channel
let orange = vscode.window.createOutputChannel("Orange");
//Write to output.
orange.appendLine("I am a banana.");

- 1,979
- 16
- 24
-
This works, but I can't get it to switch to the `Orange` channel when appending lines. Other extensions seem to have a way to switch channels automatically. – Luke Pighetti May 03 '20 at 15:23
-
11This reply is a little late, but you can call `orange.show();` to make the output channel show up. – Kugelblitz Aug 24 '20 at 11:40
-
@Kugelblitz wow thanks, i spent hours looking for the debugging window lol – Gianfranco Fertino Dec 22 '20 at 02:58
-
2That lying banana/orange! :) – Ani May 24 '21 at 03:01
Just open vscode and go to menu "Help"->"Toggle Developer Tools" and the console is displayed on rigth window.

- 309
- 2
- 4
-
The output channel thing above flat doesn't work for me. This did. – dudeNumber4 Mar 12 '22 at 23:21
On server side try using connection.console.log.
// Create a connection for the server. The connection uses
// stdin / stdout for message passing
let connection: IConnection = createConnection(process.stdin, process.stdout);
connection.console.log(`Console test.`);
The message with show in Debug console on client side.
For client side simple console.log works well for me.

- 89
- 3
-
4IConnection is a part of [vscode-languageserver-node](https://github.com/Microsoft/vscode-languageserver-node) module. – Tomasz Dolny Jan 05 '16 at 11:44
You have to set an outputChannelName
property on the client options inside the client extension code:
let clientOptions: LanguageClientOptions = {
outputChannelName: 'XYZ Language Server',
};
Once you've done that you can use console.log()
and it will be shown in the VSCode extension output panel.

- 4,858
- 2
- 24
- 29
-
3This is a bit unclear: where do I put this, why would I use it instead of the `createOutputChannel` instructions from the other answer, so on and so forth. – ELLIOTTCABLE Mar 22 '22 at 15:24
The Language Server Protocol supports logging, use the notification window/logMessage to send log messages from the server, VS Code will display the server's log in the output panel, in the channel corresponding to the language client that started the server.

- 629
- 6
- 11
Thanks guys!
export let config: any = {};
export function getConfig() {
//debug
config.debug = workspace.getConfiguration().get('VBI.debug');
config.debugToChannel = workspace.getConfiguration().get('VBI.debugToChannel'); //Instead into dev-tools-console
return config;
}
/**
* @param cat Type String --> define Cathegory [info,warn,error]
* @param o Rest Parameter, Type Any --> Data to Log
*/
export let info = vscode.window.createOutputChannel("VBI-Info");
export function log(cat: string, ...o: any) {
function mapObject(obj: any) {
switch (typeof obj) {
case 'undefined':
return 'undefined';
case 'string':
return obj;
case 'number':
return obj.toString;
case 'object':
let ret: string = '';
for (const [key, value] of Object.entries(obj)) {
ret += (`${key}: ${value}\n`);
}
return ret;
default:
return obj; //function,symbol,boolean
}
}
if (config.debug) {
if (config.debugToChannel) {
switch (cat.toLowerCase()) {
case 'info':
info.appendLine('INFO:');
o.map((args: any) => {
info.appendLine('' + mapObject(args));
});
info.show();
return;
case 'warn':
info.appendLine('WARN:');
o.map((args: any) => {
info.appendLine('' + mapObject(args));
});
info.show();
return;
case 'error':
let err:string='';
info.appendLine('ERROR: ');
//err += mapObject(cat) + ": \r\n";
o.map((args: any) => {
err += mapObject(args);
});
info.appendLine(err);
vscode.window.showErrorMessage(err);//.replace(/(\r\n|\n|\r)/gm,"")
info.show();
return;
default:
info.appendLine('INFO-Other:');
info.appendLine(mapObject(cat));
o.map((args: any) => {
info.appendLine('' + mapObject(args));
});
info.show();
return;
}
}
else {
switch (cat.toLowerCase()) {
case 'info':
console.log('INFO:', o);
return;
case 'warn':
console.log('WARNING:', o);
return;
case 'error':
console.log('ERROR:', o);
return;
default:
console.log('log:',cat, o);
return;
}
}
}
}
tests:
import * as func from './functions';
import { config } from './functions';
func.getConfig();
let text = `debugToChannel:${config.debugToChannel}\n`;
func.log('info','vbi-format',text);
func.log('warn','vbi-format',text);
func.log('error','vbi-format',text);

- 39
- 5
-
3Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 22 '21 at 11:24