├── composer.json ├── debug.php ├── demo ├── demo.php ├── php-cli-debug.png ├── php-debug-object-expand.png ├── php-debug-object.png ├── php-debug-pi.png ├── php-debug-sublime-protocol.png ├── php-debug.png ├── php-log-variable.png ├── php-visually-debug-array-full.png ├── php-visually-debug-array.png └── test.log ├── pull.bat ├── push.bat └── readme.md /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hazardland/debug", 3 | "type": "library", 4 | "description": "php variable debug", 5 | "keywords": ["debug"], 6 | "homepage": "http://github.com/hazardland/debug.php", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "hazardland", 11 | "email": "hazardland@gmail.com", 12 | "homepage": "http://hazardland.com", 13 | "role": "Developer" 14 | } 15 | ], 16 | "require": 17 | { 18 | "php": ">=5.4.21" 19 | }, 20 | "autoload": { 21 | "files": ["debug.php"] 22 | } 23 | } -------------------------------------------------------------------------------- /debug.php: -------------------------------------------------------------------------------- 1 | \n"; 125 | $charset = true; 126 | } 127 | if ($level>$limit) 128 | { 129 | if ($plain) 130 | { 131 | return str_repeat(' ', 4*$level)."...\n"; 132 | } 133 | else 134 | { 135 | return "..."; 136 | } 137 | } 138 | if (is_object($object) || (is_array($object) && count($object)>0)) 139 | { 140 | foreach ($object as $key => $value) 141 | { 142 | if ($plain) 143 | { 144 | $result .= str_repeat(' ', 4*$level).$key; 145 | } 146 | else 147 | { 148 | if (is_array($object)) 149 | { 150 | $result .= "".$key.''; 151 | } 152 | else 153 | { 154 | $result .= ''.$key.''; 155 | } 156 | } 157 | if (is_object($value) || (is_array($value) && count($value)>0)) 158 | { 159 | if (is_object($value)) 160 | { 161 | if ($plain) 162 | { 163 | $result .= " (".get_class($value).")"; 164 | } 165 | else 166 | { 167 | $result .= " ".get_class($value).""; 168 | } 169 | } 170 | else 171 | { 172 | if ($plain) 173 | { 174 | $result .= " (array)"; 175 | } 176 | else 177 | { 178 | $result .= " array"; 179 | } 180 | } 181 | if ($plain) 182 | { 183 | $result .= "\n"; 184 | } 185 | else 186 | { 187 | $collapse = !($level+1<=1 || $title===true || (is_string($title) && substr($title,0,1)=='*')); 188 | $result .= "\n
\n
".($collapse?'+':'-')."
"; 189 | } 190 | $result .= debug ($value, $title, $plain, $limit, $level+1); 191 | if ($plain) 192 | { 193 | $result .= ""; 194 | } 195 | else 196 | { 197 | $result .= "
\n"; 198 | } 199 | 200 | } 201 | else 202 | { 203 | $result .= " : "; 204 | if ($value===null) 205 | { 206 | if ($plain) 207 | { 208 | $result .= $color_cyan."null".$color_clear; 209 | } 210 | else 211 | { 212 | $result .= "null"; 213 | } 214 | } 215 | else if (is_bool($value)) 216 | { 217 | if ($value) 218 | { 219 | if ($plain) 220 | { 221 | $result .= $color_purple_light."true".$color_clear; 222 | } 223 | else 224 | { 225 | 226 | $result .= "true"; 227 | } 228 | } 229 | else 230 | { 231 | if ($plain) 232 | { 233 | $result .= $color_purple_light."false".$color_clear; 234 | } 235 | else 236 | { 237 | $result .= "false"; 238 | } 239 | } 240 | } 241 | else if (is_integer($value) || is_float($value)) 242 | { 243 | if ($plain) 244 | { 245 | $result .= $color_red_light.$value.$color_clear; 246 | } 247 | else 248 | { 249 | $result .= "".$value.""; 250 | } 251 | } 252 | else if (is_array($value)) 253 | { 254 | if ($plain) 255 | { 256 | $result .= "[]"; 257 | } 258 | else 259 | { 260 | $result .= "[]"; 261 | } 262 | } 263 | else 264 | { 265 | if ($plain) 266 | { 267 | $result .= $color_green_light."\"".$value."\"".$color_clear; 268 | } 269 | else 270 | { 271 | $result .= "\"".htmlspecialchars ($value,ENT_NOQUOTES,'UTF-8')."\""; 272 | } 273 | } 274 | if ($plain) 275 | { 276 | $result .= "\n"; 277 | } 278 | else 279 | { 280 | $result .= "
\n"; 281 | } 282 | } 283 | } 284 | if ($level>0) 285 | { 286 | return $result; 287 | } 288 | } 289 | else 290 | { 291 | if ($object===null) 292 | { 293 | if ($plain) 294 | { 295 | $result = $color_cyan."null".$color_clear; 296 | } 297 | else 298 | { 299 | $result = "null"; 300 | } 301 | } 302 | else if (is_bool($object)) 303 | { 304 | if ($object) 305 | { 306 | if ($plain) 307 | { 308 | $result = $color_purple_light."true".$color_clear; 309 | } 310 | else 311 | { 312 | 313 | $result = "true"; 314 | } 315 | } 316 | else 317 | { 318 | if ($plain) 319 | { 320 | $result = $color_purple_light."false".$color_clear; 321 | } 322 | else 323 | { 324 | $result = "false"; 325 | } 326 | } 327 | } 328 | else if (is_integer($object) || is_float($object)) 329 | { 330 | if ($plain) 331 | { 332 | $result = $color_red_light.$object.$color_clear; 333 | } 334 | else 335 | { 336 | $result = "".$object.""; 337 | } 338 | } 339 | else if (is_array($object)) 340 | { 341 | if ($plain) 342 | { 343 | $result = "[]"; 344 | } 345 | else 346 | { 347 | $result = "[]"; 348 | } 349 | } 350 | else 351 | { 352 | if ($plain) 353 | { 354 | $result = $color_green_light."\"".$object."\"".$color_clear; 355 | } 356 | else 357 | { 358 | $result = "\"".htmlspecialchars ($object,ENT_NOQUOTES,'UTF-8')."\""; 359 | } 360 | } 361 | if ($plain) 362 | { 363 | $result .= "\n"; 364 | } 365 | else 366 | { 367 | $result .= "
\n"; 368 | } 369 | } 370 | if (is_null($object)) 371 | { 372 | $type = 'null'; 373 | } 374 | else if (is_bool($object)) 375 | { 376 | $type = 'boolean'; 377 | } 378 | else if (is_object($object)) 379 | { 380 | $type = get_class($object); 381 | } 382 | else if (is_array($object)) 383 | { 384 | $type = 'array'; 385 | } 386 | else if (is_int($object)) 387 | { 388 | $type = 'integer'; 389 | } 390 | else if (is_float($object)) 391 | { 392 | $type = 'float'; 393 | } 394 | else 395 | { 396 | $type = 'string'; 397 | } 398 | if ($plain) 399 | { 400 | $header = ''; 401 | if ($title) 402 | { 403 | $header = $color_cyan; 404 | $header .= "--------------------------------------\n"; 405 | $header .= (is_bool($title) || $title===null)?$type:$title; 406 | $header .= "\n--------------------------------------\n"; 407 | $header .= $color_clear; 408 | } 409 | if (is_string($plain)) 410 | { 411 | if ($plain=='error_log') 412 | { 413 | error_log ("\n".$header.$result); 414 | } 415 | else 416 | { 417 | file_put_contents ($plain, $header.$result, FILE_APPEND); 418 | } 419 | } 420 | else 421 | { 422 | echo $header.$result; 423 | } 424 | } 425 | else 426 | { 427 | $trace = debug_backtrace(); 428 | $node = reset ($trace); 429 | $file = basename($node['file']).":".$node['line']; 430 | $debug = "
"; 431 | foreach ($trace as $key => $value) 432 | { 433 | if (isset($value['file']) && $value['line']) 434 | { 435 | $debug .= "".$value['file']." [".$value['line']."] ".$value['function']."
"; 436 | } 437 | else 438 | { 439 | $debug .= "".$value['function']."
"; 440 | } 441 | } 442 | $debug .= "
"; 443 | echo "
\n"; 444 | echo "
".(($title && !is_bool($title))?$title:$type)."
+
".$file."".$debug."
\n"; 445 | echo $result; 446 | echo "
\n"; 447 | } 448 | } 449 | 450 | ?> -------------------------------------------------------------------------------- /demo/demo.php: -------------------------------------------------------------------------------- 1 | 'alice',true=>false,1=>5,2=>1.4); 12 | public $object; 13 | } 14 | 15 | class test2 16 | { 17 | public $another; 18 | } 19 | 20 | class test3 21 | { 22 | public $string1 = "3d level"; 23 | public $string2 = "123"; 24 | public $complicated; 25 | } 26 | 27 | class test4 28 | { 29 | public $enough = "Level 4"; 30 | public $something = "Thanks for the fish!"; 31 | } 32 | 33 | $test = new test1 (); 34 | $test->object = new test2(); 35 | $test->object->another = new test3 (); 36 | $test->object->another->complicated = new test4 (); 37 | 38 | debug ($test); 39 | 40 | debug ($test, "This is debug title"); 41 | 42 | //Expand all levels while default expanded level depth is 2 43 | debug ($test, true); 44 | 45 | //Add * before title to expand all levels with title 46 | debug ($test, "* Expand all levels with title"); 47 | 48 | echo "
";
49 | 	debug ($test, "Output as plain text", true);
50 | 	echo "
"; 51 | 52 | debug ($test, date('Y-m-d H:i:s').": Save plain text to file ", "./test.log"); 53 | 54 | debug ($test, "Limit level rendering to 1", false, 1); 55 | 56 | $pi = 3.14159265359; 57 | debug ($pi, "hello this is pi"); 58 | 59 | $hm = array (1=>array(2=>array(3=>array(4=>array(5=>array(6=>array(7=>array(8=>"Last depth we created")))))))); 60 | debug ($hm, "* A very complicated expanded array"); 61 | debug ($hm, "* More levels", false, 10); 62 | 63 | ?> -------------------------------------------------------------------------------- /demo/php-cli-debug.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hazardland/debug.php/892008458cdbab1c1f8f669e69cbd7164bfeb6a6/demo/php-cli-debug.png -------------------------------------------------------------------------------- /demo/php-debug-object-expand.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hazardland/debug.php/892008458cdbab1c1f8f669e69cbd7164bfeb6a6/demo/php-debug-object-expand.png -------------------------------------------------------------------------------- /demo/php-debug-object.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hazardland/debug.php/892008458cdbab1c1f8f669e69cbd7164bfeb6a6/demo/php-debug-object.png -------------------------------------------------------------------------------- /demo/php-debug-pi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hazardland/debug.php/892008458cdbab1c1f8f669e69cbd7164bfeb6a6/demo/php-debug-pi.png -------------------------------------------------------------------------------- /demo/php-debug-sublime-protocol.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hazardland/debug.php/892008458cdbab1c1f8f669e69cbd7164bfeb6a6/demo/php-debug-sublime-protocol.png -------------------------------------------------------------------------------- /demo/php-debug.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hazardland/debug.php/892008458cdbab1c1f8f669e69cbd7164bfeb6a6/demo/php-debug.png -------------------------------------------------------------------------------- /demo/php-log-variable.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hazardland/debug.php/892008458cdbab1c1f8f669e69cbd7164bfeb6a6/demo/php-log-variable.png -------------------------------------------------------------------------------- /demo/php-visually-debug-array-full.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hazardland/debug.php/892008458cdbab1c1f8f669e69cbd7164bfeb6a6/demo/php-visually-debug-array-full.png -------------------------------------------------------------------------------- /demo/php-visually-debug-array.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hazardland/debug.php/892008458cdbab1c1f8f669e69cbd7164bfeb6a6/demo/php-visually-debug-array.png -------------------------------------------------------------------------------- /demo/test.log: -------------------------------------------------------------------------------- 1 | -------------------------------------- 2 | 2016-03-23 02:28:00: Save plain text to file 3 | -------------------------------------- 4 | string : "Test string" 5 | boolean : true 6 | integer : 17 7 | float : 9.99 8 | array (array) 9 | bob : "alice" 10 | 1 : 5 11 | 2 : 1.4 12 | object (test2) 13 | another (test3) 14 | string1 : "3d level" 15 | string2 : "123" 16 | complicated (test4) 17 | enough : "Level 4" 18 | something : "Thanks for the fish!" 19 | -------------------------------------- 20 | 2016-03-23 02:28:01: Save plain text to file 21 | -------------------------------------- 22 | string : "Test string" 23 | boolean : true 24 | integer : 17 25 | float : 9.99 26 | array (array) 27 | bob : "alice" 28 | 1 : 5 29 | 2 : 1.4 30 | object (test2) 31 | another (test3) 32 | string1 : "3d level" 33 | string2 : "123" 34 | complicated (test4) 35 | enough : "Level 4" 36 | something : "Thanks for the fish!" 37 | -------------------------------------- 38 | 2016-03-23 02:37:03: Save plain text to file 39 | -------------------------------------- 40 | string : "Test string" 41 | boolean : true 42 | integer : 17 43 | float : 9.99 44 | array (array) 45 | bob : "alice" 46 | 1 : 5 47 | 2 : 1.4 48 | object (test2) 49 | another (test3) 50 | string1 : "3d level" 51 | string2 : "123" 52 | complicated (test4) 53 | enough : "Level 4" 54 | something : "Thanks for the fish!" 55 | -------------------------------------- 56 | 2016-03-23 02:37:06: Save plain text to file 57 | -------------------------------------- 58 | string : "Test string" 59 | boolean : true 60 | integer : 17 61 | float : 9.99 62 | array (array) 63 | bob : "alice" 64 | 1 : 5 65 | 2 : 1.4 66 | object (test2) 67 | another (test3) 68 | string1 : "3d level" 69 | string2 : "123" 70 | complicated (test4) 71 | enough : "Level 4" 72 | something : "Thanks for the fish!" 73 | -------------------------------------- 74 | 2016-06-14 18:20:40: Save plain text to file 75 | -------------------------------------- 76 | string : "Test string" 77 | boolean : true 78 | integer : 17 79 | float : 9.99 80 | array (array) 81 | bob : "alice" 82 | 1 : 5 83 | 2 : 1.4 84 | object (test2) 85 | another (test3) 86 | string1 : "3d level" 87 | string2 : "123" 88 | complicated (test4) 89 | enough : "Level 4" 90 | something : "Thanks for the fish!" 91 | -------------------------------------------------------------------------------- /pull.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | git pull origin master -------------------------------------------------------------------------------- /push.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | IF %1.==. GOTO COMMENT 3 | git add * 4 | git commit -a -m %1 5 | git push origin master 6 | GOTO END 7 | :COMMENT 8 | echo no commit comment 9 | :END -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | function debug ($object) 2 | ================ 3 | 4 | function debug (mixed $object, string/boolean $title=null, boolean/string $plain=false, integer $limit=6) 5 | 6 | **debug** is a single function for visually analything / logging complex deep level objects and arrays. 7 | 8 | Example of debug call html output: 9 | 10 | ![](./demo/php-debug.png) 11 | 12 | Example of cli mode output 13 | 14 | ![](./demo/php-cli-debug.png) 15 | 16 | Or 17 | 18 | ![](./demo/php-visually-debug-array.png) 19 | 20 | Let us have an example classes (check ./demo/demo.php) 21 | ```php 22 | class test1 23 | { 24 | public $string = "Test string"; 25 | public $boolean = true; 26 | public $integer = 17; 27 | public $float = 9.99; 28 | public $array = array ('bob'=>'alice',true=>false,1=>5,2=>1.4); 29 | public $object; 30 | } 31 | 32 | class test2 33 | { 34 | public $another; 35 | } 36 | 37 | class test3 38 | { 39 | public $string1 = "3d level"; 40 | public $string2 = "123"; 41 | public $complicated; 42 | } 43 | 44 | class test4 45 | { 46 | public $enough = "Level 4"; 47 | public $something = "Thanks for the fish!"; 48 | } 49 | ``` 50 | 51 | And intialize them in a following manner: 52 | ```php 53 | $test = new test1 (); 54 | $test->object = new test2(); 55 | $test->object->another = new test3 (); 56 | $test->object->another->complicated = new test4 (); 57 | ``` 58 | 59 | Now let us start debugging **$test1**. A simpliest call: 60 | ```php 61 | debug ($test); 62 | ``` 63 | Will output following: 64 | 65 | ![](./demo/php-debug-object.png) 66 | 67 | Note that object of class *test3* is collapsed and only first property is visible you can unfold it by clicking + or you can just debug object with everything expanded by calling: 68 | 69 | ```php 70 | debug ($test, true); 71 | ``` 72 | So now we see hidden parts of our object by default 73 | 74 | ![](./demo/php-debug-object-expand.png) 75 | 76 | Putting title on debug: 77 | ```php 78 | $pi = 3.14159265359; 79 | debug ($pi, "hello this is pi"); 80 | ``` 81 | 82 | ![](./demo/php-debug-pi.png) 83 | 84 | To have both expanded and titled debug we should just put * symbol in the begining of title string like this: 85 | ```php 86 | $hm = array (1=>array(2=>array(3=>array(4=>array(5=>array(6=>array(7=>array(8=>"Last depth we created")))))))); 87 | debug ($hm, "* A very complicated expanded array"); 88 | ``` 89 | 90 | ![](./demo/php-visually-debug-array.png) 91 | 92 | On a depth level 6 (considering starting level is 0) only ... is visible because of depth rendering limit which is by default 6. To unlock other levels we should incrase limit. 93 | 94 | ```php 95 | debug ($hm, "* More levels", false, 10); 96 | ``` 97 | 98 | ![](./demo/php-visually-debug-array-full.png) 99 | 100 | Third parameter is for plain text output if you pass *true* 101 | ```php 102 | debug ($test, "Output something as plain text", true); 103 | ``` 104 | It will output something like following indented with 4 space tabs: 105 | 106 | ```php 107 | Output something as plain text 108 | ------------------- 109 | string : "Test string" 110 | boolean : true 111 | integer : 17 112 | float : 9.99 113 | array (array) 114 | bob : "alice" 115 | 1 : 5 116 | 2 : 1.4 117 | object (test2) 118 | another (test3) 119 | string1 : "3d level" 120 | string2 : "123" 121 | complicated (test4) 122 | enough : "Level 4" 123 | 124 | ``` 125 | 126 | To log plain text output in file just pass file path as a third parameter 127 | ```php 128 | debug ($test, date('Y-m-d H:i:s').": Save plain text to file ", "./test.log"); 129 | ``` 130 | 131 | ![](./demo/php-log-variable.png) 132 | 133 | And lastly if you are using subl-protocol plugin for sublime (https://github.com/thecotne/subl-protocol) you can unfold debug backtrace and with one click in browser jump on the specific file and line in sublime text editor: 134 | 135 | ![](./demo/php-debug-sublime-protocol.png) 136 | 137 | To debug in error log use 'error_log' in 3d parameter: 138 | ````PHP 139 | debug ($_GET,'GET','error_log'); 140 | ```` 141 | To view error log nicely formated use: 142 | ```` 143 | tail -f error_log | grep --line-buffered "\n--" | sed "s/\\\n/\\n/g" 144 | ```` 145 | Or if you want all other error log messages: 146 | ```` 147 | tail -f error_log | sed "s/\\\n/\\n/g" 148 | ```` --------------------------------------------------------------------------------