developer.jelix.org is not used any more and exists only for
history. Post new tickets on the Github account.
developer.jelix.org n'est plus utilisée, et existe uniquement pour son historique. Postez les nouveaux tickets sur le compte github.
developer.jelix.org n'est plus utilisée, et existe uniquement pour son historique. Postez les nouveaux tickets sur le compte github.
Ticket #625: firePHP.3.diff
File firePHP.3.diff, 6.4 KB (added by hadrien, 13 years ago) |
---|
-
lib/jelix/utils/jLog.class.php
1 1 <?php 2 /* comments & extra-whitespaces have been removed by jBuildTools*/ 2 3 /** 3 4 * @package jelix 4 5 * @subpackage utils … … 8 9 * @link http://www.jelix.org 9 10 * @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html 10 11 */ 11 12 /** 12 /** 13 13 * utility class to log some message into a file into yourapp/var/log 14 14 * @package jelix 15 15 * @subpackage utils 16 16 * @static 17 17 */ 18 class jLog { 19 20 private function __construct(){} 21 22 /** 23 * log a dump of a php value (object or else) 24 * @param mixed $obj the value to dump 25 * @param string $label a label 26 * @param string $type the log type 27 */ 28 public static function dump($obj, $label='', $type='default'){ 29 if($label!=''){ 30 $message = $label.': '.var_export($obj,true); 31 }else{ 32 $message = var_export($obj,true); 33 } 34 self::log($message, $type); 35 } 36 37 /** 38 * log a message 39 * @param mixed $message 40 * @param string $type the log type 41 */ 42 public static function log($message, $type='default'){ 43 44 $f = $GLOBALS['gJConfig']->logfiles[$type]; 45 if ($f{0} == '!') { 46 $GLOBALS['gJCoord']->addLogMsg("log $type: $message", substr($f, 1)); 47 } 48 else { 49 if(!isset($_SERVER['REMOTE_ADDR'])){ // for CLI mode (bug #111) 50 $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; 51 } 52 $f = str_replace('%ip%', $_SERVER['REMOTE_ADDR'], $f); 53 $f = str_replace('%m%', date("m"), $f); 54 $f = str_replace('%Y%', date("Y"), $f); 55 $f = str_replace('%d%', date("d"), $f); 56 $f = str_replace('%H%', date("H"), $f); 57 $sel = new jSelectorLog($f); 58 59 error_log(date ("Y-m-d H:i:s")."\t".$_SERVER['REMOTE_ADDR']."\t$type\t$message\n", 3, $sel->getPath()); 60 } 61 } 18 class jLog 19 { 20 const LOG = 'LOG'; 21 const INFO = 'INFO'; 22 const WARN = 'WARN'; 23 const ERROR = 'ERROR'; 24 const DUMP = 'DUMP'; 25 const EXCEPTION = 'EXCEPTION'; 26 const TABLE = 'TABLE'; 27 28 private function __construct(){} 29 30 /** 31 * log a dump of a php value (object or else) 32 * @param mixed $obj the value to dump 33 * @param string $label a label 34 * @param string $type the log type 35 */ 36 public static function dump($obj, $label='', $type='default'){ 37 if($label!=''){ 38 $message = $label.': '.var_export($obj,true); 39 }else{ 40 $message = var_export($obj,true); 41 } 42 self::log($message, $type); 43 } 44 45 /** 46 * log a message 47 * @param mixed $message 48 * @param string $type the log type 49 */ 50 public static function log( 51 $message, 52 $type = 'default', 53 $var_dump = false) 54 { 55 if (self::detectClientExtension()) 56 { 57 return jLog::fireLog( 58 $message, 59 $type, 60 $var_dump 61 ); 62 } 63 64 if (!isset($GLOBALS['gJConfig']->logfiles[$type])) 65 { 66 $type = 'default'; 67 } 68 $f = $GLOBALS['gJConfig']->logfiles[$type]; 69 if($f{0} == '!'){ 70 $GLOBALS['gJCoord']->addLogMsg("log $type: $message", substr($f, 1)); 71 } 72 else{ 73 if(!isset($_SERVER['REMOTE_ADDR'])){ 74 $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; 75 } 76 $f = str_replace('%ip%', $_SERVER['REMOTE_ADDR'], $f); 77 $f = str_replace('%m%', date("m"), $f); 78 $f = str_replace('%Y%', date("Y"), $f); 79 $f = str_replace('%d%', date("d"), $f); 80 $f = str_replace('%H%', date("H"), $f); 81 $sel = new jSelectorLog($f); 82 error_log(date("Y-m-d H:i:s")."\t".$_SERVER['REMOTE_ADDR']."\t$type\t$message\n", 3, $sel->getPath()); 83 } 84 } 85 86 /** 87 * Log a message in Firebug with FirePHP addon 88 * 89 * @param {mixed} $obj Object/Message to log 90 * @param {string} $type Type of log 91 * @param {boolean} $var_dump True if you want to display a var_dump instead of a json 92 * @return void 93 * @author Hadrien Lanneau (hadrien at over-blog dot com) 94 **/ 95 public static function fireLog( 96 $obj = '', 97 $type = self::LOG, 98 $var_dump = false) 99 { 100 if ($obj instanceof Exception) 101 { 102 $Object = array( 103 'Class' => get_class($obj), 104 'Message' => $obj->getMessage(), 105 'File' => $obj->getFile(), 106 'Line' => $obj->getLine(), 107 'Trace' => $obj->getTrace() 108 ); 109 if ($type === null or 110 $type === self::EXCEPTION) 111 { 112 $type = 'TRACE'; 113 } 114 } 115 if ($type == 'default') 116 { 117 $type = self::LOG; 118 } 119 120 self::setHeader('X-FirePHP-Data-100000000001','{'); 121 if ($type == self::DUMP) 122 { 123 self::setHeader( 124 'X-FirePHP-Data-200000000001', 125 '"FirePHP.Dump":{' 126 ); 127 self::setHeader( 128 'X-FirePHP-Data-299999999999','"__SKIP__":"__SKIP__"},' 129 ); 130 } 131 else 132 { 133 self::setHeader( 134 'X-FirePHP-Data-300000000001', 135 '"FirePHP.Firebug.Console":[' 136 ); 137 self::setHeader( 138 'X-FirePHP-Data-399999999999', 139 '["__SKIP__"]],' 140 ); 141 } 142 143 self::setHeader( 144 'X-FirePHP-Data-999999999999', 145 '"__SKIP__":"__SKIP__"}' 146 ); 147 148 149 if ($var_dump) 150 { 151 ob_start(); 152 var_dump($obj); 153 $obj = ob_get_contents(); 154 ob_end_clean(); 155 } 156 $msg = '["' . $type . '",' . jJson::encode($obj) . '],'; 157 foreach (explode( 158 "\n", 159 chunk_split( 160 $msg, 161 5000, 162 "\n" 163 ) 164 ) as $part) 165 { 166 if ($part) 167 { 168 usleep(1); 169 /* Ensure microtime() increments with each loop. Not very elegant but it works */ 170 $mt = explode(' ', microtime()); 171 $mt = substr($mt[1],7).substr($mt[0],2); 172 self::setHeader( 173 'X-FirePHP-Data-' . 174 ($type == self::DUMP ? 175 '2' : 176 '3' 177 ) . $mt, 178 $part 179 ); 180 } 181 } 182 183 return true; 184 } 185 186 /** 187 * Check if FirePHP is installed on client 188 * 189 * @return void 190 * @author Christoph Dorn (http://www.firephp.org) 191 **/ 192 public static function detectClientExtension() 193 { 194 if (!preg_match_all( 195 '/\sFirePHP\/([\.|\d]*)\s?/si', 196 $_SERVER['HTTP_USER_AGENT'], 197 $m 198 ) or 199 !version_compare( 200 $m[1][0], 201 '0.0.6', 202 '>=' 203 )) 204 { 205 return false; 206 } 207 return true; 208 } 209 210 /** 211 * Set Header 212 * 213 * @return void 214 * @author Christoph Dorn (http://www.firephp.org) 215 **/ 216 public static function setHeader($Name, $Value) 217 { 218 return header($Name.': '.$Value); 219 } 62 220 } 63 ?>64 No newline at end of file