|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Asymptix\tools\logging; |
|
4
|
|
|
|
|
5
|
|
|
use Asymptix\db\DBCore; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Simple Log class to store Logger messages into teh database. |
|
9
|
|
|
* |
|
10
|
|
|
* @category Asymptix PHP Framework |
|
11
|
|
|
* @author Dmytro Zarezenko <[email protected]> |
|
12
|
|
|
* @copyright (c) 2016, Dmytro Zarezenko |
|
13
|
|
|
* @license http://opensource.org/licenses/MIT |
|
14
|
|
|
*/ |
|
15
|
|
|
class LogDBObject extends \Asymptix\db\DBObject { |
|
16
|
|
|
|
|
17
|
|
|
const TABLE_NAME = "log"; |
|
18
|
|
|
const ID_FIELD_NAME = ""; |
|
19
|
|
|
protected $fieldsList = [ |
|
20
|
|
|
'type' => "", // varchar(10), default '' |
|
|
|
|
|
|
21
|
|
|
'message' => "", // text, not null, default '' |
|
|
|
|
|
|
22
|
|
|
'count' => 0, // int(6) unsigned, not null, default '' |
|
|
|
|
|
|
23
|
|
|
'last_seen' => "CURRENT_TIMESTAMP", // datetime, not null, default 'CURRENT_TIMESTAMP' |
|
|
|
|
|
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Log method, must be in all classes for Logger class functionality. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $type Log message type from Logger class. |
|
30
|
|
|
* @param string $message Message text. |
|
31
|
|
|
* @param int $time Timestamp. |
|
32
|
|
|
* |
|
33
|
|
|
* @return int Number of affected rows. |
|
34
|
|
|
* @throws \Asymptix\db\DBCoreException If some database error occurred. |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function log($type, $message, $time = null) { |
|
37
|
|
|
if (is_null($time)) { |
|
38
|
|
|
$time = time(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$query = "INSERT INTO " . self::TABLE_NAME . " (type, message, count, last_seen) VALUES (?, ?, 1, ?) |
|
42
|
|
|
ON DUPLICATE KEY UPDATE count = count + 1, last_seen = ?"; |
|
43
|
|
|
try { |
|
44
|
|
|
return DBCore::doUpdateQuery($query, "ssss", [ |
|
45
|
|
|
$type, $message, date("Y-m-d H:i:s", $time), date("Y-m-d H:i:s", $time) |
|
46
|
|
|
]); |
|
47
|
|
|
} catch (\Asymptix\db\DBCoreException $e) { |
|
48
|
|
|
print($e->getMessage()); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.