Debugging

Concepts 2023-11-30 11:00

Debugging

Debugging your configuration and application.

Sentry

With Sentry exceptions and errors could be analyzed in much more comfortable way. If Sentry is available, then an appropriate project must be created in Sentry and the resulting DSN must be stored in the file “.SENTRY”.

File “.SENTRY”

After the Sentry DSN of the appropriate project has been stored in file “.SENTRY” everything around exceptions and errors gets pushed to that Sentry project.

https://__sentry_key__@__sentry_url__/__sentry_id__

Hooks

Specially when it comes to implementing additional functionality using Hooks debugging becomes even more important.

Object "$__DEBUG"

In every Hook this object is available and initialized.

log($message, $level = ‘debug')  Log a message in the current context. 
file($filename, $message, $log = ‘debug’)  Write a message to the given filename and log it in the current context.
log_pause($enable)  

If the Hook must process a heavy load - e.g. process many records - the logging may slow down the performance.

Enabling log pause disables the writing of the logs and keep the entries in memory until log pause is disabled which then writes all log messages to the appropriate logger at once.

assert($condition, $message = ‘’, $level = ‘debug’)  

Checks if the condition is true.

If so, nothing happens.

If not, then - if given - the message is logged. If in development the Hook is aborted.

benchmark_begin($tag)  Starts a benchmark timer with the given tag. An appropriate hint is logged.
benchmark_end()  Ends the benchmark timer. An appropriate hint is logged with the resulting time.
The available parent settings

See the following example:

// Example for using $__DEBUG
// 
	$__DEBUG->log_pause(true);
	$__DEBUG->benchmark_begin("my_hook");
	
	foreach ($page_items as $temp_i => $page_item) {
		$__DEBUG->log("Processing page item " . $temp_i);
		
		// Do something with $page_item
	}
	
	$__DEBUG->benchmark_end();
	$__DEBUG->log_pause(false);
// 

Object "$__REQUEST"

This object is available and initialized globally and is used to automatically store informations about the current processed request into access logs or a Matomo server.

If using a Matomo server then you would be able to make the current request something special like an `event` or `goal`.

set_goal($idGoal, $revenue = 0.0, $orderId = '')  If `$orderId` is given, then the current request becomes an ecommerce order with the given value, otherwise the goal with id `$idGoal` is used.
set_event($category, $action, $name)  Makes the current request an event with given category, action and name.
set_alt_user($alt_user_id)  Will set the given user id as the active user getting stored in access logs or a Matomo server. It will not change the security context of the active session or request.
The available parent settings

See the following example:

// Example for using $__REQUEST
// 
	global $__REQUEST;

	$__REQUEST->set_goal(1, 99, 'ORDER123');
// 

Class “SafeScope”

Specially when in a Hook having to pay attention to close special additional external connections and/or clean up temporary stuff could make things confusing and unclear if there are many control pathes.

Having this a more structured way this class is provided.

// Example for using SafeScope
// 
	$some_handle = something_to_open("my_service");
	
	$__DEBUG->assert($some_handle);
	
	$__SCOPE = new SafeScope([
		'__destruct' => function () use (&$some_handle) {
			something_to_close($some_handle);
		}
	]);
	
	// Things could be done with $some_handle without 
	// having it to closed.
// 

It is possible to attach additionally functions.

// Example for using SafeScope
// 
	$some_handle = something_to_open("my_service");
	
	$__DEBUG->assert($some_handle);
	
	$__SCOPE = new SafeScope([
		'__destruct' => function () use (&$some_handle) {
			something_to_close($some_handle);
		},
		'ping' => function () use (&$some_handle) {
			something_to_ping($some_handle);
		}
	]);
	
	// Things could be done with $some_handle without 
	// having it to closed.
	
	$__SCOPE->ping();
	
	// Something we could do via Scope while processing the Hook
//