1: <?php
2: 3: 4: 5: 6: 7: 8:
9:
10: namespace OpenCloud\Common\Log;
11:
12: use OpenCloud\Common\Exceptions\LoggingException;
13:
14: 15: 16: 17: 18:
19: class Logger extends AbstractLogger
20: {
21: 22: 23: 24: 25:
26: private $enabled;
27:
28: 29: 30: 31: 32: 33:
34: private $urgentLevels = array(
35: LogLevel::EMERGENCY,
36: LogLevel::ALERT,
37: LogLevel::CRITICAL
38: );
39:
40: 41: 42: 43: 44:
45: private $options = array(
46: 'outputToFile' => false,
47: 'logFile' => null,
48: 'dateFormat' => 'd/m/y H:I',
49: 'delimeter' => ' - '
50: );
51:
52: public function __construct($enabled = false)
53: {
54: $this->enabled = $enabled;
55: }
56:
57: public static function newInstance()
58: {
59: return new static();
60: }
61:
62: 63: 64: 65: 66: 67:
68: private function outputIsUrgent($logLevel)
69: {
70: return in_array($logLevel, $this->urgentLevels);
71: }
72:
73: 74: 75: 76: 77: 78: 79:
80: private function interpolate($message, array $context = array())
81: {
82:
83: $replace = array();
84: foreach ($context as $key => $val) {
85: $replace['{' . $key . '}'] = $val;
86: }
87:
88:
89: return strtr($message, $replace);
90: }
91:
92: 93: 94: 95: 96: 97:
98: public function setEnabled($enabled)
99: {
100: $this->enabled = $enabled;
101: return $this;
102: }
103:
104: 105: 106: 107: 108:
109: public function isEnabled()
110: {
111: return $this->enabled === true;
112: }
113:
114: 115: 116: 117: 118:
119: public function setOptions(array $options = array())
120: {
121: foreach ($options as $key => $value) {
122: $this->setOption($key, $value);
123: }
124: return $this;
125: }
126:
127: 128: 129: 130: 131:
132: public function getOptions()
133: {
134: return $this->options;
135: }
136:
137: 138: 139: 140: 141: 142:
143: public function setOption($key, $value)
144: {
145: if ($this->optionExists($key)) {
146: $this->options[$key] = $value;
147: return $this;
148: }
149: }
150:
151: 152: 153: 154: 155: 156:
157: public function getOption($key)
158: {
159: if ($this->optionExists($key)) {
160: return $this->options[$key];
161: }
162: }
163:
164: 165: 166: 167: 168: 169:
170: private function optionExists($key)
171: {
172: return array_key_exists($key, $this->getOptions());
173: }
174:
175: 176: 177: 178: 179: 180: 181:
182: public function log($level, $message, array $context = array())
183: {
184: if ($this->outputIsUrgent($level) || $this->isEnabled()) {
185: $this->dispatch($message, $context);
186: }
187: }
188:
189: 190: 191: 192: 193: 194:
195: private function formatFileLine($string)
196: {
197: $format = $this->getOption('dateFormat') . $this->getOption('delimeter');
198: return date($format) . $string;
199: }
200:
201: 202: 203: 204: 205: 206: 207:
208: private function dispatch($message, $context)
209: {
210: $output = $this->interpolate($message, $context) . PHP_EOL;
211:
212: if ($this->getOption('outputToFile') === true) {
213: $file = $this->getOption('logFile');
214:
215: if (!is_writable($file)) {
216: throw new LoggingException(
217: 'The log file either does not exist or is not writeable'
218: );
219: }
220:
221:
222: file_put_contents($file, $this->formatFileLine($output), FILE_APPEND);
223: } else {
224:
225: echo $output;
226: }
227: }
228:
229: public function deprecated($method, $new)
230: {
231: $string = sprintf('The %s method is deprecated, please use %s instead', $method, $new);
232: return $this->warning($string);
233: }
234:
235: }