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