1
|
|
|
<?php /** MicroDbDriver */ |
2
|
|
|
|
3
|
|
|
namespace Micro\Logger\Drivers; |
4
|
|
|
|
5
|
|
|
use Micro\Base\Exception; |
6
|
|
|
use Micro\Db\Adapter; |
7
|
|
|
use Micro\Db\Injector; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* DB logger class file . |
11
|
|
|
* |
12
|
|
|
* Writer logs in DB |
13
|
|
|
* |
14
|
|
|
* @author Oleg Lunegov <[email protected]> |
15
|
|
|
* @link https://github.com/linpax/microphp-framework |
16
|
|
|
* @copyright Copyright (c) 2013 Oleg Lunegov |
17
|
|
|
* @license https://github.com/linpax/microphp-framework/blob/master/LICENSE |
18
|
|
|
* @package Micro |
19
|
|
|
* @subpackage Logger\Driver |
20
|
|
|
* @version 1.0 |
21
|
|
|
* @since 1.0 |
22
|
|
|
*/ |
23
|
|
|
class DbDriver extends LoggerDriver |
24
|
|
|
{ |
25
|
|
|
/** @var string $tableName logger table name */ |
26
|
|
|
public $tableName; |
27
|
|
|
/** @var Adapter $db */ |
28
|
|
|
protected $db; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor prepare DB |
33
|
|
|
* |
34
|
|
|
* @access public |
35
|
|
|
* |
36
|
|
|
* @param array $params configuration params |
37
|
|
|
* |
38
|
|
|
* @result void |
39
|
|
|
* @throws Exception |
40
|
|
|
*/ |
41
|
|
|
public function __construct(array $params = []) |
42
|
|
|
{ |
43
|
|
|
parent::__construct($params); |
44
|
|
|
|
45
|
|
|
$this->tableName = !empty($params['table']) ? $params['table'] : 'logs'; |
46
|
|
|
$this->db = (new Injector)->getDriver(); |
|
|
|
|
47
|
|
|
|
48
|
|
|
if (!$this->db->tableExists($this->tableName)) { |
49
|
|
|
$this->db->createTable( |
50
|
|
|
$this->tableName, |
51
|
|
|
array( |
52
|
|
|
'`id` INT AUTO_INCREMENT', |
53
|
|
|
'`level` VARCHAR(20) NOT NULL', |
54
|
|
|
'`message` TEXT NOT NULL', |
55
|
|
|
'`date_create` INT NOT NULL', |
56
|
|
|
'PRIMARY KEY(id)' |
57
|
|
|
), |
58
|
|
|
'ENGINE=InnoDB DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci' |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Send log message into DB |
65
|
|
|
* |
66
|
|
|
* @access public |
67
|
|
|
* |
68
|
|
|
* @param integer $level level number |
69
|
|
|
* @param string $message message to write |
70
|
|
|
* |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
|
public function sendMessage($level, $message) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$this->db->insert($this->tableName, [ |
|
|
|
|
76
|
|
|
'level' => $level, |
77
|
|
|
'message' => $message, |
78
|
|
|
'date_create' => $_SERVER['REQUEST_TIME'] |
79
|
|
|
]); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..