1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20: 21:
22: class FireLogger extends Object
23: {
24: const DEBUG = 'debug',
25: INFO = 'info',
26: WARNING = 'warning',
27: ERROR = 'error',
28: CRITICAL = 'critical';
29:
30: private static $payload = array('logs' => array());
31:
32:
33: 34: 35: 36: 37:
38: public static function log($message, $priority = self::DEBUG)
39: {
40: if (!isset($_SERVER['HTTP_X_FIRELOGGER']) || headers_sent()) {
41: return FALSE;
42: }
43:
44: $item = array(
45: 'name' => 'PHP',
46: 'level' => $priority,
47: 'order' => count(self::$payload['logs']),
48: 'time' => str_pad(number_format((microtime(TRUE) - Debugger::$time) * 1000, 1, '.', ' '), 8, '0', STR_PAD_LEFT) . ' ms',
49: 'template' => '',
50: 'message' => '',
51: 'style' => 'background:#767ab6',
52: );
53:
54: $args = func_get_args();
55: if (isset($args[0]) && is_string($args[0])) {
56: $item['template'] = array_shift($args);
57: }
58:
59: if (isset($args[0]) && $args[0] instanceof Exception) {
60: $e = array_shift($args);
61: $trace = $e->getTrace();
62: if (isset($trace[0]['class']) && $trace[0]['class'] === 'Debugger'
63: && ($trace[0]['function'] === '_shutdownHandler' || $trace[0]['function'] === '_errorHandler')
64: ) {
65: unset($trace[0]);
66: }
67:
68: $file = str_replace(dirname(dirname(dirname($e->getFile()))), "\xE2\x80\xA6", $e->getFile());
69: $item['template'] = ($e instanceof ErrorException ? '' : get_class($e) . ': ')
70: . $e->getMessage() . ($e->getCode() ? ' #' . $e->getCode() : '') . ' in ' . $file . ':' . $e->getLine();
71: $item['pathname'] = $e->getFile();
72: $item['lineno'] = $e->getLine();
73:
74: } else {
75: $trace = debug_backtrace();
76: if (isset($trace[1]['class']) && $trace[1]['class'] === 'Debugger'
77: && ($trace[1]['function'] === 'fireLog')
78: ) {
79: unset($trace[0]);
80: }
81:
82: foreach ($trace as $frame) {
83: if (isset($frame['file']) && is_file($frame['file'])) {
84: $item['pathname'] = $frame['file'];
85: $item['lineno'] = $frame['line'];
86: break;
87: }
88: }
89: }
90:
91: $item['exc_info'] = array('', '', array());
92: $item['exc_frames'] = array();
93:
94: foreach ($trace as $frame) {
95: $frame += array('file' => NULL, 'line' => NULL, 'class' => NULL, 'type' => NULL, 'function' => NULL, 'object' => NULL, 'args' => NULL);
96: $item['exc_info'][2][] = array($frame['file'], $frame['line'], "$frame[class]$frame[type]$frame[function]", $frame['object']);
97: $item['exc_frames'][] = $frame['args'];
98: }
99:
100: if (isset($args[0]) && in_array($args[0], array(self::DEBUG, self::INFO, self::WARNING, self::ERROR, self::CRITICAL), TRUE)) {
101: $item['level'] = array_shift($args);
102: }
103:
104: $item['args'] = $args;
105:
106: self::$payload['logs'][] = self::jsonDump($item, -1);
107: foreach (str_split(base64_encode(@json_encode(self::$payload)), 4990) as $k => $v) {
108: header("FireLogger-de11e-$k:$v");
109: }
110: return TRUE;
111: }
112:
113:
114: 115: 116: 117: 118: 119:
120: private static function jsonDump(& $var, $level = 0)
121: {
122: if (is_bool($var) || is_null($var) || is_int($var) || is_float($var)) {
123: return $var;
124:
125: } elseif (is_string($var)) {
126: if (Debugger::$maxLen && strlen($var) > Debugger::$maxLen) {
127: $var = substr($var, 0, Debugger::$maxLen) . " \xE2\x80\xA6 ";
128: }
129: return Strings::fixEncoding($var);
130:
131: } elseif (is_array($var)) {
132: static $marker;
133: if ($marker === NULL) {
134: $marker = uniqid("\x00", TRUE);
135: }
136: if (isset($var[$marker])) {
137: return "\xE2\x80\xA6RECURSION\xE2\x80\xA6";
138:
139: } elseif ($level < Debugger::$maxDepth || !Debugger::$maxDepth) {
140: $var[$marker] = TRUE;
141: $res = array();
142: foreach ($var as $k => & $v) {
143: if ($k !== $marker) {
144: $res[self::jsonDump($k)] = self::jsonDump($v, $level + 1);
145: }
146: }
147: unset($var[$marker]);
148: return $res;
149:
150: } else {
151: return " \xE2\x80\xA6 ";
152: }
153:
154: } elseif (is_object($var)) {
155: $arr = (array) $var;
156: static $list = array();
157: if (in_array($var, $list, TRUE)) {
158: return "\xE2\x80\xA6RECURSION\xE2\x80\xA6";
159:
160: } elseif ($level < Debugger::$maxDepth || !Debugger::$maxDepth) {
161: $list[] = $var;
162: $res = array("\x00" => '(object) ' . get_class($var));
163: foreach ($arr as $k => & $v) {
164: if ($k[0] === "\x00") {
165: $k = substr($k, strrpos($k, "\x00") + 1);
166: }
167: $res[self::jsonDump($k)] = self::jsonDump($v, $level + 1);
168: }
169: array_pop($list);
170: return $res;
171:
172: } else {
173: return " \xE2\x80\xA6 ";
174: }
175:
176: } elseif (is_resource($var)) {
177: return "resource " . get_resource_type($var);
178:
179: } else {
180: return "unknown type";
181: }
182: }
183:
184: }
185: