Class Debugger
Provide custom logging and error handling.
Debugger overrides PHP's default error handling to provide stack traces and enhanced logging
Link: http://book.cakephp.org/2.0/en/development/debugging.html#debugger-class
Copyright: Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
License: MIT License
Location: Cake/Utility/Debugger.php
Properties summary
-
$_data
protectedHolds current output data when outputFormat is false.string
-
$_outputFormat
protectedThe current output format.string
-
$_templates
protectedstring
Templates used when generating trace or error strings. Can be global or indexed by the format value used in $_outputFormat.
-
$errors
publicA list of errors generated by the application.array
Method Summary
- __construct() publicConstructor.
- Export an array type object. Filters out keys used in datasource configuration.
- Protected export function used to keep track of indentation and recursion.
-
Wraps the highlight_string function in case the server API does not implement the function as it is the case of the HipHop interpreter
- Handles object to string conversion.
- Add an output format or update a format in Debugger.
- Verifies that the application's salt and cipher seed value has been changed from the default value.
- Recursively formats and outputs the contents of the supplied variable.
- Grabs an excerpt from a file and highlights a given line of code.
- Converts a variable to a string for debug output.
- Returns a reference to the Debugger singleton object instance.
-
Get the type of the given variable. Will return the class name for objects.
-
Creates an entry in the log file. The log entry will contain a stack trace from where it was called. as well as export the variable using exportVar. By default the log is written to the debug log.
-
Switches output format, updates format strings. Can be used to switch the active output format:
- Get/Set the output format for Debugger error rendering.
- outputError() publicTakes a processed array of data from an error and displays it in the chosen format.
- Overrides PHP's default error handling.
- Outputs a stack trace based on the supplied options.
-
Shortens file paths by replacing the application base path with 'APP', and the CakePHP core path with 'CORE'.
Method Detail
_array()source protected static
_array( array $var , integer $depth , integer $indent )
Export an array type object. Filters out keys used in datasource configuration.
The following keys are replaced with ***'s
- password
- login
- host
- database
- port
Parameters
- array
$var
- The array to export.
- integer
$depth
- The current depth, used for recursion tracking.
- integer
$indent
- The current indentation level.
Returns
stringExported array.
_export()source protected static
_export( mixed $var , integer $depth , integer $indent )
Protected export function used to keep track of indentation and recursion.
Parameters
- mixed
$var
- The variable to dump.
- integer
$depth
- The remaining depth.
- integer
$indent
- The current indentation level.
Returns
stringThe dumped variable.
_highlight()source protected static
_highlight( string $str )
Wraps the highlight_string function in case the server API does not implement the function as it is the case of the HipHop interpreter
Parameters
- string
$str
- the string to convert
Returns
string_object()source protected static
_object( string $var , integer $depth , integer $indent )
Handles object to string conversion.
Parameters
- string
$var
- Object to convert
- integer
$depth
- The current depth, used for tracking recursion.
- integer
$indent
- The current indentation level.
Returns
stringSee
Debugger::exportVar()addFormat()source public static
addFormat( string $format , array $strings )
Add an output format or update a format in Debugger.
Debugger::addFormat('custom', $data);
Where $data is an array of strings that use CakeText::insert() variable replacement. The template vars should be in a {:id}
style. An error formatter can have the following keys:
- 'error' - Used for the container for the error message. Gets the following template variables:
id
,error
,code
,description
,path
,line
,links
,info
- 'info' - A combination of
code
,context
andtrace
. Will be set with the contents of the other template keys. - 'trace' - The container for a stack trace. Gets the following template variables:
trace
- 'context' - The container element for the context variables. Gets the following templates:
id
,context
- 'links' - An array of HTML links that are used for creating links to other resources. Typically this is used to create javascript links to open other sections. Link keys, are:
code
,context
,help
. See the js output format for an example. - 'traceLine' - Used for creating lines in the stacktrace. Gets the following template variables:
reference
,path
,line
Alternatively if you want to use a custom callback to do all the formatting, you can use the callback key, and provide a callable:
Debugger::addFormat('custom', array('callback' => array($foo, 'outputError'));
The callback can expect two parameters. The first is an array of all the error data. The second contains the formatted strings generated using the other template strings. Keys like info
, links
, code
, context
and trace
will be present depending on the other templates in the format type.
Parameters
- string
$format
Format to use, including 'js' for JavaScript-enhanced HTML, 'html' for straight HTML output, or 'txt' for unformatted text.
- array
$strings
- Template strings, or a callback to be used for the output format.
Returns
Theresulting format string set.
checkSecurityKeys()source public static
checkSecurityKeys( )
Verifies that the application's salt and cipher seed value has been changed from the default value.
dump()source public static
dump( mixed $var , integer $depth 3 )
Recursively formats and outputs the contents of the supplied variable.
Parameters
- mixed
$var
- the variable to dump
- integer
$depth
optional 3 - The depth to output to. Defaults to 3.
See
Debugger::exportVar()Link
http://book.cakephp.org/2.0/en/development/debugging.html#Debugger::dumpexcerpt()source public static
excerpt( string $file , integer $line , integer $context 2 )
Grabs an excerpt from a file and highlights a given line of code.
Usage:
Debugger::excerpt('/path/to/file', 100, 4);
The above would return an array of 8 items. The 4th item would be the provided line, and would be wrapped in <span class="code-highlight"></span>
. All of the lines are processed with highlight_string() as well, so they have basic PHP syntax highlighting applied.
Parameters
- string
$file
- Absolute path to a PHP file
- integer
$line
- Line number to highlight
- integer
$context
optional 2 - Number of lines of context to extract above and below $line
Returns
arraySet of lines highlighted
See
http://php.net/highlight_stringLink
http://book.cakephp.org/2.0/en/development/debugging.html#Debugger::excerptexportVar()source public static
exportVar( string $var , integer $depth 3 )
Converts a variable to a string for debug output.
Note: The following keys will have their contents replaced with *****
:
- password
- login
- host
- database
- port
This is done to protect database credentials, which could be accidentally shown in an error message if CakePHP is deployed in development mode.
Parameters
- string
$var
- Variable to convert
- integer
$depth
optional 3 - The depth to output to. Defaults to 3.
Returns
stringVariable as a formatted string
Link
http://book.cakephp.org/2.0/en/development/debugging.html#Debugger::exportVargetInstance()source public static
getInstance( string $class null )
Returns a reference to the Debugger singleton object instance.
Parameters
- string
$class
optional null - Debugger class name.
Returns
objectgetType()source public static
getType( mixed $var )
Get the type of the given variable. Will return the class name for objects.
Parameters
- mixed
$var
- The variable to get the type of
Returns
stringThe type of variable.
log()source public static
log( mixed $var , integer $level LOG_DEBUG , integer $depth 3 )
Creates an entry in the log file. The log entry will contain a stack trace from where it was called. as well as export the variable using exportVar. By default the log is written to the debug log.
Parameters
- mixed
$var
- Variable or content to log
- integer
$level
optional LOG_DEBUG - type of log to use. Defaults to LOG_DEBUG
- integer
$depth
optional 3 - The depth to output to. Defaults to 3.
Link
http://book.cakephp.org/2.0/en/development/debugging.html#Debugger::logoutput()source public static
output( string $format null , array $strings array() )
Switches output format, updates format strings. Can be used to switch the active output format:
Deprecated
3.0.0 Use Debugger::outputAs() and Debugger::addFormat(). Will be removed in 3.0
Parameters
- string
$format
optional null Format to use, including 'js' for JavaScript-enhanced HTML, 'html' for straight HTML output, or 'txt' for unformatted text.
- array
$strings
optional array() - Template strings to be used for the output format.
Returns
stringoutputAs()source public static
outputAs( string $format null )
Get/Set the output format for Debugger error rendering.
Parameters
- string
$format
optional null The format you want errors to be output as. Leave null to get the current format.
Returns
mixedReturns null when setting. Returns the current format when getting.
Throws
CakeException
when choosing a format that doesn't exist.
outputError()source public
outputError( string $data )
Takes a processed array of data from an error and displays it in the chosen format.
Parameters
- string
$data
- Data to output.
showError()source public static
showError( integer $code , string $description , string $file null , integer $line null , array $context null )
Overrides PHP's default error handling.
Deprecated
3.0.0 Will be removed in 3.0. This function is superseded by Debugger::outputError().Parameters
- integer
$code
- Code of error
- string
$description
- Error description
- string
$file
optional null - File on which error occurred
- integer
$line
optional null - Line that triggered the error
- array
$context
optional null - Context
Returns
boolean|nullTrue if error was handled, otherwise null.
trace()source public static
trace( array $options array() )
Outputs a stack trace based on the supplied options.
Options
-
depth
- The number of stack frames to return. Defaults to 999 -
format
- The format you want the return. Defaults to the currently selected format. If format is 'array' or 'points' the return will be an array. -
args
- Should arguments for functions be shown? If true, the arguments for each method call will be displayed. -
start
- The stack frame to start generating a trace from. Defaults to 0
Parameters
- array
$options
optional array() - Format for outputting stack trace
Returns
mixedFormatted stack trace
Link
http://book.cakephp.org/2.0/en/development/debugging.html#Debugger::tracetrimPath()source public static
trimPath( string $path )
Shortens file paths by replacing the application base path with 'APP', and the CakePHP core path with 'CORE'.
Parameters
- string
$path
- Path to shorten
Returns
stringNormalized path
Properties detail
$_templatessource
protected string
Templates used when generating trace or error strings. Can be global or indexed by the format value used in $_outputFormat.
array( 'log' => array( 'trace' => '{:reference} - {:path}, line {:line}', 'error' => "{:error} ({:code}): {:description} in [{:file}, line {:line}]" ), 'js' => array( 'error' => '', 'info' => '', 'trace' => '<pre class="stack-trace">{:trace}</pre>', 'code' => '', 'context' => '', 'links' => array(), 'escapeContext' => true, ), 'html' => array( 'trace' => '<pre class="cake-error trace"><b>Trace</b> <p>{:trace}</p></pre>', 'context' => '<pre class="cake-error context"><b>Context</b> <p>{:context}</p></pre>', 'escapeContext' => true, ), 'txt' => array( 'error' => "{:error}: {:code} :: {:description} on line {:line} of {:path}\n{:info}", 'code' => '', 'info' => '' ), 'base' => array( 'traceLine' => '{:reference} - {:path}, line {:line}', 'trace' => "Trace:\n{:trace}\n", 'context' => "Context:\n{:context}\n", ) )
© 2005–2016 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/2.9/class-Debugger.html