簡體   English   中英

如何停止主管從Node.js服務器的日志輸出中剝離顏色?

[英]How do I stop supervisord from stripping colors from the log output of my Node.js server?

我正在使用超級用戶來管理Node.js服務器(確保在發生崩潰時重新啟動並發送崩潰警報電子郵件)。 但是,我發現如果我通過supervisor運行我的app.js進程,則server.logconsole的輸出都是彩色的。 我正在使用Winston庫來處理我的日志記錄。 我在下面有一些輸出示例:

內容server.log通過運行之后supervisord

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

通過外殼( $ node app.js )運行后server.log內容:

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監視我的Node服務器,那么顏色也會從那里剝離。 從外殼運行它時,我可以在控制台中看到顏色輸出。

有誰知道為什么會這樣以及我如何解決這個問題?

編輯 :由於有人要求我的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;

我在超級用戶堆棧交換上找到了此問題的答案。

引用一下:

只需在任何命令之前插入unbuffer即可,即使它實際上正在傳遞到另一個可執行文件中,也可以認為它正在寫入交互式輸出。 ls的情況下,這將保留顏色。

例如

unbuffer ls -l --color=auto | tee output.log

如果尚未安裝,則可以在Ubuntu和其他Debian-ish Linux發行版上安裝unbuffer

sudo apt-get install expect-dev

暫無
暫無

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

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