Skip to content

Repository files navigation

Debug functions for PHP

CIcodecov

A collection of simple debug functions which might be helpful in case a full-fledged debugger is not available.

Requirements

The following versions of PHP are supported:

  • PHP 7.2
  • PHP 7.3
  • PHP 7.4

Installation

composer install --dev fabacino/debug-functions

Usage

  • dbg: Print out debug value.
  • dbgr: Return debug value.
  • dbglog: Log debug value into file.
  • dbginit: Initialize debug settings.

The functions accept any value for debugging, as long as it can be passed as a variable.

Examples

Print values to standard output

dbg(123);
dbg('a string');
dbg([
'token' => 'dp83kspo',
'is_default' => true
]);
// Output
123
a string
Array
(
[token] => dp83kspo
[is_default] => true
)

When running in a non-CLI environment the ouput is wrapped around pre tags for better formatting.

Use dbginit or the flag USE_VARDUMP in case you prefer var_dump:

dbginit(['use_vardump' => true]);
dbg(123);
// ordbg(123, Debug::USE_VARDUMP);
// which is the same asdbg(123, 1);
// Output
int(123)
string(8) "a string"
array(2) {
'token' => string(8) "dp83kspo"
'is_default' => bool(true)
}

Use dbginit or the flag USE_HTMLENTITIES in case you want to encode HTML entities:

dbginit(['use_htmlentities' => true]);
dbg('<b>important</b>');
// ordbg('<b>important</b>', Debug::USE_HTMLENTITIES);
// which is the same asdbg('<b>important</b>', 2);
// Output
&lt;b&gt;important&lt;/b&gt;

Save debug output in variable

// dbgr is the same as dbg, except that the output is returned instead of printed.$dbg = dbgr(123);

Log debug output to file

// dbglog is the same as dbg, except that the output is logged instead of printed.dbginit(['log_file' => '/path/to/logfile']);
dbglog($this->doSomething());

Alternatively you can pass your own PSR-3 compatible logger:

$logger = newYourLogger();
...dbginit(['logger' => $logger]);
dbglog($this->doSomething());

License

The MIT License (MIT). Please see License File for more information.

About

Debug functions for PHP

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages