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!
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With