2017-10-10 89 views
0

這不會以彩色方式打印到控制檯。一切都是白色的。我一直在Google上搜索幾個小時。我正在運行Ubuntu 17.Winston將不會登錄顏色

// Logging with Winston 
const os = require('os') 
const fs = require('fs') 
const path = require('path') 
const config = require('../data/config/config') 
const winston = require('winston') 
const { createLogger, format, transports } = winston 
const tsFormat =() => (new Date()).toLocaleTimeString() 

const logDir = path.resolve(os.homedir(), '.test-logs') 

if (!fs.existsSync(logDir)) { 
    fs.mkdirSync(logDir) 
} 

const logger = createLogger({ 
    format: format.combine(
    format.splat(), 
    format.simple() 
), 
    transports: [ 
    new transports.Console({ timestamp: tsFormat, level: config.logLevel, colorize: true }), 
    new transports.File({ filename: path.resolve(logDir, 'info.log'), level: 'info' }), 
    new transports.File({ filename: path.resolve(logDir, 'error.log'), level: 'error' }) 
    ] 
}) 

module.exports = logger 

我沒有收到任何錯誤消息或任何內容。它只是白色的。

回答

1

編輯:回答我的問題後,我通過他們github頁面上的談話說,你實際上可以通過格式的傳輸自己。

const logger = createLogger({ 
    transports: [ 
    new transports.Console({ level: config.logLevel, format: customFormat, colorize: true, handleExceptions: true, humanReadableUnhandledException: true }), 
    new transports.File({ filename: path.resolve(logDir, 'server.log'), level: 'silly' }), 
    new transports.File({ filename: path.resolve(logDir, 'error.log'), level: 'error', handleExceptions: true }) 
    ] 
}) 

原來的答案

您必須添加實例化時記錄到上色的格式。在他們的文檔中沒有這樣的例子。他們提到的名爲logForm的回購協議完全按照我剛纔的做法完成。它應該是一個超鏈接。我可能會做一個公關。回購是here

我不得不去他們的例子目錄找到這個例子。

// Logging with Winston 
const os = require('os') 
const fs = require('fs') 
const path = require('path') 
const config = require('../data/config/config') 
const winston = require('winston') 
const { createLogger, format, transports } = winston 
const { combine, timestamp, label, printf, colorize } = format 
const myFormat = printf(info => { 
    return `${info.timestamp} [${info.label}] ${info.level}: ${info.message}` 
}) 

const logDir = path.resolve(os.homedir(), '.curator-server') 

if (!fs.existsSync(logDir)) { 
    fs.mkdirSync(logDir) 
} 

const logger = createLogger({ 
    format: combine(
    label({ label: 'Curator Server' }), 
    colorize(), // Here is where the color happens 
    timestamp(), 
    myFormat 
), 
    transports: [ 
    new transports.Console({ level: config.logLevel, colorize: true, handleExceptions: true, humanReadableUnhandledException: true }), 
    new transports.File({ filename: path.resolve(logDir, 'info.log'), level: 'info' }), 
    new transports.File({ filename: path.resolve(logDir, 'error.log'), level: 'error', handleExceptions: true }) 
    ] 
}) 

module.exports = logger