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