1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* YOURLS simple logger |
5
|
|
|
* |
6
|
|
|
* @since 1.7.3 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace YOURLS\Admin; |
10
|
|
|
|
11
|
|
|
use YOURLS\Database\YDB; |
12
|
|
|
|
13
|
|
|
class Logger extends \Aura\Sql\Profiler { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* the YDB instance |
17
|
|
|
* @var YDB |
18
|
|
|
*/ |
19
|
|
|
protected $ydb; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Debug log where messages are stored |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $debug_log = array(); |
26
|
|
|
|
27
|
|
|
public function __construct(YDB $ydb) { |
28
|
|
|
$this->ydb = $ydb; |
29
|
|
|
/** |
|
|
|
|
30
|
|
|
* @todo Extend this to allow calling an external logger |
31
|
|
|
*/ |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $message |
36
|
|
|
*/ |
37
|
40 |
|
public function log($message) { |
38
|
40 |
|
$this->debug_log[] = $message; |
39
|
40 |
|
} |
40
|
|
|
|
41
|
1 |
|
public function get_log() { |
42
|
1 |
|
return $this->debug_log; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Extends \Aura\Sql\Profiler::addProfile() to log queries in our YOURLS logger |
47
|
|
|
* |
48
|
|
|
* @since 1.7.3 |
49
|
|
|
* @param float $duration The query duration. |
50
|
|
|
* @param string $function The PDO method that made the entry. |
51
|
|
|
* @param string $statement The SQL query statement. |
52
|
|
|
* @param array $bind_values The values bound to the statement. |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
36 |
|
public function addProfile($duration, $function, $statement, array $bind_values = array() ) { |
56
|
36 |
|
parent::addProfile($duration, $function, $statement, $bind_values); |
57
|
|
|
|
58
|
36 |
|
if($function == 'connect') { |
|
|
|
|
59
|
|
|
$this->log( sprintf('SQL: CONNECT (%s s)', number_format($duration, 5)) ); |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// If we are emulating prepare, don't log 'prepare' statement, as they are not actual queries sent to the server |
|
|
|
|
64
|
|
|
// See YDB::get_queries() for details |
65
|
36 |
|
if ($this->ydb->get_emulate_state() && $function !== 'prepare') { |
66
|
36 |
|
$this->log( sprintf('SQL: %s (%s s)', $this->pretty_format($statement, $bind_values), number_format($duration, 5) ) ); |
|
|
|
|
67
|
|
|
} |
68
|
36 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Format PDO statement with bind/values replacement |
72
|
|
|
* |
73
|
|
|
* This replaces PDO binds such as 'key_name = :name' with corresponding array values, eg array('name'=>'some value') |
|
|
|
|
74
|
|
|
* This is merely a cosmetic replacement to allow for readability: the result WILL NOT be valid SQL! (eg no proper quotes) |
|
|
|
|
75
|
|
|
* |
76
|
|
|
* @since 1.7.3 |
77
|
|
|
* @param string $statement SQL query with PDO style named placeholders |
78
|
|
|
* @param array $values Optional array of values corresponding to placeholders |
79
|
|
|
* @return string Readable SQL query with placeholders replaced |
80
|
|
|
*/ |
81
|
36 |
|
public function pretty_format($statement, array $values = array() ) { |
82
|
36 |
|
if (!$values) { |
|
|
|
|
83
|
3 |
|
return $statement; |
84
|
|
|
} |
85
|
|
|
|
86
|
34 |
|
return preg_replace_callback( |
87
|
34 |
|
'/:([^\s;)]*)/', |
88
|
|
|
|
89
|
|
|
/** |
|
|
|
|
90
|
|
|
* @param string $matches |
91
|
|
|
*/ |
92
|
|
|
function ($matches) use ($values) { |
93
|
34 |
|
$replacement = isset( $values[$matches[1]] ) ? $values[$matches[1]] : ''; |
94
|
34 |
|
if(is_array($replacement)) { |
95
|
|
|
$replacement = implode(",", $replacement); |
|
|
|
|
96
|
|
|
} |
97
|
34 |
|
return "'$replacement'"; |
|
|
|
|
98
|
34 |
|
}, |
99
|
|
|
$statement |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|