如何在winston中记录JavaScript对象和数组作为控制台。日志吗?

时间:2022-12-29 16:56:10

I was looking at top Node logging systems: npmlog, log4js, bunyan and winston and decided to use winston for having the most npm monthly downloads.

我查看了*节点日志记录系统:npmlog、log4js、bunyan和winston,并决定使用winston来获得npm每月最多的下载。

What I want to set up is custom logger which I will be able to use on development environment with logger.debug(...) which won't log anything on production environment. This will help me so when I'm on development environment, I won't need to write anything since I'll see all the outputs.

我想设置的是自定义日志记录器,我可以使用它在开发环境中使用logger.debug(…),它不会在生产环境中记录任何内容。这将帮助我在开发环境中,我不需要写任何东西,因为我将看到所有的输出。

This is what I have now:

这就是我现在所拥有的:

var level = 'debug';
if (process.env.NODE_ENV !== 'development'){
  level = 'production'; // this will never be logged!
}

var logger = new winston.Logger({
  transports: [
    // some other loggings
    new winston.transports.Console({
      name: 'debug-console',
      level: level,
      prettyPrint: true,
      handleExceptions: true,
      json: false,
      colorize: true
    })

  ],
  exitOnError: false // don't crush no error
});

Problem occurs when I'm trying to log JavaScript Object or Javascript Array. With Object, I need to do toJSON(), and for Array I need first JSON.stringify() and then JSON.parse().

当我尝试记录JavaScript对象或JavaScript数组时出现问题。对于对象,我需要执行toJSON(),对于数组,我需要首先执行JSON.stringify(),然后执行JSON.parse()。

