1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19: namespace Fluent\Logger;
20:
21: 22: 23: 24: 25:
26: abstract class BaseLogger implements \Fluent\Logger
27: {
28: protected $error_handler = null;
29:
30: 31: 32: 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: 41: 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: 54: 55: 56:
57: public function registerErrorHandler($callable)
58: {
59: if (is_string($callable)) {
60: $this->error_handler = array($callable);
61: } else if (is_array($callable)) {
62: switch(count($callable)) {
63: case 1:
64: if (!function_exists($callable[0])) {
65: throw new \Exception("could not register error handler: function does not exist");
66: }
67: break;
68: case 2:
69: if (!method_exists($callable[0],$callable[1])) {
70: throw new \Exception("could not register error handler: method does not exist");
71: }
72: break;
73: default:
74: throw new \Exception("could not register error handler: unexpected array count.");
75: }
76:
77: $this->error_handler = $callable;
78: } else if ($callable instanceof \Closure) {
79: $this->error_handler = $callable;
80: } else {
81: throw new \Exception("could not register error handler");
82: }
83: return true;
84: }
85: }
86: