|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* Author: Adrian Dumitru |
|
5
|
|
|
* Date: 6/3/2017 8:51 PM |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Qpdb\QueryBuilder\DB; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class DbLog |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
private static $instance; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $path; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var \DateTimeZone |
|
24
|
|
|
*/ |
|
25
|
|
|
private $dateTimeZone; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* DbLog constructor. |
|
30
|
|
|
*/ |
|
31
|
|
|
private function __construct() |
|
32
|
|
|
{ |
|
33
|
|
|
$this->dateTimeZone = new \DateTimeZone( 'Europe/Bucharest' ); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param $query |
|
39
|
|
|
* @param $duration |
|
40
|
|
|
*/ |
|
41
|
|
|
public function writeQueryDuration( $query, $duration ) |
|
42
|
|
|
{ |
|
43
|
|
|
$backtrace = end( debug_backtrace() ); |
|
44
|
|
|
$location = $backtrace[ 'file' ] . " Line: " . $backtrace[ 'line' ]; |
|
45
|
|
|
|
|
46
|
|
|
$this->path = DbConfig::getInstance()->getLogPathQueryDuration(); //my comments |
|
47
|
|
|
$message = "Duration: " . round( $duration, 5 ) . "\r\n"; |
|
48
|
|
|
$message .= "Location: $location\r\n"; |
|
49
|
|
|
$message .= "Query: $query"; |
|
50
|
|
|
$this->write( $message ); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $query |
|
55
|
|
|
* @param \PDOException $e |
|
56
|
|
|
*/ |
|
57
|
|
|
public function writeQueryErrors( $query, \PDOException $e ) |
|
58
|
|
|
{ |
|
59
|
|
|
$backtrace = end( $e->getTrace() ); |
|
60
|
|
|
$location = $backtrace[ 'file' ] . " Line: " . $backtrace[ 'line' ]; |
|
61
|
|
|
|
|
62
|
|
|
$this->path = DbConfig::getInstance()->getLogPathErrors(); //my comments |
|
63
|
|
|
$message = "Query: $query" . "\r\n"; |
|
64
|
|
|
$message .= "Location: $location\r\n"; |
|
65
|
|
|
$message .= "Error code : " . $e->getCode() . "\r\n"; |
|
66
|
|
|
$message .= "" . $e->getMessage(); |
|
67
|
|
|
|
|
68
|
|
|
$this->write( $message ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
public function write( $message ) |
|
73
|
|
|
{ |
|
74
|
|
|
|
|
75
|
|
|
$date = new \DateTime(); |
|
76
|
|
|
$date->setTimezone( $this->dateTimeZone ); |
|
77
|
|
|
|
|
78
|
|
|
$log = $this->path . $date->format( 'Y-m-d' ) . ".txt"; |
|
79
|
|
|
$time = $date->format( 'H:i:s' ); |
|
80
|
|
|
|
|
81
|
|
|
$messageFormat = "[$time]\r\n$message\r\n\r\n"; |
|
82
|
|
|
|
|
83
|
|
|
if ( is_dir( $this->path ) ) { |
|
84
|
|
|
if ( !file_exists( $log ) ) { |
|
85
|
|
|
$fh = fopen( $log, 'a+' ); |
|
86
|
|
|
fwrite( $fh, $messageFormat ); |
|
87
|
|
|
fclose( $fh ); |
|
88
|
|
|
} |
|
89
|
|
|
else { |
|
90
|
|
|
$this->edit( $log, $messageFormat ); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
else { |
|
94
|
|
|
if ( mkdir( $this->path, 0777 ) === true ) { |
|
95
|
|
|
$this->write( $message ); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param string $log |
|
103
|
|
|
* @param string $message |
|
104
|
|
|
*/ |
|
105
|
|
|
private function edit( $log, $message ) |
|
106
|
|
|
{ |
|
107
|
|
|
file_put_contents( $log, $message, FILE_APPEND ); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return DbLog |
|
113
|
|
|
*/ |
|
114
|
|
|
public static function getInstance() |
|
115
|
|
|
{ |
|
116
|
|
|
if ( null === self::$instance ) { |
|
117
|
|
|
self::$instance = new self(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return self::$instance; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
} |