如何阻止 supervisord 从我的 Node.js 服务器的日志输出中去除颜色?
How do I stop supervisord from stripping colors from the log output of my Node.js server?
我正在使用 supervisord 来管理我的 Node.js 服务器(确保它在发生崩溃时重新启动并发送崩溃警报电子邮件)。但是,我发现如果我 运行 我的 app.js
通过 supervisord 处理,我的 server.log
和 console
的输出都被去除了颜色。我正在使用 Winston library 来处理我的日志记录。我在下面有几个输出示例:
从 运行 到 supervisord
后 server.log
的内容:
2015-08-12T20:41:29.203Z - silly: Connected to redis
2015-08-12T20:41:29.206Z - debug: Connected to redis
2015-08-12T20:41:29.206Z - verbose: Connected to redis
2015-08-12T20:41:29.207Z - info: Connected to redis
2015-08-12T20:41:29.207Z - warn: Connected to redis
2015-08-12T20:41:29.207Z - error: Connected to redis
从 运行 到 shell 之后 server.log
的内容($ node app.js
):
2015-08-12T20:41:37.732Z - ^[[35msilly^[[39m: Connected to redis
2015-08-12T20:41:37.737Z - ^[[34mdebug^[[39m: Connected to redis
2015-08-12T20:41:37.741Z - ^[[36mverbose^[[39m: Connected to redis
2015-08-12T20:41:37.742Z - ^[[32minfo^[[39m: Connected to redis
2015-08-12T20:41:37.742Z - ^[[33mwarn^[[39m: Connected to redis
2015-08-12T20:41:37.742Z - ^[[31merror^[[39m: Connected to redis
我还注意到,如果我使用 supervisorctl
中的 tail
来监视我的节点服务器,颜色也会从那里剥离。当从 shell 中 运行ning 时,我可以在控制台中看到颜色输出。
有谁知道为什么会发生这种情况以及我该如何解决这个问题?
编辑:因为有人询问我的 Winston 配置:
var winston = require( 'winston' ),
fs = require( 'fs' ),
logDir = 'logs', // Or read from a configuration
logger;
winston.setLevels( winston.config.npm.levels );
winston.addColors( winston.config.npm.colors );
if ( !fs.existsSync( logDir ) ) {
// Create the directory if it does not exist
fs.mkdirSync( logDir );
}
logger = new( winston.Logger )( {
transports: [
new winston.transports.Console( {
level: 'silly',
colorize: true
} ),
new winston.transports.File( {
level: 'silly',
json: false,
colorize: true,
filename: logDir + '/server.log',
maxsize: 1024 * 1024 * 25 // 25MB
} )
],
exceptionHandlers: [
new winston.transports.File( {
filename: 'log/exceptions.log'
} )
]
} );
module.exports = logger;
我在 Super User Stack Echange 上找到了这个问题的答案。
引用它:
Simply insert unbuffer
before any command to make it think it is
writing to an interactive output even if it is actually piping into
another executable. This will preserve color in the case of ls
.
For example
unbuffer ls -l --color=auto | tee output.log
If you don't already have it installed, on Ubuntu and other Debian-ish Linux distributions you can install unbuffer
by doing.
sudo apt-get install expect-dev
我正在使用 supervisord 来管理我的 Node.js 服务器(确保它在发生崩溃时重新启动并发送崩溃警报电子邮件)。但是,我发现如果我 运行 我的 app.js
通过 supervisord 处理,我的 server.log
和 console
的输出都被去除了颜色。我正在使用 Winston library 来处理我的日志记录。我在下面有几个输出示例:
从 运行 到 supervisord
后 server.log
的内容:
2015-08-12T20:41:29.203Z - silly: Connected to redis
2015-08-12T20:41:29.206Z - debug: Connected to redis
2015-08-12T20:41:29.206Z - verbose: Connected to redis
2015-08-12T20:41:29.207Z - info: Connected to redis
2015-08-12T20:41:29.207Z - warn: Connected to redis
2015-08-12T20:41:29.207Z - error: Connected to redis
从 运行 到 shell 之后 server.log
的内容($ node app.js
):
2015-08-12T20:41:37.732Z - ^[[35msilly^[[39m: Connected to redis
2015-08-12T20:41:37.737Z - ^[[34mdebug^[[39m: Connected to redis
2015-08-12T20:41:37.741Z - ^[[36mverbose^[[39m: Connected to redis
2015-08-12T20:41:37.742Z - ^[[32minfo^[[39m: Connected to redis
2015-08-12T20:41:37.742Z - ^[[33mwarn^[[39m: Connected to redis
2015-08-12T20:41:37.742Z - ^[[31merror^[[39m: Connected to redis
我还注意到,如果我使用 supervisorctl
中的 tail
来监视我的节点服务器,颜色也会从那里剥离。当从 shell 中 运行ning 时,我可以在控制台中看到颜色输出。
有谁知道为什么会发生这种情况以及我该如何解决这个问题?
编辑:因为有人询问我的 Winston 配置:
var winston = require( 'winston' ),
fs = require( 'fs' ),
logDir = 'logs', // Or read from a configuration
logger;
winston.setLevels( winston.config.npm.levels );
winston.addColors( winston.config.npm.colors );
if ( !fs.existsSync( logDir ) ) {
// Create the directory if it does not exist
fs.mkdirSync( logDir );
}
logger = new( winston.Logger )( {
transports: [
new winston.transports.Console( {
level: 'silly',
colorize: true
} ),
new winston.transports.File( {
level: 'silly',
json: false,
colorize: true,
filename: logDir + '/server.log',
maxsize: 1024 * 1024 * 25 // 25MB
} )
],
exceptionHandlers: [
new winston.transports.File( {
filename: 'log/exceptions.log'
} )
]
} );
module.exports = logger;
我在 Super User Stack Echange 上找到了这个问题的答案。
引用它:
Simply insert
unbuffer
before any command to make it think it is writing to an interactive output even if it is actually piping into another executable. This will preserve color in the case ofls
.For example
unbuffer ls -l --color=auto | tee output.log
If you don't already have it installed, on Ubuntu and other Debian-ish Linux distributions you can install
unbuffer
by doing.
sudo apt-get install expect-dev