2013-07-31 55 views
18

我試圖讓溫斯頓靚打印到控制檯上,所以我堅持這樣的文件,並與節點運行它:溫斯頓不漂亮地打印到控制檯

var winston = require('winston'); 
winston.cli(); 
winston.data({ 
    a: "test", 
    of: "many", 
    properties: { 
    like: "this" 
    } 
}); 
winston.data('data', { 
    a: "test", 
    of: "many", 
    properties: { 
    like: "this" 
    } 
}); 

終端回吐以下(不完全漂亮)消息:

data:  a=test, of=many, like=this 
data: data a=test, of=many, like=this 

我下面(「在CLI工具使用溫斯頓」)在Winston Readme的說明。我誤讀了什麼嗎?在某處丟失設置?

回答

25

我想出了答案(文檔不正確)。如果使用構造函數並手動添加傳輸,則可以爲winston和單個傳輸設置選項。某些選項需要直接添加到winston,而其他選項需要添加到運輸。

例如爲:

var winston = require('winston'); 
var logger = new (winston.Logger)({ 
    levels: { 
    trace: 0, 
    input: 1, 
    verbose: 2, 
    prompt: 3, 
    debug: 4, 
    info: 5, 
    data: 6, 
    help: 7, 
    warn: 8, 
    error: 9 
    }, 
    colors: { 
    trace: 'magenta', 
    input: 'grey', 
    verbose: 'cyan', 
    prompt: 'grey', 
    debug: 'blue', 
    info: 'green', 
    data: 'grey', 
    help: 'cyan', 
    warn: 'yellow', 
    error: 'red' 
    } 
}); 

logger.add(winston.transports.Console, { 
    level: 'trace', 
    prettyPrint: true, 
    colorize: true, 
    silent: false, 
    timestamp: false 
}); 

logger.add(winston.transports.File, { 
    prettyPrint: false, 
    level: 'info', 
    silent: false, 
    colorize: true, 
    timestamp: true, 
    filename: './nKindler.log', 
    maxsize: 40000, 
    maxFiles: 10, 
    json: false 
}); 
+6

不應水平的數字是按相反的順序?例如。錯誤= 0和跟蹤= 9 https://github.com/winstonjs/winston#logging-levels – Lukas

+0

這不適用於自定義格式化程序。 – theusguy

2

我把@ partycoder答案,並修剪下來只使用附帶溫斯頓默認的日誌記錄級別。它也不會顛倒錯誤的順序。 0 =最高優先級。

winston.addColors({ 
    silly: 'magenta', 
    debug: 'blue', 
    verbose: 'cyan', 
    info: 'green', 
    warn: 'yellow', 
    error: 'red' 
}); 

winston.remove(winston.transports.Console); 
winston.add(winston.transports.Console, { 
    level: process.env.LOG_LEVEL, 
    prettyPrint: true, 
    colorize: true, 
    silent: false, 
    timestamp: false 
}); 
0

如果您使用[email protected],那麼接受的答案將不起作用。請嘗試以下操作:

const winston = require("winston"); 
let date = new Date().toISOString(); 
const logFormat = winston.format.printf(function(info) { 
    return `${date}-${info.level}: ${JSON.stringify(info.message, null, 4)}\n`; 
}); 
const logger = new winston.createLogger({ 
    transports: [ 
    new winston.transports.Console({ 
     level: level, 
     format: winston.format.combine(winston.format.colorize(), logFormat) 
    }) 
    ] 
}); 

日誌將具有以下格式:

它的彩色BTW

2018-03-01T19:49:54.042Z-info: "----- Customer Details ------" 

2018-03-01T19:49:54.042Z-info: [ 
    { 
     "A": 1, 
     "B": 2 
    } 
]