It's not nice to write all the time this methods, just to log something that I want. Furthermore, it's not even resource-friendly, because those formatting methods need to be executed before logger.debug() realises that it's on production and that it shouldn't log it in the first place (basically, it's evaluating arguments before function call). I just like how old-fashined console.log() logs JavaScript objects and arrays.

一直写这些方法是不好的,只是为了记录我想要的东西。此外,它甚至对资源不友好,因为这些格式化方法需要在logger.debug()意识到它在生产环境中,并且不应该首先记录它(基本上,它是在函数调用之前计算参数)之前执行。我喜欢老式的console.log()记录JavaScript对象和数组。

Now, as I'm writing this question, I found that there is a way of describing custom format for every winston transports object. Is that the way of doing it, or is there some other way?

现在,当我写这个问题时,我发现有一种方法可以描述每个winston传输对象的自定义格式。是这样做的,还是有别的方法?

5 个解决方案

#1


11  

try changing prettyPrint parameter to

试着将漂亮打印参数更改为

prettyPrint: function ( object ){
    return JSON.stringify(object);
}

#2


7  

logger.log("info", "Starting up with config %j", config);

Winstons uses the built-in utils.format library. https://nodejs.org/dist/latest/docs/api/util.html#util_util_format_format_args

Winstons使用内置的utils。格式库。https://nodejs.org/dist/latest/docs/api/util.html util_util_format_format_args

#3


4  

Use the built-in Node.js function util.format to convert your objects to strings in the same way that console.log does.

使用内置的节点。js函数跑龙套。将对象转换为字符串的格式与控制台相同。日志。

#4


1  

My recommendation is to write your own abstraction on top of winston that has a convenience method for printing your objects for debugging.

我的建议是在winston上编写您自己的抽象,它有一个方便的方法来打印您的对象进行调试。

You may also look at this response for a hint of how the method could be developed.

您还可以查看此响应以了解如何开发该方法。

https://*.com/a/12620543/2211743

https://*.com/a/12620543/2211743

#5


0  

As Leo already pointed out in his answer, Winston makes use of String Interpolation provided by util.format:

正如Leo在他的回答中已经指出的,Winston使用了util.format提供的字符串插值:

const winston = require("winston");                                                                                                                                                                                                    
const logger = new winston.Logger({                                                                                                                                                                                                    
  transports: [                                                                                                                                                                                                                        
    // some other loggings                                                                                                                                                                                                             
    new winston.transports.Console({                                                                                                                                                                                                   
      name: "debug-console",                                                                                                                                                                                                           
      level: process.env.LOGLEVEL || "info",                                                                                                                                                                                           
      prettyPrint: true,                                                                                                                                                                                                               
      handleExceptions: true,                                                                                                                                                                                                          
      json: false,                                                                                                                                                                                                                     
      colorize: true                                                                                                                                                                                                                   
    })                                                                                                                                                                                                                                 
  ],                                                                                                                                                                                                                                   
  exitOnError: false // don't crush no error                                                                                                                                                                                           
});                                                                                                                                                                                                                                    

const nestedObj = {                                                                                                                                                                                                                    
  foo: {                                                                                                                                                                                                                               
    bar: {                                                                                                                                                                                                                             
      baz: "example"                                                                                                                                                                                                                   
    }                                                                                                                                                                                                                                  
  }                                                                                                                                                                                                                                    
};                                                                                                                                                                                                                                     

const myString = "foo";                                                                                                                                                                                                                

logger.log("info", "my nested object: %j. My string: %s", nestedObj, myString);                                                                                                                                                                                                                                  

When calling logger.log, you can define placeholders which will be appropriately replaced. %j will be replaced by the equivalent of JSON.stringify(nestedObj)

当调用logger。日志,您可以定义占位符,它将被适当地替换。%j将被等效的JSON.stringify(nestedObj)代替

#1


11  

try changing prettyPrint parameter to

试着将漂亮打印参数更改为

prettyPrint: function ( object ){
    return JSON.stringify(object);
}

#2


7  

logger.log("info", "Starting up with config %j", config);

Winstons uses the built-in utils.format library. https://nodejs.org/dist/latest/docs/api/util.html#util_util_format_format_args

Winstons使用内置的utils。格式库。https://nodejs.org/dist/latest/docs/api/util.html util_util_format_format_args

#3


4  

Use the built-in Node.js function util.format to convert your objects to strings in the same way that console.log does.

使用内置的节点。js函数跑龙套。将对象转换为字符串的格式与控制台相同。日志。

#4


1  

My recommendation is to write your own abstraction on top of winston that has a convenience method for printing your objects for debugging.

我的建议是在winston上编写您自己的抽象,它有一个方便的方法来打印您的对象进行调试。

You may also look at this response for a hint of how the method could be developed.

您还可以查看此响应以了解如何开发该方法。

https://*.com/a/12620543/2211743

https://*.com/a/12620543/2211743

#5


0  

As Leo already pointed out in his answer, Winston makes use of String Interpolation provided by util.format:

正如Leo在他的回答中已经指出的,Winston使用了util.format提供的字符串插值:

const winston = require("winston");                                                                                                                                                                                                    
const logger = new winston.Logger({                                                                                                                                                                                                    
  transports: [                                                                                                                                                                                                                        
    // some other loggings                                                                                                                                                                                                             
    new winston.transports.Console({                                                                                                                                                                                                   
      name: "debug-console",                                                                                                                                                                                                           
      level: process.env.LOGLEVEL || "info",                                                                                                                                                                                           
      prettyPrint: true,                                                                                                                                                                                                               
      handleExceptions: true,                                                                                                                                                                                                          
      json: false,                                                                                                                                                                                                                     
      colorize: true                                                                                                                                                                                                                   
    })                                                                                                                                                                                                                                 
  ],                                                                                                                                                                                                                                   
  exitOnError: false // don't crush no error                                                                                                                                                                                           
});                                                                                                                                                                                                                                    

const nestedObj = {                                                                                                                                                                                                                    
  foo: {                                                                                                                                                                                                                               
    bar: {                                                                                                                                                                                                                             
      baz: "example"                                                                                                                                                                                                                   
    }                                                                                                                                                                                                                                  
  }                                                                                                                                                                                                                                    
};                                                                                                                                                                                                                                     

const myString = "foo";                                                                                                                                                                                                                

logger.log("info", "my nested object: %j. My string: %s", nestedObj, myString);                                                                                                                                                                                                                                  

When calling logger.log, you can define placeholders which will be appropriately replaced. %j will be replaced by the equivalent of JSON.stringify(nestedObj)

当调用logger。日志,您可以定义占位符,它将被适当地替换。%j将被等效的JSON.stringify(nestedObj)代替