2015-10-12 130 views
1

我有一个日志文件“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将其存储在数据库中。

+0

它看起来像一个json文件。使用json解析器 – Jens

+0

试一下'json_decode()'。我认为它是以json格式。 –

+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" 
    } ] 
    } 

“通知:”位后的字符串有效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 
+0

如果我使用file_get_contents($ filename)包含文件,代码不起作用 –

+0

我将用代码更新答案以正确解析日志文件 –

+0

$ string不包含“通知”。字符串不能被编辑,因为它来自不需要编辑的文件。 –

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); 

?> 
+0

否输出正在显示。这是空白 –

+0

@伸手可及,现在它会适合你.. –

+0

仍然没有显示输出。谢谢你虽然得到了答案:-) –

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 

其实“通知”是防止被解读为JSON字符串。由于有效的json应该从花括号或方括号开始,我们首先删除“通知”,然后从字符串的两侧修剪多余的空格或双冒号。因此只剩下有效的JSON。

+0

我不应该删除它们。这就是我如何得到我的文件。我不应该每次都进去编辑。 –

+0

您不必删除它们,此代码将自动删除它们。它跟踪双冒号的第一个出现,然后删除在这种情况下是“通知”的任何东西。 –

+0

没有得到任何输出..虽然得到了答案。谢谢。 –