Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.59% |
35 / 37 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
Db | |
94.59% |
35 / 37 |
|
66.67% |
2 / 3 |
8.01 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
writeLog | |
91.30% |
21 / 23 |
|
0.00% |
0 / 1 |
5.02 | |||
createTable | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Log\Writer; |
15 | |
16 | use Pop\Db\Adapter\AbstractAdapter; |
17 | |
18 | /** |
19 | * Db log writer class |
20 | * |
21 | * @category Pop |
22 | * @package Pop\Log |
23 | * @author Nick Sagona, III <dev@nolainteractive.com> |
24 | * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
25 | * @license http://www.popphp.org/license New BSD License |
26 | * @version 3.3.0 |
27 | */ |
28 | class Db extends AbstractWriter |
29 | { |
30 | |
31 | /** |
32 | * DB adapter |
33 | * @var AbstractAdapter |
34 | */ |
35 | protected $db = null; |
36 | |
37 | /** |
38 | * Table |
39 | * @var string |
40 | */ |
41 | protected $table = 'pop_log'; |
42 | |
43 | /** |
44 | * Constructor |
45 | * |
46 | * Instantiate the DB writer object |
47 | * |
48 | * The DB table requires the following fields at a minimum: |
49 | * timestamp DATETIME |
50 | * level INT |
51 | * name VARCHAR |
52 | * message TEXT, VARCHAR, etc. |
53 | * context TEXT, VARCHAR, etc. |
54 | * |
55 | * @param AbstractAdapter $db |
56 | * @param string $table |
57 | */ |
58 | public function __construct(AbstractAdapter $db, $table = 'pop_log') |
59 | { |
60 | $this->db = $db; |
61 | $this->table = $table; |
62 | |
63 | if (!$db->hasTable($this->table)) { |
64 | $this->createTable(); |
65 | } |
66 | } |
67 | |
68 | /** |
69 | * Write to the log |
70 | * |
71 | * @param mixed $level |
72 | * @param string $message |
73 | * @param array $context |
74 | * @return Db |
75 | */ |
76 | public function writeLog($level, $message, array $context = []) |
77 | { |
78 | if ($this->isWithinLogLimit($level)) { |
79 | $sql = $this->db->createSql(); |
80 | $fields = [ |
81 | 'timestamp' => $context['timestamp'], |
82 | 'level' => $level, |
83 | 'name' => $context['name'], |
84 | 'message' => $message, |
85 | 'context' => $this->getContext($context) |
86 | ]; |
87 | |
88 | $columns = []; |
89 | $params = []; |
90 | |
91 | $i = 1; |
92 | foreach ($fields as $column => $value) { |
93 | $placeholder = $sql->getPlaceholder(); |
94 | |
95 | if ($placeholder == ':') { |
96 | $placeholder .= $column; |
97 | } else if ($placeholder == '$') { |
98 | $placeholder .= $i; |
99 | } |
100 | $columns[$column] = $placeholder; |
101 | $params[$column] = $value; |
102 | $i++; |
103 | } |
104 | |
105 | $sql->insert($this->table)->values($columns); |
106 | |
107 | $this->db->prepare((string)$sql) |
108 | ->bindParams($params) |
109 | ->execute(); |
110 | } |
111 | |
112 | return $this; |
113 | } |
114 | |
115 | /** |
116 | * Create table in database |
117 | * |
118 | * @return void |
119 | */ |
120 | protected function createTable() |
121 | { |
122 | $schema = $this->db->createSchema(); |
123 | $schema->create($this->table) |
124 | ->int('id')->increment() |
125 | ->datetime('timestamp') |
126 | ->int('level', 1) |
127 | ->varchar('name', 255) |
128 | ->text('message') |
129 | ->text('context') |
130 | ->primary('id'); |
131 | |
132 | $this->db->query($schema); |
133 | } |
134 | |
135 | } |