解析日志文件
Parsing a log file
我有一个日志文件 "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.
检索它们并将其存储在数据库中
没关系,如果您的 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
实际上 "Notification" 阻止字符串被读取为 JSON。因为有效 json 应该从大括号或方括号开始,
我们先删除 "Notification" 然后删除 trim 字符串两边的多余空格或双冒号。因此只剩下有效的JSON。
我有一个日志文件 "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.
检索它们并将其存储在数据库中没关系,如果您的 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
实际上 "Notification" 阻止字符串被读取为 JSON。因为有效 json 应该从大括号或方括号开始, 我们先删除 "Notification" 然后删除 trim 字符串两边的多余空格或双冒号。因此只剩下有效的JSON。