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