重定向控制台。log消息到输出频道

发布于 2025-01-20 10:52:08 字数 445 浏览 2 评论 0原文

如何配置我的扩展名来编写所有Conselo.log} Info | debug消息给输出渠道? 这似乎是lsp扩展名的默认值请参阅此问题,然后固定,但是我找不到如何设置此配置的常规扩展名。

显然,可以直接创建和写入自定义“ nofollow noreferrer”>输出通道但这需要我创建一个自定义的伐木类,该类仅复制以前做过的事情。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

活雷疯 2025-01-27 10:52:08

这是一个解决方法,而不是精确的解决方案,

我最终创建了一个记录器,该记录器包裹outputchannel并写入它,

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);
            }
        }
    }
}

然后在您的扩展程序中只需创建一个记录器,然后使用它

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}`);

创建并显示outputchannel < /代码>带有“ nav-code”的名称

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文