commit | author | age
|
90c639
|
1 |
<?php |
Z |
2 |
/** |
|
3 |
* link: http://www.zjhejiang.com/ |
|
4 |
* copyright: Copyright (c) 2018 浙江禾匠信息科技有限公司 |
|
5 |
* author: wxf |
|
6 |
*/ |
|
7 |
|
|
8 |
namespace app\core; |
|
9 |
|
|
10 |
|
|
11 |
use app\models\CoreExceptionLog; |
|
12 |
|
|
13 |
class ExceptionLog |
|
14 |
{ |
|
15 |
/** |
|
16 |
* 异常等级 |
|
17 |
*/ |
|
18 |
const LEVEL_ERROR = 1;// 错误 |
|
19 |
const LEVEL_WARNING = 2;// 警告 |
|
20 |
const LEVEL_INFO = 3;// 记录信息 |
|
21 |
|
|
22 |
public static function index($page) |
|
23 |
{ |
|
24 |
$query = CoreExceptionLog::find(); |
|
25 |
|
|
26 |
$count = $query->count(); |
|
27 |
$pagination = new Pagination(['totalCount' => $count, 'pageSize' => 20, 'page' => $page - 1]); |
|
28 |
|
|
29 |
$list = $query->asArray()->all(); |
|
30 |
|
|
31 |
return [ |
|
32 |
'list' => $list, |
|
33 |
'pagination' => $pagination |
|
34 |
]; |
|
35 |
} |
|
36 |
|
|
37 |
public function detail($id) |
|
38 |
{ |
|
39 |
$log = CoreExceptionLog::findOne($id); |
|
40 |
|
|
41 |
return $log; |
|
42 |
} |
|
43 |
|
|
44 |
/** |
|
45 |
* 创建异常日志 |
|
46 |
* @param $title |
|
47 |
* @param array $content |
|
48 |
* @param $level |
|
49 |
* @return bool |
|
50 |
*/ |
|
51 |
public function create($title, array $content, $level) |
|
52 |
{ |
|
53 |
try { |
|
54 |
$mallId = \Yii::$app->mall->id; |
|
55 |
} catch (\Exception $e) { |
|
56 |
$mallId = 0; |
|
57 |
} |
|
58 |
|
|
59 |
try { |
|
60 |
$log = new CoreExceptionLog(); |
|
61 |
$log->mall_id = $mallId; |
|
62 |
$log->level = $level; |
|
63 |
$log->title = $title; |
|
64 |
$log->content = \Yii::$app->serializer->encode($content); |
|
65 |
$res = $log->save(); |
|
66 |
\Yii::warning('异常日志记录是否存储成功:' . $res); |
|
67 |
return $res; |
|
68 |
|
|
69 |
} catch (\Exception $e) { |
|
70 |
\Yii::error($e->getMessage()); |
|
71 |
return false; |
|
72 |
} |
|
73 |
} |
|
74 |
|
|
75 |
public function error($title, array $content) |
|
76 |
{ |
|
77 |
return $this->create($title, $content, self::LEVEL_ERROR); |
|
78 |
} |
|
79 |
|
|
80 |
public function warning($title, array $content) |
|
81 |
{ |
|
82 |
return $this->create($title, $content, self::LEVEL_WARNING); |
|
83 |
} |
|
84 |
|
|
85 |
public function info($title, array $content) |
|
86 |
{ |
|
87 |
return $this->create($title, $content, self::LEVEL_INFO); |
|
88 |
} |
|
89 |
|
|
90 |
|
|
91 |
/** |
|
92 |
* 删除日志 |
|
93 |
* @param $id |
|
94 |
* @return mixed |
|
95 |
*/ |
|
96 |
public static function delete($id) |
|
97 |
{ |
|
98 |
$log = CoreExceptionLog::findOne($id); |
|
99 |
|
|
100 |
if ($log) { |
|
101 |
$log->is_delete = 1; |
|
102 |
|
|
103 |
return $log->save(); |
|
104 |
} |
|
105 |
|
|
106 |
return false; |
|
107 |
} |
|
108 |
} |