【问题标题】:Visual Studio Code not showing console logsVisual Studio Code 不显示控制台日志
【发布时间】:2017-08-14 04:10:58
【问题描述】:

例如,在这个方法中,我使用 console.log() 来登录到控制台以进行调试

_onSearchTextChanged = (event) => {
    console.log('_onSearchTextChanged');
    ...
};

但 Visual Studio Code 在控制台中没有显示任何内容

【问题讨论】:

    标签: logging console visual-studio-code


    【解决方案1】:

    如果你在visual studio code中使用debug模式,可以添加选项:

    {
     "outputCapture": "std"
    }
    

    这会将您的日志重定向到调试控制台中。

    【讨论】:

    【解决方案2】:

    launch.json 内部(使用F1 打开),在configurations 下,使用stdconsole 添加outputCapture 属性(如果不存在),如下所示:

    {
      ...
      "configurations": [
        {
          ...
          "outputCapture": "std", // or "console" 
        }
      ]
    }
    

    至于"std",这是documentation要说的:

    outputCapture - 如果设置为 std,则从进程 stdout/stderr 输出 将显示在调试控制台中,而不是监听输出 通过调试端口。这对于程序或日志库很有用 直接写入 stdout/stderr 流而不是使用 console.* API。

    另外请注意,使用std 会显示完整的错误(对于 VSCode 1.49.0)。比如创建一个包含错误的js文件:

    console.log(a) // error: a is undefined
    

    使用std

    c: \Users\path\to\file.js: 1
    console.log(a) // error: a is undefined
        ^
    
    ReferenceError: a is not defined
    at Object.<anonymous>(c: \Users\path\to\file.js: 1: 13)
    at Module._compile(internal / modules / cjs / loader.js: 1158: 30)
    at Object.Module._extensions..js(internal / modules / cjs / loader.js: 1178: 10)
    at Module.load(internal / modules / cjs / loader.js: 1002: 32)
    at Function.Module._load(internal / modules / cjs / loader.js: 901: 14)
    at Function.executeUserEntryPoint[as runMain](internal / modules / run_main.js: 74: 12)
    at internal / main / run_main_module.js: 18: 47
    

    使用console:

    Uncaught ReferenceError: a is not defined
    

    所以在我看来,std 更好一些。

    【讨论】:

      【解决方案3】:

      如果您正在浏览器中运行和测试您的代码,请按“F12”(适用于谷歌浏览器)以查看浏览器中的日志。 如果您在调试模式下运行,则在 Visual Studio Code 中只会显示日志。在菜单栏中有 Debug 选项可以在调试模式下运行,或者您可以找到完整的参考here

      【讨论】:

      • 这不是问题的答案
      猜你喜欢
      • 1970-01-01
      • 2021-04-13
      • 2019-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-13
      相关资源
      最近更新 更多