我正在解决客户端安装BlackBoard插件时出现的问题。根据我们的请求,我们得到了一些json格式的日志文件。这些是非常难读的。
我尝试在IDE中打开,比如VS代码,它们对于应用程序来说太大了。我还下载了Apache,因为我在日志中注意到了对Tomcat的一些引用。查看器似乎能够解析这些文件,但是打开文件时希望我选择一种格式,而且我对如何确定日志文件格式还不太了解。
下面是日志文件中的一个示例行:
{"tags":["plugin
s"],"path":"/usr/local/blackboard/logs/plugins/bbgs-mbs/application.log","host":"ip-xx-xxx-xxx-xxx","message":"2019-10-07 05:02:16 | ERROR | 234:org.hibernate.util.JDBCExceptionReporter | ERROR: relation \"bbgs_mbs_alerts\" does not exist","type":"plugins","@version":"1","@timestamp":"2019-10-07T09:02:16.715Z","clientid":"xxxxxxxxxxxxx"}理想情况下,我所追求的只是一种简单的方法,可以加载我所拥有的4个文件,按日期排序,并试图找到与客户端报告的错误之间的关联。
帮帮我,你是我唯一的希望
发布于 2019-10-17 18:28:51
我用一个节点脚本调整了日志文件。具体来说,我对文件做了一些字符串替换,将其解析为JSON,并将其写回一个新文件。
我还使用jsonpath npm包将所需的属性写入txt文件。
这是一个完整的解决方案,如果其他人可能觉得它有帮助的话(我知道它很难看,但只是在完成了一次有用的事情之后:/)
// Make sure we got a filename on the command line.
if (process.argv.length < 3) {
console.log('Usage: node ' + process.argv[1] + ' FILENAME');
process.exit(1);
}
// Read the file and print its contents.
var fs = require('fs')
, jp = require('jsonpath')
, filename = process.argv[2]
, property = process.argv[3]
, debug_mode = process.argv[4]
, only_messages = process.argv[5];
fs.readFile(filename, 'utf8', (err, data) => {
if (err) throw err;
const _newNameArr = filename.split('/');
const _filename = _newNameArr[_newNameArr.length - 1];
const replaced = data.replace(/[\n\r]/g, '');
const replacedAgain = replaced.replace(/\}[\s]*\{/g, '},{');
const _destFileName = (only_messages == 'true')
? `messages_${_filename}`
: `${property || 'full'}_${_filename.replace('.txt', '.json')}`;
if (debug_mode == 'true') {
fs.writeFile('DEBUG-replaced_' + _destFileName, replacedAgain, (err) => {
if (err) throw err;
console.log('The file has been saved!');
});
}
const parsed = JSON.parse(replacedAgain);
const _fileContent = (only_messages == 'true')
? jp.query(parsed, '$..message').join('\n')
: JSON.stringify(parsed, (property ? [property] : null), 1);
fs.writeFile(_destFileName, _fileContent, (err) => {
if (err) throw err;
console.log(`The file, ${_destFileName}, has been saved!`);
});
});https://stackoverflow.com/questions/58421258
复制相似问题