1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Jaeger |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2015-2016, mithra62 |
6
|
|
|
* @link http://jaeger-app.com |
7
|
|
|
* @version 1.0 |
8
|
|
|
* @filesource ./Traits/Log.php |
9
|
|
|
*/ |
10
|
|
|
namespace JaegerApp\Traits; |
11
|
|
|
|
12
|
|
|
use Monolog\Logger; |
13
|
|
|
use Monolog\Handler\StreamHandler; |
14
|
|
|
use Monolog\Formatter\LineFormatter; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Jaeger - Logging Trait |
18
|
|
|
* |
19
|
|
|
* Handles logging among all the other objects |
20
|
|
|
* |
21
|
|
|
* @package Logger |
22
|
|
|
* @author Eric Lamb <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
trait Log |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The PSR-3 Logging object we're using |
29
|
|
|
* |
30
|
|
|
* @var \Monolog\Logger |
31
|
|
|
*/ |
32
|
|
|
private $logger = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The path to where we're logging files |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $log_path = null; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Logs a generic error |
43
|
|
|
* |
44
|
|
|
* @param string $error |
45
|
|
|
* @return \Monolog\Boolean |
46
|
|
|
*/ |
47
|
|
|
public function logWarning($error) |
48
|
|
|
{ |
49
|
|
|
return $this->getLogger()->addWarning($error); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Logs a debug error |
54
|
|
|
* |
55
|
|
|
* @param string $error |
56
|
|
|
* @return \Monolog\Boolean |
57
|
|
|
*/ |
58
|
|
|
public function logDebug($error) |
59
|
|
|
{ |
60
|
|
|
return $this->getLogger()->addDebug($error); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Logs an error |
65
|
|
|
* |
66
|
|
|
* @param string $error |
67
|
|
|
* @return \Monolog\Boolean |
68
|
|
|
*/ |
69
|
|
|
public function logError($error) |
70
|
|
|
{ |
71
|
|
|
return $this->getLogger()->addError($error); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Logs an emergency instance |
76
|
|
|
* |
77
|
|
|
* @param unknown $error |
78
|
|
|
* @return \Monolog\Boolean |
79
|
|
|
*/ |
80
|
|
|
public function logEmergency($error) |
81
|
|
|
{ |
82
|
|
|
return $this->getLogger()->addEmergency($error); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns an instance of the logger (creating one if it doesn't exist yet) |
87
|
|
|
* |
88
|
|
|
* @param string $name |
89
|
|
|
* @return \Monolog\Logger |
90
|
|
|
*/ |
91
|
|
|
public function getLogger($name = 'm62') |
92
|
|
|
{ |
93
|
|
|
if (is_null($this->logger)) { |
94
|
|
|
$this->logger = new Logger($name); |
95
|
|
|
$dateFormat = "Y n j, g:i a"; |
96
|
|
|
$output = "%datetime% > %level_name% > %message% %context% %extra%\n"; |
97
|
|
|
$formatter = new LineFormatter($output, $dateFormat); |
98
|
|
|
|
99
|
|
|
$stream = new StreamHandler($this->getPathToLogFile($name), Logger::DEBUG); |
100
|
|
|
$stream->setFormatter($formatter); |
101
|
|
|
|
102
|
|
|
$this->logger->pushHandler($stream); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this->logger; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns the path to the log file |
110
|
|
|
* |
111
|
|
|
* @param string $name |
112
|
|
|
* The name of the log file to use |
113
|
|
|
* @return \mithra62\Traits::$log_path |
114
|
|
|
*/ |
115
|
|
|
public function getPathToLogFile($name = 'm62') |
116
|
|
|
{ |
117
|
|
|
if (is_null($this->log_path)) { |
118
|
|
|
$this->log_path = realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'logs') . DIRECTORY_SEPARATOR . $name . '.log'; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $this->log_path; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Sets the path to the log file to use |
126
|
|
|
* |
127
|
|
|
* @param string $path |
128
|
|
|
* The full path to the log file |
129
|
|
|
* @return \mithra62\Traits\Log |
130
|
|
|
*/ |
131
|
|
|
public function setPathToLogFile($path) |
132
|
|
|
{ |
133
|
|
|
$this->log_path = $path; |
134
|
|
|
$this->logger = null; |
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Removes the logging file |
140
|
|
|
* |
141
|
|
|
* @return \mithra62\Traits\Log |
142
|
|
|
*/ |
143
|
|
|
public function removeLogFile() |
144
|
|
|
{ |
145
|
|
|
if (file_exists($this->log_path)) { |
146
|
|
|
$this->logger = null; |
147
|
|
|
unlink($this->log_path); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
} |