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