Reputation: 51
I have a server implemented on node.js and I use Bunyan for logging. Up until now when I need to debug issues I look at the logs in the Console, or open old log files with /bunyan/bin/bunyan <log_file.log>
. This is becoming far from ideal because I have to either vnc or ssh into the server, and when it's older logs that I'm looking at it takes a long time to find what I'm looking for on bunyan cli.
I want to extend my logging functionality so that I can serve the log files so that I can view them on a browser. So far this is what I've implemented:
var express = require('express');
var app = express();
var bunyan = require('bunyan');
var serveIndex = require('serve-index');
var log = bunyan.createLogger({
name: 'server',
streams: [
{
level: 'info',
stream: process.stdout
},
{
type: 'rotating-file',
level: 'info',
path: __dirname+'/logs/server-logs.log',
period: '1m',
count: 12
}
]
});
app.use('/logs', serveIndex('logs', {'icons': true}))
app.get('/logs/*',function(req,res)
{
var file_name = __dirname + '/logs/' + JSON.stringify(req.params['0']);
file_name = file_name.replace(/"/g, '');
log.info(file_name);
var obj = fs.readFileSync(file_name, 'utf8');
res.format({
'application/json': function(){
res.send(obj);
},
'default': function(){
res.status(406).send('Not Acceptable');
}
});
});
/*Run the server*/
app.listen(3005,function()
{
log.info("Started.");
});
What this gives me is the ability to got to http://server/logs
and I get a listing of all log files, I can click them and they get displayed on the browser. The problem is they're still just raw json, not a very friendly format to read.
What I want is to print in the browser just the same way bunyan prints in cli, in a pretty way.
Any suggestions? I've searched for hours looking something that would do this but couldn't find anything.
Thanks!
Upvotes: 5
Views: 3626
Reputation: 1882
You can use frontail to show logs in a html page served!
And you can use bunyan-format package to format bunyan logs output nicely!
Upvotes: 3
Reputation: 9
If you can in your application add some HTML, just load the bunyan logs with some ajax request. As this is just a JSON response you can format it however you wish. And modern browsers allow to format JSON to be more readable.
When transforming JSON object to string you can indent it with spaces:
JSON.stringify(bunyanLogsObject, null, 2);
Where:
bunyanLogsObject
- is your logsnull
- replacer, read below2
- number of spaces to indent with, providing this parameter actually turns formatting onAs replacer you can provide a function to further format the input or an array of whitelist properties to display. More info can be found at MDN.
Upvotes: -1