我正在尝试 Node bunyan模块。作为其中的一部分,我想了解一些有关 Bunyan 的事实..
bunyan 是否提供了一种更改bunyan 日志上打印内容顺序的方法。例如,默认情况下时间戳将在末尾打印...有没有办法在开始时打印它?如果是,请与我分享..
Bunyan 日志将通过在应用程序中指定路径记录到文件中。我们可以在属性文件中的其他地方指定,而不是在应用程序中指定。如果是这样,请分享如何做到这一点...
最佳答案
对于#1,我假设您正在使用bunyan-cli。您可以更改一些内容,但我认为您无法更改顺序。它确实有一个格式化程序,您可以使用 node index.js | Bunyan -o Short
来更改您的输出:
[2015-05-13T22:55:28.613Z] INFO: App/sampleObject/77405 on host.local: User logged in (reqId=1, user=frank)
[2015-05-13T22:55:28.615Z] INFO: App/sampleObject/77405 on host.local: User queried DB (reqId=1, user=frank)
对此:
22:55:15.830Z INFO App/sampleObject: User logged in (reqId=1, user=frank)
22:55:15.832Z INFO App/sampleObject: User queried DB (reqId=1, user=frank)
我发现它更具可读性。
对于#2,您需要在应用程序启动时从配置文件设置一个日志记录实例。如下所示:
var bunyan = require('bunyan');
var configOptions = require('../path/to/config.json');
var logger = bunyan.createLogger( configOptions );
bunyan.log = logger;
有更好的策略来加载配置文件,但单个 JSON 文件就可以了。如果您想设置 process.stdout
流,您可能需要比纯 JSON 提供的更多选项,因此在这种情况下 config.js
文件会更好。
在其他文件中,您可以像这样访问日志:
var log = require('bunyan').log;
log.info('This is another file.`);
请务必在需要其他文件之前配置记录器,否则日志记录对象 log
将无法正确初始化。
NOTE: You can also add a stream to a Bunyan logger dynamically. This is not in their documentation (so maybe use at your own risk), but for a given
logger
, you can make the calllogger.addStream( streamConfigObj )
wherestreamConfigObj
is the same object you would use instream
orstream:[]
to.createLogger
关于node.js - 在 Bunyan 日志记录中格式化输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30348762/