将日志文件解析到JSON对象

发布于 2025-02-12 01:56:02 字数 619 浏览 1 评论 0原文

这是日志文件的内容

[2022-06-30T09:56:40.146Z] ### POST https://test.csdf/auth/send_otp  
    
 {
  "method": "POST",
  "headers": {
    "User-Agent": "testing",
    "Content-Type": "application/json"
  }
}
[2022-06-30T09:56:40.668Z] ### POST https://test.csdf/auth/login  
    
 {
  "method": "POST",
  "headers": {
    "User-Agent": "testing",
    "Content-Type": "application/json"
  }
}

我拥有名为request.log的日志文件,

[
  {
     "method": "POST",
     "url": "https://test.co/otp"
  },
  {
     "method": "POST",
     "url": "https://test.co/login"
  }
]

I have log file named request.log this is the content of the log file

[2022-06-30T09:56:40.146Z] ### POST https://test.csdf/auth/send_otp  
    
 {
  "method": "POST",
  "headers": {
    "User-Agent": "testing",
    "Content-Type": "application/json"
  }
}
[2022-06-30T09:56:40.668Z] ### POST https://test.csdf/auth/login  
    
 {
  "method": "POST",
  "headers": {
    "User-Agent": "testing",
    "Content-Type": "application/json"
  }
}

how can I parse the log file and transform it into array json object like this

[
  {
     "method": "POST",
     "url": "https://test.co/otp"
  },
  {
     "method": "POST",
     "url": "https://test.co/login"
  }
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终遇你 2025-02-19 01:56:02

您必须运行某种脚本才能将它们转换为JSON。在JavaScript/nodejs中,您可以做这样的事情:

const logFileContents = `[2022-06-30T09:56:40.146Z] ### POST https://test.csdf/auth/send_otp  
    
{
 "method": "POST",
 "headers": {
   "User-Agent": "testing",
   "Content-Type": "application/json"
 }
}
[2022-06-30T09:56:40.668Z] ### POST https://test.csdf/auth/login  
   
{
 "method": "POST",
 "headers": {
   "User-Agent": "testing",
   "Content-Type": "application/json"
 }
}`;

function convertToJSON(logs) {
  return logs
    .match(/\[.+\]\s\#+\s\w+\s.*\n/g) // fetch lines to parse
    .map((line) => line.split('###')[1].trim().split(' ')) //isolate method/url
    .map((log) => ({
      method: log[0],
      url: log[1],
    }));// convert array to object
}

console.log(JSON.stringify(convertToJSON(logFileContents), null, 4));

如果它对您的日志文件不起作用,只需修复正则态度以匹配您的案例

You have to run some kind of script to convert them to JSON. In javascript/nodejs you could do something like this:

const logFileContents = `[2022-06-30T09:56:40.146Z] ### POST https://test.csdf/auth/send_otp  
    
{
 "method": "POST",
 "headers": {
   "User-Agent": "testing",
   "Content-Type": "application/json"
 }
}
[2022-06-30T09:56:40.668Z] ### POST https://test.csdf/auth/login  
   
{
 "method": "POST",
 "headers": {
   "User-Agent": "testing",
   "Content-Type": "application/json"
 }
}`;

function convertToJSON(logs) {
  return logs
    .match(/\[.+\]\s\#+\s\w+\s.*\n/g) // fetch lines to parse
    .map((line) => line.split('###')[1].trim().split(' ')) //isolate method/url
    .map((log) => ({
      method: log[0],
      url: log[1],
    }));// convert array to object
}

console.log(JSON.stringify(convertToJSON(logFileContents), null, 4));

if it doesn't work for your log files, just fix the regex to match your case

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文