1: <?php
2: /**
3: * Fluent-Logger-PHP
4: *
5: * Copyright (C) 2011 - 2012 Fluent-Logger-PHP Contributors
6: *
7: * Licensed under the Apache License, Version 2.0 (the "License");
8: * you may not use this file except in compliance with the License.
9: * You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
13: * Unless required by applicable law or agreed to in writing, software
14: * distributed under the License is distributed on an "AS IS" BASIS,
15: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: * See the License for the specific language governing permissions and
17: * limitations under the License.
18: */
19: namespace Fluent\Logger;
20:
21: /**
22: * Fluent basic logger class.
23: *
24: * all fluenet logger must extend this class.
25: */
26: abstract class BaseLogger implements \Fluent\Logger\LoggerInterface
27: {
28: protected $error_handler = null;
29:
30: /**
31: * @param $entity
32: * @param void $error error message
33: */
34: public function defaultErrorHandler(BaseLogger $logger, Entity $entity, $error)
35: {
36: error_log(sprintf("%s %s %s: %s", get_class($logger), $error, $entity->getTag(), json_encode($entity->getData())));
37: }
38:
39: /**
40: * @param Entity $entity
41: * @param void $error error message
42: */
43: protected function processError(Entity $entity, $error)
44: {
45: if (!is_null($this->error_handler)) {
46: call_user_func_array($this->error_handler, array($this, $entity, $error));
47: } else {
48: $this->defaultErrorHandler($this, $entity, $error);
49: }
50: }
51:
52: /**
53: * @param callable $callable function name, array or closure
54: * @return bool
55: * @throws \InvalidArgumentException
56: */
57: public function registerErrorHandler($callable)
58: {
59: if (is_callable($callable)) {
60: $this->error_handler = $callable;
61: } else {
62: throw new \InvalidArgumentException("Error handler must be callable.");
63: }
64: return true;
65: }
66: }
67: