Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display bunyan logs in the browser?

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!

like image 846
Dani C. Avatar asked Oct 30 '22 09:10

Dani C.


1 Answers

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!

like image 166
Hemã Vidal Avatar answered Nov 14 '22 07:11

Hemã Vidal