41

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

Gama11
  • 31,714
  • 9
  • 78
  • 100
hinst
  • 802
  • 1
  • 7
  • 16

6 Answers6

50

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.");

Results of creating output channel.

Alex
  • 1,979
  • 16
  • 24
14

Just open vscode and go to menu "Help"->"Toggle Developer Tools" and the console is displayed on rigth window.

Luciano Ribas
  • 309
  • 2
  • 4
7

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.

  • 4
    IConnection is a part of [vscode-languageserver-node](https://github.com/Microsoft/vscode-languageserver-node) module. – Tomasz Dolny Jan 05 '16 at 11:44
7

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.

TBieniek
  • 4,858
  • 2
  • 24
  • 29
  • 3
    This 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
1

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.

Youssef SABIH
  • 629
  • 6
  • 11
1

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);
Vivil
  • 39
  • 5
  • 3
    Your 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