如何按字段读取日志文件?

时间:2017-09-14 08:09:44

标签: php

如何逐字段读取日志文件?我能够逐行读取日志文件。 如何逐字段读取日志文件?我能够逐行读取日志文件。 如何逐字段读取日志文件?我能够逐行读取日志文件。

logs.php:

{"timestamp":"2017-08-28 11:22:52:000000","level":"6","code":"1024","type":"Notice","description":"SQLSTATE[42S02]: Base table or view not found: 1146 Table 'pranalytics.dimstoreproducts' doesn't exist","file":"D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\OrdersModel.php","line":"72","context":null,"path":"D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\OrdersModel.php","message":"Notice (1024): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'pranalytics.dimstoreproducts' doesn't exist in [D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\OrdersModel.php, line 72]"}
{"timestamp":"2017-08-08 13:31:58:000000","level":"6","code":"8","type":"Notice","description":"Use of undefined constant pharmarackdb - assumed 'pharmarackdb'","file":"D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php","line":"30","context":null,"path":"D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php","message":"Notice (8): Use of undefined constant pharmarackdb - assumed 'pharmarackdb' in [D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php, line 30]"}
{"timestamp":"2017-08-08 13:33:54:000000","level":"6","code":"8","type":"Notice","description":"Use of undefined constant pharmarackdb - assumed 'pharmarackdb'","file":"D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php","line":"30","context":null,"path":"D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php","message":"Notice (8): Use of undefined constant pharmarackdb - assumed 'pharmarackdb' in [D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php, line 30]"}
{"timestamp":"2017-08-08 13:36:09:000000","level":"6","code":"8","type":"Notice","description":"Use of undefined constant pharmarackdb - assumed 'pharmarackdb'","file":"D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php","line":"31","context":null,"path":"D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php","message":"Notice (8): Use of undefined constant pharmarackdb - assumed 'pharmarackdb' in [D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php, line 31]"}{"timestamp":"2017-08-29 08:01:02:000000","level":"6","code":"1024","type":"Notice","description":"SQLSTATE[42S02]: Base table or view not found: 1146 Table 'pranalytics.dimstoreproducts' doesn't exist","file":"D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\OrdersModel.php","line":"72","context":null,"path":"D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\OrdersModel.php","message":"Notice (1024): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'pranalytics.dimstoreproducts' doesn't exist in [D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\OrdersModel.php, line 72]"}
{"timestamp":"2017-08-29 09:12:51:000000","level":"6","code":"1024","type":"Notice","description":"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''0','10'' at line 1","file":"D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php","line":"268","context":null,"path":"D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php","message":"Notice (1024): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''0','10'' at line 1 in [D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php, line 268]"}
{"timestamp":"2017-08-29 09:13:10:000000","level":"6","code":"1024","type":"Notice","description":"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''0','10'' at line 1","file":"D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php","line":"268","context":null,"path":"D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php","message":"Notice (1024): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''0','10'' at line 1 in [D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php, line 268]"}
{"timestamp":"2017-08-29 09:13:34:000000","level":"6","code":"1024","type":"Notice","description":"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''0','10'' at line 1","file":"D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php","line":"268","context":null,"path":"D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php","message":"Notice (1024): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''0','10'' at line 1 in [D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php, line 268]"}
{"timestamp":"2017-08-29 09:14:28:000000","level":"6","code":"1024","type":"Notice","description":"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''0','10'' at line 1","file":"D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php","line":"268","context":null,"path":"D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php","message":"Notice (1024): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''0','10'' at line 1 in [D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php, line 268]"}
{"timestamp":"2017-08-29 09:15:06:000000","level":"6","code":"1024","type":"Notice","description":"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('0','10')' at line 1","file":"D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php","line":"268","context":null,"path":"D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php","message":"Notice (1024): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('0','10')' at line 1 in [D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php, line 268]"}
{"timestamp":"2017-08-29 09:16:33:000000","level":"6","code":"1024","type":"Notice","description":"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('0','10')' at line 1","file":"D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php","line":"269","context":null,"path":"D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php","message":"Notice (1024): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('0','10')' at line 1 in [D:\\xampp\\htdocs\\PharmarackReports\\RESTAPI\\Model\\StoreproductsMappingModel.php, line 269]"}

我的PHP代码读取日志文件:

<table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
    <thead>
        <tr>
            <th>Sr.No.</th>
            <th>Error</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $cnt = 1;
        if ($file = fopen("RESTAPI/logs/app_errors.log", "r")) {
          while(!feof($file)) {
             $line = fgets($file);
             # do same stuff with the $line
        ?>
        <tr class="odd gradeX">
            <td>
                <?= $cnt ?>
            </td>
            <td>
                <?= $line ?>
            </td>
        </tr>
        <?php $cnt++; }
        fclose($file);
        }?>
    </tbody>
</table>

1 个答案:

答案 0 :(得分:1)

使用php explode

$arrayOfFields = explode(",", $line);

foreach($arrayOfFields as $field){
  echo $field . " ";
}

这应输出

&#34;时间戳&#34;:&#34; 2017-08-28 11:22:52:000000&#34;  &#34;电平&#34;:&#34; 6&#34; ...