浏览代码

txt文件上传^C志优化

曹鹤洋 2 年之前
父节点
当前提交
df6334a95e
共有 2 个文件被更改,包括 20 次插入11 次删除
  1. 9 0
      application/api/config.php
  2. 11 11
      application/api/controller/Index.php

+ 9 - 0
application/api/config.php

@@ -3,4 +3,13 @@
 //配置文件
 return [
     'exception_handle'        => '\\app\\api\\library\\ExceptionHandle',
+
+    'log'                    => [
+        // 日志记录方式,支持 file socket trace sae
+        'type' => 'file',
+        // 日志保存目录
+        'path' => LOG_PATH,
+        // 日志记录级别
+        'level' => ['log'],
+    ]
 ];

+ 11 - 11
application/api/controller/Index.php

@@ -30,7 +30,6 @@ class Index extends Api
     public function vocs()
     {
         $req=$this->request->param();
-        Log::record($req);
 
         $time = date('Y/m/d H:i:s');
         $rows = $req;
@@ -55,9 +54,10 @@ class Index extends Api
                     //单个日志文件的大小限制,超过后会自动记录到第二个文件
                     'file_size'     =>2097152,
                     //日志的时间格式,默认是` c `
-                    'time_format'   =>'c'
+                    'time_format'   =>'c',
+                    'level' => ['log'],
                 ]);
-                Log::record($req);
+                Log::record(['失败'=>'样品编号为'.$res['data_txt_name'].'数据插入失败','原因'=>'该文件编号不以ABCD开头']);
                 return $this->success('数据文件不以A、B、C、D开头,已记录日志',$req,0);
             }
             foreach($rows as $k=>$v){
@@ -343,11 +343,12 @@ class Index extends Api
 
                 //提交事务
                 db()->commit();
+                Log::record(['成功'=>'样品编号为'.$res['data_txt_name'].'数据插入成功']);
                 return json_encode(['code'=>'0','msg'=>'成功','time'=>$time,'data'=>[$res,$row]]);
             } catch (\Exception $e){
                 //失败回滚
                 db()->rollback();
-                Log::record($e->getMessage());
+                Log::record(['失败'=>'样品编号为'.$res['data_txt_name'].'数据插入失败','原因'=>$e->getMessage()]);
                 return $e->getMessage();
             }
 
@@ -548,12 +549,12 @@ class Index extends Api
 
                 //提交事务
                 db()->commit();
-
+                Log::record(['成功'=>'样品编号为'.$res['data_txt_name'].'数据插入成功']);
                 return json_encode(['code'=>'0','msg'=>'成功','time'=>$time,'data'=>[$res,$row]]);
             } catch (\Exception $e){
                 //失败回滚
                 db()->rollback();
-                Log::record($e->getMessage());
+                Log::record(['失败'=>'样品编号为'.$res['data_txt_name'].'数据插入失败','原因'=>$e->getMessage()]);
                 return $e->getMessage();
             }
 
@@ -567,7 +568,6 @@ class Index extends Api
     public function updateVocs()
     {
         $req=$this->request->param();
-        Log::record($req);
 
         $time = date('Y/m/d H:i:s');
         $rows = $req;
@@ -863,14 +863,13 @@ class Index extends Api
 
                         //提交事务
                         db()->commit();
+                        Log::record(['成功'=>'样品编号为'.$res['data_txt_name'].'数据插入成功']);
                         return json_encode(['code'=>'0','msg'=>'修改成功','time'=>$time,'data'=>[$res,$row]]);
                     }
-
-
                 } catch (\Exception $e){
                     //失败回滚
                     db()->rollback();
-                    Log::record($e->getMessage());
+                    Log::record(['失败'=>'样品编号为'.$res['data_txt_name'].'数据插入失败','原因'=>$e->getMessage()]);
                     return $e->getMessage();
                 }
             }else{
@@ -1072,12 +1071,13 @@ class Index extends Api
 
                         //提交事务
                         db()->commit();
+                        Log::record(['成功'=>'样品编号为'.$res['data_txt_name'].'数据插入成功']);
                         return json_encode(['code'=>'0','msg'=>'修改成功','time'=>$time,'data'=>[$res,$row]]);
                     }
                 } catch (\Exception $e){
                     //失败回滚
                     db()->rollback();
-                    Log::record($e->getMessage());
+                    Log::record(['失败'=>'样品编号为'.$res['data_txt_name'].'数据插入失败','原因'=>$e->getMessage()]);
                     return $e->getMessage();
                 }
             }else{