温斯顿并不适合打印机

时间:2021-02-03 21:44:48

I'm trying to get Winston to pretty print to the console, so I stuck this in a file and ran it with node:

我试图让Winston打印到控制台,所以我把它放在一个文件中并用节点运行它:

var winston = require('winston');
winston.cli();
winston.data({
  a: "test",
  of: "many",
  properties: {
    like: "this"
  }
});
winston.data('data', {
  a: "test",
  of: "many",
  properties: {
    like: "this"
  }
});

The terminal spit back the following (not exactly pretty) messages:

终端吐出以下(不完全相同)消息:

data:     a=test, of=many, like=this
data:    data a=test, of=many, like=this

I'm following the instructions on the Winston Readme ("Using winston in a CLI tool"). Am I misreading something? Missing a setting somewhere?

我按照Winston自述文件中的说明(“在CLI工具中使用winston”)。我误读了什么吗?在某个地方错过了设置?

3 个解决方案

#1


26  

I figured out the answer (the documentation is incorrect). If you use the constructor, and manually add transports, you can set options, both for winston, and for individual transports. Certain options need to be added to winston directly, while others need to be added to the transport.

我想出了答案(文档不正确)。如果使用构造函数并手动添加传输,则可以为winston和单个传输设置选项。某些选项需要直接添加到winston,而其他选项需要添加到传输中。

E.g.:

例如。:

var winston = require('winston');
var logger = new (winston.Logger)({
  levels: {
    trace: 0,
    input: 1,
    verbose: 2,
    prompt: 3,
    debug: 4,
    info: 5,
    data: 6,
    help: 7,
    warn: 8,
    error: 9
  },
  colors: {
    trace: 'magenta',
    input: 'grey',
    verbose: 'cyan',
    prompt: 'grey',
    debug: 'blue',
    info: 'green',
    data: 'grey',
    help: 'cyan',
    warn: 'yellow',
    error: 'red'
  }
});

logger.add(winston.transports.Console, {
  level: 'trace',
  prettyPrint: true,
  colorize: true,
  silent: false,
  timestamp: false
});

logger.add(winston.transports.File, {
  prettyPrint: false,
  level: 'info',
  silent: false,
  colorize: true,
  timestamp: true,
  filename: './nKindler.log',
  maxsize: 40000,
  maxFiles: 10,
  json: false
});

#2


2  

I took @partycoder's answer and trimmed it down to use only the default logging levels that come with winston. It also doesn't reverse the order of the errors. 0 = highest priority.

我拿了@ partycoder的答案并将其修剪下来,只使用winston附带的默认日志记录级别。它也不会颠倒错误的顺序。 0 =最高优先级。

winston.addColors({
  silly: 'magenta',
  debug: 'blue',
  verbose: 'cyan',
  info: 'green',
  warn: 'yellow',
  error: 'red'
});

winston.remove(winston.transports.Console);
winston.add(winston.transports.Console, {
  level: process.env.LOG_LEVEL,
  prettyPrint: true,
  colorize: true,
  silent: false,
  timestamp: false
});

#3


1  

If you're using winston@3.0.0-rc0 then the accepted answer won't work. Try the following:

如果您使用的是winston@3.0.0-rc0,那么接受的答案将无效。请尝试以下方法:

const winston = require("winston");
let date = new Date().toISOString();
const logFormat = winston.format.printf(function(info) {
  return `${date}-${info.level}: ${JSON.stringify(info.message, null, 4)}\n`;
});
const logger = new winston.createLogger({
  transports: [
    new winston.transports.Console({
      level: level,
      format: winston.format.combine(winston.format.colorize(), logFormat)
    })
  ]
});

The logs will have the following format:

日志将具有以下格式:

It's colored BTW

它的颜色是BTW

2018-03-01T19:49:54.042Z-info: "----- Customer Details ------"

2018-03-01T19:49:54.042Z-info: [
    {
        "A": 1,
        "B": 2
    }
]

#1


26  

I figured out the answer (the documentation is incorrect). If you use the constructor, and manually add transports, you can set options, both for winston, and for individual transports. Certain options need to be added to winston directly, while others need to be added to the transport.

我想出了答案(文档不正确)。如果使用构造函数并手动添加传输,则可以为winston和单个传输设置选项。某些选项需要直接添加到winston,而其他选项需要添加到传输中。

E.g.:

例如。:

var winston = require('winston');
var logger = new (winston.Logger)({
  levels: {
    trace: 0,
    input: 1,
    verbose: 2,
    prompt: 3,
    debug: 4,
    info: 5,
    data: 6,
    help: 7,
    warn: 8,
    error: 9
  },
  colors: {
    trace: 'magenta',
    input: 'grey',
    verbose: 'cyan',
    prompt: 'grey',
    debug: 'blue',
    info: 'green',
    data: 'grey',
    help: 'cyan',
    warn: 'yellow',
    error: 'red'
  }
});

logger.add(winston.transports.Console, {
  level: 'trace',
  prettyPrint: true,
  colorize: true,
  silent: false,
  timestamp: false
});

logger.add(winston.transports.File, {
  prettyPrint: false,
  level: 'info',
  silent: false,
  colorize: true,
  timestamp: true,
  filename: './nKindler.log',
  maxsize: 40000,
  maxFiles: 10,
  json: false
});

#2


2  

I took @partycoder's answer and trimmed it down to use only the default logging levels that come with winston. It also doesn't reverse the order of the errors. 0 = highest priority.

我拿了@ partycoder的答案并将其修剪下来,只使用winston附带的默认日志记录级别。它也不会颠倒错误的顺序。 0 =最高优先级。

winston.addColors({
  silly: 'magenta',
  debug: 'blue',
  verbose: 'cyan',
  info: 'green',
  warn: 'yellow',
  error: 'red'
});

winston.remove(winston.transports.Console);
winston.add(winston.transports.Console, {
  level: process.env.LOG_LEVEL,
  prettyPrint: true,
  colorize: true,
  silent: false,
  timestamp: false
});

#3


1  

If you're using winston@3.0.0-rc0 then the accepted answer won't work. Try the following:

如果您使用的是winston@3.0.0-rc0,那么接受的答案将无效。请尝试以下方法:

const winston = require("winston");
let date = new Date().toISOString();
const logFormat = winston.format.printf(function(info) {
  return `${date}-${info.level}: ${JSON.stringify(info.message, null, 4)}\n`;
});
const logger = new winston.createLogger({
  transports: [
    new winston.transports.Console({
      level: level,
      format: winston.format.combine(winston.format.colorize(), logFormat)
    })
  ]
});

The logs will have the following format:

日志将具有以下格式:

It's colored BTW

它的颜色是BTW

2018-03-01T19:49:54.042Z-info: "----- Customer Details ------"

2018-03-01T19:49:54.042Z-info: [
    {
        "A": 1,
        "B": 2
    }
]