Ticket #625: firePHP.2.diff

File firePHP.2.diff, 6.3 kB (added by hadrien, 7 months ago)

Ajout de fireLog

  • lib/jelix/utils/jLog.class.php

    old new  
    11<?php 
     2/* comments & extra-whitespaces have been removed by jBuildTools*/ 
    23/** 
    34* @package    jelix 
    45* @subpackage utils 
     
    89* @link       http://www.jelix.org 
    910* @licence    GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html 
    1011*/ 
    11  
    12 /** 
     12 /** 
    1313 * utility class to log some message into a file into yourapp/var/log 
    1414 * @package    jelix 
    1515 * @subpackage utils 
    1616 * @static 
    1717 */ 
    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     } 
     18class 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                 
     116                self::setHeader('X-FirePHP-Data-100000000001','{'); 
     117                if ($type == self::DUMP) 
     118                { 
     119                        self::setHeader( 
     120                                'X-FirePHP-Data-200000000001', 
     121                                '"FirePHP.Dump":{' 
     122                        ); 
     123                        self::setHeader( 
     124                                'X-FirePHP-Data-299999999999','"__SKIP__":"__SKIP__"},' 
     125                        ); 
     126                } 
     127                else 
     128                { 
     129                        self::setHeader( 
     130                                'X-FirePHP-Data-300000000001', 
     131                                '"FirePHP.Firebug.Console":[' 
     132                        ); 
     133                        self::setHeader( 
     134                                'X-FirePHP-Data-399999999999', 
     135                                '["__SKIP__"]],' 
     136                        ); 
     137                } 
     138                 
     139                self::setHeader( 
     140                        'X-FirePHP-Data-999999999999', 
     141                        '"__SKIP__":"__SKIP__"}' 
     142                ); 
     143                 
     144                 
     145                if ($var_dump) 
     146                { 
     147                        ob_start(); 
     148                        var_dump($obj); 
     149                        $obj = ob_get_contents(); 
     150                        ob_end_clean(); 
     151                } 
     152                $msg = '["' . $type . '",' . jJson::encode($obj) . '],'; 
     153                foreach (explode( 
     154                                        "\n", 
     155                                        chunk_split( 
     156                                                $msg, 
     157                                                5000, 
     158                                                "\n" 
     159                                        ) 
     160                                ) as $part) 
     161                { 
     162                        if ($part) 
     163                        { 
     164                                usleep(1); 
     165                                /* Ensure microtime() increments with each loop. Not very elegant but it works */ 
     166                                $mt = explode(' ', microtime()); 
     167                                $mt = substr($mt[1],7).substr($mt[0],2); 
     168                                self::setHeader( 
     169                                        'X-FirePHP-Data-' . 
     170                                        ($type == self::DUMP ? 
     171                                                '2' : 
     172                                                '3' 
     173                                        ) . $mt, 
     174                                        $part 
     175                                ); 
     176                        } 
     177                } 
     178                 
     179                return true; 
     180        } 
     181         
     182        /** 
     183         * Check if FirePHP is installed on client 
     184         * 
     185         * @return void 
     186         * @author Christoph Dorn (http://www.firephp.org) 
     187         **/ 
     188        public static function detectClientExtension() 
     189        { 
     190            if (!preg_match_all( 
     191                                '/\sFirePHP\/([\.|\d]*)\s?/si', 
     192                                $_SERVER['HTTP_USER_AGENT'], 
     193                                $m 
     194                        ) or 
     195                        !version_compare( 
     196                                $m[1][0], 
     197                                '0.0.6', 
     198                                '>=' 
     199                        )) 
     200                { 
     201                        return false; 
     202                } 
     203                return true; 
     204        } 
     205         
     206        /** 
     207         * Set Header 
     208         * 
     209         * @return void 
     210         * @author Christoph Dorn (http://www.firephp.org) 
     211         **/ 
     212        public static function setHeader($Name, $Value) 
     213        { 
     214                return header($Name.': '.$Value); 
     215        } 
    62216} 
    63 ?> 
Download in other formats: Original Format