解析日志文件

时间:2015-10-12 11:12:12

标签: php mysql json logging text-parsing

我有一个日志文件“file.log”。我试图在php中解析文件以获取键值对。

Notification: {
"state" : "PROGRESSING",
"version" : "2012-09-25",
"jobId" : "xxxxxxxxx",
"pipelineId" : "xxxxxxxxxxx",
"input" : {
   "key" : "upload2/xxxxx",
   "frameRate" : "auto",
   "resolution" : "auto",
   "aspectRatio" : "auto",
   "interlaced" : "auto",
   "container" : "auto"
 },
"outputKeyPrefix" : "output2/test4/",
"outputs" : [ {
   "id" : "1",
   "presetId" : "xxxxxxxxxxxx",
   "key" : "xxxxxxxx",
   "rotate" : "auto",
   "status" : "Progressing"
    } ]
  }

我改变了价值观。我试图通过这个PHP代码解析它。

<?php
$myFile = "file.log";
$lines = file($myFile);
foreach ($lines as $no => $ln) {
$out = explode(":", $ln);
echo($out[1]);
echo(trim($out[1]));
?>

我的输出为

  

{{“PROGRESSING”,“PROGRESSING”,“2012-09-25”,“2012-09-25”,“xxxxxxxxxxxx”,“xxxxxxxxxx”,“xxxxxxxxxxxxxxx”,

它继续......不是正确的格式。我希望它作为关键值对。怎么做?请大家帮忙!我还需要检索它们并使用mysql将它存储在数据库中。

3 个答案:

答案 0 :(得分:2)

更新

$logFile = file_get_contents('logfile.log');

// first we replace all instances of the string "Notification: " with a comma to separate the json objects
$cleanLog = str_replace("Notification: ",",",$logFile);

// next we replace the first comma
$cleanLog = '[' . ltrim($cleanLog,",") . ']';

// construct the list of object
$objects = json_decode($cleanLog);

// use this main loop to iterate over all Notification rows
foreach ($objects as $object){
    // write a mysql insert statement here, 
    // you can address each object and inner members as follows:

    print $object->state . PHP_EOL;
    print $object->outputKeyPrefix . PHP_EOL;
    print $object->outputs[0]->id . PHP_EOL;
    print $object->input->key . PHP_EOL;
}

使用此日志文件示例作为参考:

logfile.log

Notification: {
"state" : "PROGRESSING",
"version" : "2012-09-25",
"jobId" : "xxxxxxxxx",
"pipelineId" : "xxxxxxxxxxx",
"input" : {
   "key" : "upload2/xxxxx",
   "frameRate" : "auto",
   "resolution" : "auto",
   "aspectRatio" : "auto",
   "interlaced" : "auto",
   "container" : "auto"
 },
"outputKeyPrefix" : "output2/test4/",
"outputs" : [ {
   "id" : "1",
   "presetId" : "xxxxxxxxxxxx",
   "key" : "xxxxxxxx",
   "rotate" : "auto",
   "status" : "Progressing"
    } ]
  }
Notification: {
"state" : "PROGRESSING",
"version" : "2012-09-25",
"jobId" : "xxxxxxxxx",
"pipelineId" : "xxxxxxxxxxx",
"input" : {
   "key" : "upload2/xxxxx",
   "frameRate" : "auto",
   "resolution" : "auto",
   "aspectRatio" : "auto",
   "interlaced" : "auto",
   "container" : "auto"
 },
"outputKeyPrefix" : "output2/test4/",
"outputs" : [ {
   "id" : "1",
   "presetId" : "xxxxxxxxxxxx",
   "key" : "xxxxxxxx",
   "rotate" : "auto",
   "status" : "Progressing"
    } ]
  }
Notification: {
"state" : "PROGRESSING",
"version" : "2012-09-25",
"jobId" : "xxxxxxxxx",
"pipelineId" : "xxxxxxxxxxx",
"input" : {
   "key" : "upload2/xxxxx",
   "frameRate" : "auto",
   "resolution" : "auto",
   "aspectRatio" : "auto",
   "interlaced" : "auto",
   "container" : "auto"
 },
"outputKeyPrefix" : "output2/test4/",
"outputs" : [ {
   "id" : "1",
   "presetId" : "xxxxxxxxxxxx",
   "key" : "xxxxxxxx",
   "rotate" : "auto",
   "status" : "Progressing"
    } ]
  }

&#34;通知后的字符串:&#34;位有效json。您可以按如下方式解析它:

<?php

$string = '{
"state" : "PROGRESSING",
"version" : "2012-09-25",
"jobId" : "xxxxxxxxx",
"pipelineId" : "xxxxxxxxxxx",
"input" : {
   "key" : "upload2/xxxxx",
   "frameRate" : "auto",
   "resolution" : "auto",
   "aspectRatio" : "auto",
   "interlaced" : "auto",
   "container" : "auto"
 },
"outputKeyPrefix" : "output2/test4/",
"outputs" : [ {
   "id" : "1",
   "presetId" : "xxxxxxxxxxxx",
   "key" : "xxxxxxxx",
   "rotate" : "auto",
   "status" : "Progressing"
    } ]
  }';

// construct object  
$object = json_decode($string);

// call each property of the object or inner object

print $object->state . PHP_EOL;
// PROGRESSING

print $object->outputKeyPrefix . PHP_EOL;  
// output2/test4/

print $object->outputs[0]->id . PHP_EOL;
// 1

// or, for multiple outputs

foreach ($object->outputs as $output)
    print $output->rotate . PHP_EOL;
// auto

答案 1 :(得分:0)

不是问题,如果您的json字符串不在json文件中..您可以将其解析为 -

<?php
$myFile = "file.log";
$json = file_get_contents($myFile);
$json= explode(":",$json,2);
$json=$json[1];
$obj=json_decode($json,true);
print_r($obj);

?>

答案 2 :(得分:0)

试试这个:

$myFile = "file.log";
$str = file_get_contents($myFile);
$json = trim(strstr($str, ':'), ': ');
print_r(json_decode($json));// for Object
print_r(json_decode($json, true));// for Array

实际上&#34;通知&#34;阻止字符串被读取为JSON。有效的json应该从花括号或方括号开始, 我们删除&#34;通知&#34;首先,然后从字符串的两边修剪多余的空格或双冒号。因此只剩下有效的JSON。