Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 解析日志文件_Php_Mysql_Json_Logging_Text Parsing - Fatal编程技术网

Php 解析日志文件

Php 解析日志文件,php,mysql,json,logging,text-parsing,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",

我有一个日志文件“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]));
?>

我得到了我的输出

{{“正在进行”,“正在进行”,“2012-09-25”,“2012-09-25”,“XXXXXXXXXXXXXX”,“xxxxxxxxxx”,“XXXXXXXXXXXXXX”


它一直在运行。格式不正确。我想要它作为键值对。如何做?请大家需要帮助!我还需要检索它们并使用mysql将其存储在数据库中。

没关系,如果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);

?>

更新:

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

“Notification:”位后的字符串是有效的
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
试试这个:

$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。

它看起来像一个JSON文件。使用JSON parsertry with
JSON\u decode()
一次。我想它是JSON格式的。不。这是一个转码作业的日志文件当我尝试使用JSON decode时,我遇到了这个错误“致命错误:未捕获异常‘InvalidArgumentException’,消息为‘传递的变量不是数组或对象,而是使用空数组’”我不应该删除它们。这就是我获取文件的方式。我不应该每次都进去编辑。你不必删除它们,这段代码会自动删除它们。它跟踪第一次出现的双冒号,然后删除之前的任何“通知”在这种情况下。没有得到任何输出..但是得到了答案。谢谢。如果我使用file_get_contents($filename)包含该文件,则代码不起作用。我将使用代码更新答案,以正确解析日志文件$string不包含“通知”.String无法编辑,因为它来自一个不需要编辑的文件。我更新了答案以显示如何删除“通知”:部分并最终得到一个有效的
json
字符串。我只假设了一个多条目日志示例的外观,但代码应该足够好以供参考。谢谢。它工作了…-)。现在我如何在mysql中将通知的每个条目保存为一个单独的行。我应该在foreach循环内的mysql中使用insert命令吗?仍然没有t显示输出。谢谢,但我得到了答案:-)