1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* MIT License |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 MZ Desenvolvimento de Sistemas LTDA |
6
|
|
|
* |
7
|
|
|
* @author Francimar Alves <[email protected]> |
8
|
|
|
* |
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
11
|
|
|
* in the Software without restriction, including without limitation the rights |
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
14
|
|
|
* furnished to do so, subject to the following conditions: |
15
|
|
|
* |
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
17
|
|
|
* copies or substantial portions of the Software. |
18
|
|
|
* |
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25
|
|
|
* SOFTWARE. |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
namespace NFe\Log; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Salva mensagens de erro, depuração entre outros |
32
|
|
|
*/ |
33
|
|
|
class Logger |
34
|
|
|
{ |
35
|
|
|
private $directory; |
36
|
|
|
private $write_function; |
37
|
|
|
private static $instance; |
38
|
|
|
|
39
|
2 |
|
public function __construct($logger = array()) |
40
|
|
|
{ |
41
|
2 |
|
$this->fromArray($logger); |
42
|
2 |
|
} |
43
|
|
|
|
44
|
9 |
|
public static function getInstance() |
45
|
|
|
{ |
46
|
9 |
|
if (is_null(self::$instance)) { |
47
|
1 |
|
self::$instance = new self(); |
48
|
1 |
|
} |
49
|
9 |
|
return self::$instance; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Pasta onde serão salvos os arquivos de Log |
54
|
|
|
*/ |
55
|
1 |
|
public function getDirectory() |
56
|
|
|
{ |
57
|
1 |
|
return $this->directory; |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
public function setDirectory($directory) |
61
|
|
|
{ |
62
|
3 |
|
$this->directory = $directory; |
63
|
3 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Altera a função que escreve os logs, informe null para restaurar o padrão |
68
|
|
|
* @param function $write_function nova função que será usada |
69
|
|
|
*/ |
70
|
9 |
|
public function setWriteFunction($write_function) |
71
|
|
|
{ |
72
|
9 |
|
$this->write_function = $write_function; |
73
|
9 |
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
public function toArray() |
77
|
|
|
{ |
78
|
1 |
|
$logger = array(); |
79
|
1 |
|
$logger['directory'] = $this->getDirectory(); |
80
|
1 |
|
return $logger; |
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
public function fromArray($logger = array()) |
84
|
|
|
{ |
85
|
3 |
|
if ($logger instanceof Logger) { |
86
|
1 |
|
$logger = $logger->toArray(); |
87
|
3 |
|
} elseif (!is_array($logger)) { |
88
|
1 |
|
return $this; |
89
|
|
|
} |
90
|
3 |
|
if (!isset($logger['directory'])) { |
91
|
2 |
|
$this->setDirectory(dirname(dirname(dirname(__DIR__))).'/docs/logs'); |
92
|
2 |
|
} else { |
93
|
1 |
|
$this->setDirectory($logger['directory']); |
94
|
|
|
} |
95
|
3 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function writeLog($type, $message) |
99
|
|
|
{ |
100
|
|
|
$filename = $this->getDirectory().'/'.date('Ymd').'.txt'; |
101
|
|
|
$fp = @fopen($filename, 'a'); |
|
|
|
|
102
|
|
|
if (!$fp) { |
103
|
|
|
throw new \Exception('Não foi possível abrir o arquivo de log "'.$filename.'"', 500); |
104
|
|
|
} |
105
|
|
|
fwrite($fp, date('d/m/Y H:i:s').' - '.$type.': '.$message."\n"); |
106
|
|
|
fclose($fp); |
107
|
|
|
chmod($filename, 0755); |
108
|
|
|
} |
109
|
|
|
|
110
|
2 |
|
private function write($type, $message) |
111
|
|
|
{ |
112
|
2 |
|
if (!is_null($this->write_function)) { |
113
|
2 |
|
call_user_func_array($this->write_function, func_get_args()); |
114
|
2 |
|
} else { |
115
|
|
|
$this->writeLog($type, $message); |
116
|
|
|
} |
117
|
2 |
|
} |
118
|
|
|
|
119
|
1 |
|
private function errorLog($message) |
120
|
|
|
{ |
121
|
1 |
|
$this->write('error', $message); |
122
|
1 |
|
} |
123
|
|
|
|
124
|
2 |
|
private function warningLog($message) |
125
|
|
|
{ |
126
|
2 |
|
$this->write('warning', $message); |
127
|
2 |
|
} |
128
|
|
|
|
129
|
1 |
|
private function debugLog($message) |
130
|
|
|
{ |
131
|
1 |
|
$this->write('debug', $message); |
132
|
1 |
|
} |
133
|
|
|
|
134
|
1 |
|
private function informationLog($message) |
135
|
|
|
{ |
136
|
1 |
|
$this->write('information', $message); |
137
|
1 |
|
} |
138
|
|
|
|
139
|
2 |
|
public function __call($name, $arguments) |
140
|
|
|
{ |
141
|
|
|
switch ($name) { |
142
|
2 |
|
case 'error': |
143
|
2 |
|
case 'warning': |
144
|
2 |
|
case 'debug': |
145
|
2 |
|
case 'information': |
146
|
1 |
|
return call_user_func_array(array($this, $name . 'Log'), $arguments); |
147
|
1 |
|
default: |
148
|
1 |
|
throw new \BadMethodCallException( |
149
|
1 |
|
sprintf('Call to undefined function: %s->%s().', get_class($this), $name), |
150
|
|
|
500 |
151
|
1 |
|
); |
152
|
1 |
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
3 |
|
public static function __callStatic($name, $arguments) |
156
|
|
|
{ |
157
|
|
|
switch ($name) { |
158
|
3 |
|
case 'error': |
159
|
3 |
|
case 'warning': |
160
|
3 |
|
case 'debug': |
161
|
3 |
|
case 'information': |
162
|
2 |
|
return call_user_func_array(array(self::getInstance(), $name . 'Log'), $arguments); |
163
|
1 |
|
default: |
164
|
1 |
|
throw new \BadMethodCallException( |
165
|
1 |
|
sprintf('Call to undefined function: %s::%s().', __CLASS__, $name), |
166
|
|
|
500 |
167
|
1 |
|
); |
168
|
1 |
|
} |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.