當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Node.js console.trace()用法及代碼示例


console.trace()方法是控製台模塊的內置應用程序編程接口,用於在換行符中將堆棧跟蹤消息打印到stderr。與console.error()方法類似。

用法:

console.trace(message, args);

參數:此方法具有上述和以下所述的兩個參數:

  1. message:此參數指定要打印的消息。
  2. args:它是可選參數,用於指定要在消息中作為替換值傳遞的參數。所有傳遞的參數均發送到util.format()。

返回值:此方法隻返回“ Trace:”字符串,後跟格式化後的消息並以換行符的形式發送到stderr並堆棧跟蹤到代碼中的當前位置,因此不會返回任何內容。

以下示例說明了Node.js中console.trace()方法的用法:



範例1:
文件名:app.js

// Node.js program to demonstrate the  
// console.trace() method 
  
// Accessing console module 
const console = require('console'); 
   
// Calling console.trace() method 
console.trace("stack teace sample"); 
  
console.trace( 
    "stack trace sample with args:%d", 39);

使用以下命令運行app.js文件:

node app.js

輸出:

Trace:stack teace sample
at Object. (C:\nodejs\g\console\console_trace.js:4:9)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Trace:stack trace sample with args:39
at Object. (C:\nodejs\g\console\console_trace.js:5:9)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

範例2:
文件名:app.js

// Node.js program to demonstrate the  
// console.trace() method 
  
// Accessing console module 
const console = require('console'); 
   
// Calling console.trace() method 
console.trace("stack trace message:"
    + "at %s:line no:%d ", "ff()", 96); 
   
var isTrace = true; 
  
console.custom_trace = function(message) {    
  if (isTrace) { 
    console.trace(message); 
  } 
} 
  
console.custom_trace("custom trace message");

使用以下命令運行app.js文件:

node app.js

輸出:

Trace:stack trace message:at ff():line no:96
at Object. (C:\nodejs\g\console\console_trace_1.js:4:9)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Trace:custom trace message
at Console.console.custom_trace (C:\nodejs\g\console\console_trace_1.js:11:13)
at Object. (C:\nodejs\g\console\console_trace_1.js:14:9)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

注意:上麵的程序將通過使用以下命令進行編譯和運行node filename.js命令。

參考: https://nodejs.org/api/console.html#console_console_trace_data_args




相關用法


注:本文由純淨天空篩選整理自anwesha0107大神的英文原創作品 Node.js console.trace() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。