我使用节点脚本调整了日志文件。具体来说,我对文件进行了一些字符串替换,将其解析为 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!`);
});
});