简体   繁体   English

如何在浏览器中显示bunyan日志?

[英]How to display bunyan logs in the browser?

I have a server implemented on node.js and I use Bunyan for logging. 我在node.js上实现了一个服务器,我使用Bunyan进行日志记录。 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> . 到目前为止,当我需要调试问题时,我会查看控制台中的日志,或者使用/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. 这变得远非理想,因为我必须vnc或ssh进入服务器,而当它是我正在查看的旧日志时,需要很长时间才能找到我在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. 这给了我的能力是http://server/logs ,我得到了所有日志文件的列表,我可以点击它们,它们会显示在浏览器上。 The problem is they're still just raw json, not a very friendly format to read. 问题是它们仍然只是原始的json,而不是一种非常友好的格式。

What I want is to print in the browser just the same way bunyan prints in cli, in a pretty way. 我想要的是在浏览器中打印,就像bunyan在cli中打印一样,以一种漂亮的方式。

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! 您可以使用frontail在提供的html页面中显示日志!

And you can use bunyan-format package to format bunyan logs output nicely! 并且您可以使用bunyan格式包来很好地格式化bunyan日志输出!

If you can in your application add some HTML, just load the bunyan logs with some ajax request. 如果您可以在您的应用程序中添加一些HTML,只需加载带有一些ajax请求的bunyan日志。 As this is just a JSON response you can format it however you wish. 由于这只是一个JSON响应,您可以根据需要对其进行格式化。 And modern browsers allow to format JSON to be more readable. 现代浏览器允许格式化JSON以使其更具可读性。

When transforming JSON object to string you can indent it with spaces: 将JSON对象转换为字符串时,可以使用空格缩进它:

JSON.stringify(bunyanLogsObject, null, 2);

Where: 哪里:

  • bunyanLogsObject - is your logs bunyanLogsObject - 是你的日志
  • null - replacer, read below null - 替换器,请阅读以下内容
  • 2 - number of spaces to indent with, providing this parameter actually turns formatting on 2 - 要缩进的空格数,提供此参数实际上会打开格式

As 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 . 更多信息可以在MDN找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM