1

how can I configure my extension to write all conselo.log}info|debug messages to an outputchannel ? this seems to be the default for LSP Extensions See this issue where it was broken and then fixed, however I have not been able to find how set this configuration for a regular extension.

Clearly it is possible to create and write directly to a custom Output Channel but that would require me to create a custom logging class that just replicates something that has been done before.

Jos Verlinde
  • 1,468
  • 12
  • 25
  • https://nodejs.org/api/console.html – rioV8 Apr 10 '22 at 12:15
  • @rioV8, the node console does not provide the ability to write to an VSCode Output Channel. – Jos Verlinde Apr 10 '22 at 13:16
  • write your own implementation of a `stream.Writable` and pass it to the constructor – rioV8 Apr 10 '22 at 13:34
  • yes , but that would still require that that new console would need to be inserted into VSCode as that creates the context for the extensions. Please refer to : https://code.visualstudio.com/api/extension-capabilities/overview – Jos Verlinde Apr 10 '22 at 15:21
  • let this new `stream.Writable` tunnel the text to the Output Channel and redefine the global `console` for your extension files – rioV8 Apr 10 '22 at 15:36
  • do you have a sample/ outline of how that code would look like ? ie how to redefine an object in a scope that is create by vscode before my code even runs ? – Jos Verlinde Apr 10 '22 at 16:03

1 Answers1

1

This is a workaround rather than exact solution

I ended up creating a logger that wraps an outputchannel and writes to it

import * as vscode from 'vscode';
import * as util from 'util' // has no default export
import { OutputChannel } from "vscode";

export class Logger {
    private outputChannel:OutputChannel ;

    constructor(extensionOuputName: string, private useConsole = true) {
        if (!useConsole) {
            this.outputChannel = vscode.window.createOutputChannel(extensionOuputName);
            this.outputChannel.show();
        }
    }

    public append(o: any) {
        const prefix = `[${new Date().toLocaleString()}]`;

        if (o.constructor === Array) {
            o.forEach(item => this.append(item));
        }
        else {
            if (this.useConsole) {
                console.log(prefix, o);
            }
            else {
                this.outputChannel.append(prefix + ' ');
                const isObject = (typeof o === 'object' && o !== null);
                //be carefull stringify can not convert circular dependencies
                //this.outputChannel.appendLine(isObject ? JSON.stringify(o) : o);
                this.outputChannel.appendLine(isObject ? util.inspect(o) : o);
            }
        }
    }
}

Then in your extension just create a logger and use it

export class YourClass {
    //false to use the outputchannel - true to use regular console
    private log: Logger = new Logger('Nav-Code', false);
    this.log.append(`Checking folder: ${this.projectFolder}`);

This creates and shows an outputchannel with the name 'Nav-Code' enter image description here

Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99