我可以在winston中更改日志数据的颜色吗?

时间:2022-11-21 08:54:41

I happened to use bunyan to log the the data . I wanted the logs be printed with appropriate colors like errors in red , debug yellow .. etc; unfortunately I couldn't find anyways to do that . And now I would like to know if its possible with winston. Can I change the color of log data in winston ?

我碰巧使用bunyan来记录数据。我希望日志打印出适当的颜色,如红色错误,调试黄色等等;不幸的是我无法找到那样做。现在我想知道它是否可能与温斯顿。我可以在winston中更改日志数据的颜色吗?

here is the code that I executed .

这是我执行的代码。

  var logger = require("winston-color");
  var winston = require('winston');  
  var util    = require('util');

  var logFilename = __dirname + '/logfile.log';

  var logger = new (winston.Logger)({
    transports: [
      new (winston.transports.Console)(),
      new (winston.transports.File)({ 
      filename: 'logfile.log',
      timestamp:true 
     }),
      new (winston.transports.File)({
      name: 'error-log',
      filename: 'error.log',
      level: 'error'
      }),

     new (winston.transports.File)({
     name: 'info-log',
     filename: 'info.log',
     level: 'info'
     }),
    ]
  });
  logger.info('Hello Winston info!');
  logger.debug('Hello Winston debug!');
  logger.warn('Hello Winston warn!');
  logger.info('Hello again distributed logs'); 
  logger.error('error1');
  logger.error('error2');

the output screen shot here

输出屏幕在这里拍摄

working code's output here here

在这里工作代码的输出

4 个解决方案

#1


0  

I have not tried this. But according to this you use color for logs. enter link description here

我没试过这个。但根据这个你使用颜色的日志。在此输入链接描述

#2


0  

Yes you can. You can use the following code that I am using in my project.

是的你可以。您可以使用我在项目中使用的以下代码。

logger/WinstonPlugin.js

记录器/ WinstonPlugin.js

/* jslint node: true */
/* jshint esversion: 6 */

'use strict';
const Winston = require('winston');
const logLevel = 'debug';

var logger;

(function createLogger() {

    logger = new(Winston.Logger)({
        transports: [
            new(Winston.transports.Console)({
                level: logLevel,
                colorize: true,
                timestamp: function () {
                    return (new Date()).toLocaleTimeString();
                },
                prettyPrint: true
            })
        ]
    });

    Winston.addColors({
        error: 'red',
        warn: 'yellow',
        info: 'cyan',
        debug: 'green'
    });
})();

module.exports = logger;

And anytime you needed the Winston in any your code file. You can access like below:

任何时候你需要任何代码文件中的温斯顿。你可以访问如下:

const Winston = require('logger/WinstonPlugin');
Winston.info('This is a info statement');
Winston.debug('This is a debug statement');
Winston.debug('This is a warning statement');
Winston.error('This is a error statement');

and you will see the colored output in the console.

您将在控制台中看到彩色输出。

#3


0  

Tried your fix; Winston doesn't pay attention to any of the color settings.

试过你的修复;温斯顿不注意任何颜色设置。

#4


0  

Why not use log4js instead? Here's a complete example:

为什么不使用log4js呢?这是一个完整的例子:

/* logger.js (tested with log4js 2.5.3) */
const log4js = require('log4js')
const logger = function(topic) {
    const f = `logs/${topic}-${new Date().toISOString().substr(0, 10)}.log`
    log4js.configure({
        appenders: {
            out: { type: 'stdout'},
            [topic]: {
                type: 'file', filename: f, maxLogSize: 128*1024*1024, backups: 10, compress: false
            }
        },
        categories: {
            default: { appenders: ['out', topic], level: 'debug' }
        }
    })
    return log4js.getLogger(topic)

}
module.exports=logger

/* app.js */
const logger = require('./logger')('some-topic')

logger.info('loaded')
logger.warn('no such command')
logger.error('oops some problem')

#1


0  

I have not tried this. But according to this you use color for logs. enter link description here

我没试过这个。但根据这个你使用颜色的日志。在此输入链接描述

#2


0  

Yes you can. You can use the following code that I am using in my project.

是的你可以。您可以使用我在项目中使用的以下代码。

logger/WinstonPlugin.js

记录器/ WinstonPlugin.js

/* jslint node: true */
/* jshint esversion: 6 */

'use strict';
const Winston = require('winston');
const logLevel = 'debug';

var logger;

(function createLogger() {

    logger = new(Winston.Logger)({
        transports: [
            new(Winston.transports.Console)({
                level: logLevel,
                colorize: true,
                timestamp: function () {
                    return (new Date()).toLocaleTimeString();
                },
                prettyPrint: true
            })
        ]
    });

    Winston.addColors({
        error: 'red',
        warn: 'yellow',
        info: 'cyan',
        debug: 'green'
    });
})();

module.exports = logger;

And anytime you needed the Winston in any your code file. You can access like below:

任何时候你需要任何代码文件中的温斯顿。你可以访问如下:

const Winston = require('logger/WinstonPlugin');
Winston.info('This is a info statement');
Winston.debug('This is a debug statement');
Winston.debug('This is a warning statement');
Winston.error('This is a error statement');

and you will see the colored output in the console.

您将在控制台中看到彩色输出。

#3


0  

Tried your fix; Winston doesn't pay attention to any of the color settings.

试过你的修复;温斯顿不注意任何颜色设置。

#4


0  

Why not use log4js instead? Here's a complete example:

为什么不使用log4js呢?这是一个完整的例子:

/* logger.js (tested with log4js 2.5.3) */
const log4js = require('log4js')
const logger = function(topic) {
    const f = `logs/${topic}-${new Date().toISOString().substr(0, 10)}.log`
    log4js.configure({
        appenders: {
            out: { type: 'stdout'},
            [topic]: {
                type: 'file', filename: f, maxLogSize: 128*1024*1024, backups: 10, compress: false
            }
        },
        categories: {
            default: { appenders: ['out', topic], level: 'debug' }
        }
    })
    return log4js.getLogger(topic)

}
module.exports=logger

/* app.js */
const logger = require('./logger')('some-topic')

logger.info('loaded')
logger.warn('no such command')
logger.error('oops some problem')