簡體   English   中英

使用console.log登錄到特定的控制台

[英]log to specific console with console.log

您如何從表達的任何位置確定邏輯從哪個連接發起?

我正在使用Express,並且在我的代碼中做了很多邏輯工作。 為了讓用戶知道發生了什么,我幾乎在所有地方都使用控制台日志。 這樣,如果邏輯是通過命令行運行的,它只會打印到控制台,並且如果邏輯通過Express url路由運行,我將覆蓋控制台,如下所示:

console.log = (print) => {
    res.write(print + '<br>')
};

非常簡單,但是當兩個同時運行時,最新的一個會竊取控制台日志功能,因此它開始從兩個進程中打印控制台日志。 關於如何在不將特殊打印功能作為參數傳遞給我所有功能的情況下提出任何建議?

也許使用一個全局打印對象來檢測它在哪個節點進程中開始,並且僅打印來自該源的打印語句,但是那您將如何關閉連接呢?

使用npm模塊winston https://www.npmjs.com/package/winston

var winston = require('winston');

  winston.log('info', 'Hello distributed log files!');
  winston.info('Hello again distributed logs');

  winston.level = 'debug';
  winston.log('debug', 'Now my debug messages are written to console!');

並提供一些運輸https://github.com/winstonjs/winston/blob/master/docs/transports.md

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM