简体   繁体   English

在Bunyan控制台中显示调试消息

[英]Show debug messages in console in bunyan

child.log.info('info');
child.log.debug('debug');

I use the following command: 我使用以下命令:

node app.js | bunyan -o short -l debug

However it only shows INFO. 但是,它仅显示INFO。 Does not show DEBUG. 不显示DEBUG。 I'd like ot display debug messages in the console, what am i doing wrong? 我想在控制台中显示调试消息,我在做什么错?

When you create a logger, use the level option to set the level at which bunyan will emit log messages: 创建记录器时,请使用level选项设置bunyan发出日志消息的级别:

var log = bunyan.createLogger({ level: "debug" });

Similarly, use the level option to set the level at which child loggers will emit log messages: 同样,使用level选项设置子记录器发出日志消息的级别:

var child = log.child({ level: "debug" });

This level option set at the time of instantiation determines the minimum level of log message emitted by bunyan. 在实例化时设置的此level选项确定bunyan发出的日志消息的最低级别。

The command line switch -l ( or --level ) is simply a filter; 命令行开关-l (或--level )只是一个过滤器; it allows through messages emitted at a given level or above. 它允许通过以给定级别或更高级别发出的消息。 But, it does not set the minimum level of log message emitted by your instance of bunyan. 但是,它不会设置您的bunyan实例发出的日志消息的最低级别。

For example, suppose I want an instance of bunyan that emits log messages at the "debug" level or higher. 例如,假设我想要一个Bunyan实例,该实例以"debug"级别或更高级别发出日志消息。 I can put the following in main.js : 我可以在main.js放入以下main.js

var bunyan = require("bunyan");
var log = bunyan.createLogger({ level: "debug" });

log.trace("Nitty gritty info here.");
log.debug("Eek ork op.");
log.info("The Red Zone is for loading and unloading only.");

If I run the following from the command line: 如果我从命令行运行以下命令:

node main.js | bunyan

I will see this in the output: 我将在输出中看到:

Eek ork op.
The Red Zone is for loading and unloading only.

Notice I didn't filter anything from the command line and the call to log.trace() didn't print anything, either. 注意,我没有从命令行过滤任何内容,并且对log.trace()的调用也没有打印任何内容。

Now, type this into the command line: 现在,在命令行中输入以下内容:

node main.js | bunyan -l "debug"

You'll get: 你会得到:

Eek ork op.
The Red Zone is for loading and unloading only.

The same as before. 和以前一样。

Now, try: 现在,尝试:

node main.js | bunyan -l "trace"

You'll still get the same output as before. 您仍将获得与以前相同的输出。

Why? 为什么? Because your instance of bunyan is set to emit messages at the "debug" level or higher. 因为您的bunyan实例设置为在"debug"级别或更高级别发出消息。 The command-line switch -l is just a filter. 命令行开关-l只是一个过滤器。

Finally, try: 最后,尝试:

node main.js | bunyan -l "info"

This time, you'll only see: 这次,您只会看到:

The Red Zone is for loading and unloading only.

The call to log.debug() was filtered out. log.debug()的调用已被滤除。

While creating streams for different log levels, creating a stream for debug like 在为不同的日志级别创建流时,为调试创建流,例如

 bunyan.createLogger({
    name: <name>,
    streams: [{
        level : 'debug',
        stream : process.stdout
    }]
});

Using this all your debugging logs would be streamed to the console. 使用此功能,所有调试日志将流式传输到控制台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM