I have few questions about Node.js Bunyan logging. I'm kinda new the bunyan logging, so please I apologize if i ask any laymen questions.
我对Node有几个问题。js的传教士日志记录。我对班扬日志有点陌生,所以如果我问任何外行问题,请向我道歉。
I'm trying to stream bunyan log output in json format. Primarily in a file and I have plans to stream it a remote host.
我尝试以json格式流bunyan日志输出。主要是在一个文件中,我计划将它作为一个远程主机流。
Here's a simple code that i'm trying:
这里有一个我正在尝试的简单代码:
var bunyan = require("bunyan");
var logger = bunyan.createLogger({
name: "testApp",
streams: [
{
path: "bunayan.log"
}
],
src: true
});
logger.info("Data sent to file");
The output is:
的输出是:
{"name":"testApp","hostname":"xxx.xxx.com","pid":14124,"level":30,"msg":"Data sent to file","time":"2018-05-07T19:14:15.866Z","src":{"file":"/path/to/file/banyan_test.js","line":11},"v":0}
So, i'm trying to format the output like this;
我试着格式化输出,像这样;
- Override the hostname or set a desired hostname
- 重写主机名或设置所需的主机名
- Change "level":30 to "level":"info"
- 改变“级别”:30到“水平”:“信息”
- Change the format of the time json object
- 更改time json对象的格式
- Add additional json object for example:
"attr4": "value"
- 添加额外的json对象,例如:“attr4”:“value”
- Is there any way to change the default json object name such as
time
totimestamp
- 是否有办法将默认的json对象名称(如time)更改为timestamp
I couldn't find any simple or clear example of doing any of the above change. Can anyone please show me some examples to start with? Doesn't need to be all points together but at least a head start or any helpful documentation.
我找不到任何简单或清晰的例子来做上述任何改变。谁能先给我举几个例子吗?不需要所有的点在一起,但至少是一个开头或任何有用的文档。
1 个解决方案
#1
1
Checkout the Banyan API Documentation. All your use cases are covered.
检查Banyan API文档。您的所有用例都包含在内。
- Override the hostname or set a desired hostname
- 重写主机名或设置所需的主机名
Configurable while constructing the logger
在构造日志记录器时可配置。
- Change "level":30 to "level":"info"
- 改变“级别”:30到“水平”:“信息”
- Change the format of the time json object
- 更改time json对象的格式
- Add additional json object for example: "attr4": "value"
- 添加额外的json对象,例如:“attr4”:“value”
- Is there any way to change the default json object name such as time to timestamp
- 是否有办法将默认的json对象名称(如time)更改为timestamp
You can override the json object: See this: https://github.com/trentm/node-bunyan/issues/194
您可以覆盖json对象:查看以下内容:https://github.com/trentm/node-bunyan/issues/194。
var bunyan = require('bunyan');
function modifiedStream(filePath) {
return {
write: log => {
log.level = bunyan.nameFromLevel[log.level];
log.time = new Date().valueOf();
log._timeStamp = new Date().toISOString();
log.myProp = "Some Value" + new Date();
var logLine = JSON.stringify(log, bunyan.safeCycles(), 2);
console.log(logLine);
}
};
}
var logger = bunyan.createLogger({
name: 'myapp',
hostname: "My Test Mac",
streams: [{
type: 'raw',
stream: modifiedStream()
}]
});
logger.info("Hello");
logger.info({
customProp1: "AAA",
customProp2: "BBB"
});
Output:
输出:
{
"name": "myapp",
"hostname": "My Test Mac",
"pid": 89297,
"level": "info",
"msg": "Hello",
"time": 1525813632657,
"v": 0,
"_timeStamp": "2018-05-08T21:07:12.657Z",
"myProp": "Some ValueWed May 09 2018 02:37:12 GMT+0530 (IST)"
}
{
"name": "myapp",
"hostname": "My Test Mac",
"pid": 89297,
"level": "info",
"customProp1": "AAA",
"customProp2": "BBB",
"msg": "",
"time": 1525813632659,
"v": 0,
"_timeStamp": "2018-05-08T21:07:12.659Z",
"myProp": "Some ValueWed May 09 2018 02:37:12 GMT+0530 (IST)"
}
#1
1
Checkout the Banyan API Documentation. All your use cases are covered.
检查Banyan API文档。您的所有用例都包含在内。
- Override the hostname or set a desired hostname
- 重写主机名或设置所需的主机名
Configurable while constructing the logger
在构造日志记录器时可配置。
- Change "level":30 to "level":"info"
- 改变“级别”:30到“水平”:“信息”
- Change the format of the time json object
- 更改time json对象的格式
- Add additional json object for example: "attr4": "value"
- 添加额外的json对象,例如:“attr4”:“value”
- Is there any way to change the default json object name such as time to timestamp
- 是否有办法将默认的json对象名称(如time)更改为timestamp
You can override the json object: See this: https://github.com/trentm/node-bunyan/issues/194
您可以覆盖json对象:查看以下内容:https://github.com/trentm/node-bunyan/issues/194。
var bunyan = require('bunyan');
function modifiedStream(filePath) {
return {
write: log => {
log.level = bunyan.nameFromLevel[log.level];
log.time = new Date().valueOf();
log._timeStamp = new Date().toISOString();
log.myProp = "Some Value" + new Date();
var logLine = JSON.stringify(log, bunyan.safeCycles(), 2);
console.log(logLine);
}
};
}
var logger = bunyan.createLogger({
name: 'myapp',
hostname: "My Test Mac",
streams: [{
type: 'raw',
stream: modifiedStream()
}]
});
logger.info("Hello");
logger.info({
customProp1: "AAA",
customProp2: "BBB"
});
Output:
输出:
{
"name": "myapp",
"hostname": "My Test Mac",
"pid": 89297,
"level": "info",
"msg": "Hello",
"time": 1525813632657,
"v": 0,
"_timeStamp": "2018-05-08T21:07:12.657Z",
"myProp": "Some ValueWed May 09 2018 02:37:12 GMT+0530 (IST)"
}
{
"name": "myapp",
"hostname": "My Test Mac",
"pid": 89297,
"level": "info",
"customProp1": "AAA",
"customProp2": "BBB",
"msg": "",
"time": 1525813632659,
"v": 0,
"_timeStamp": "2018-05-08T21:07:12.659Z",
"myProp": "Some ValueWed May 09 2018 02:37:12 GMT+0530 (IST)"
}