温斯顿不会登录颜色
Winston won't log in color
这不会以彩色打印到控制台。一切都是白色的。我已经谷歌搜索了几个小时。我是 运行 Ubuntu 17.
// 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
我没有收到任何错误消息或任何内容。就是白色。
Edit: After answering my own question, I was told through a conversation on their github page that you can actually pass formats to the transports themselves.
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
这不会以彩色打印到控制台。一切都是白色的。我已经谷歌搜索了几个小时。我是 运行 Ubuntu 17.
// 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
我没有收到任何错误消息或任何内容。就是白色。
Edit: After answering my own question, I was told through a conversation on their github page that you can actually pass formats to the transports themselves.
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