BricksMembers includes debug logging functions to help you troubleshoot issues and understand plugin behavior during development.
Enable Debug Mode
Enable debug logging in your wp-config.php:
// Enable BricksMembers logging
define( 'BRM_DEBUG', true );
define( 'BRM_DEBUG_LEVEL', 'debug' ); // Options: debug, info, warning, error
If BRM_DEBUG is not defined, the plugin falls back to WordPress debug settings (WP_DEBUG and WP_DEBUG_LOG).
Which to use? Use BRM_DEBUG to enable logging. By default the logger records all levels (debug/info/warning/error); set BRM_DEBUG_LEVEL to raise the threshold.
What Gets Logged
Logging calls already exist across the plugin and will emit when debug mode is on. Examples include:
- Security/auth: webhook auth failures/rate limits, AJAX nonce/capability failures, API key verification
- Core flows: access denials, level assignments, recompute scheduling, batch worker start/finish (SPM, Progress, Drip)
- Data mutations: level create/update/delete failures, progress toggle failures (already-completed/not-tracked)
- Modules: protected downloads activity, licensing checks, rate-limiting events
Logging Functions
Use the \BaselMedia\BricksMembers\Utilities\DebugLogger class to log at different levels (requires BRM_DEBUG or WP_DEBUG + WP_DEBUG_LOG):
use BaselMedia\BricksMembers\Utilities\DebugLogger;
// Debug messages (detailed information)
DebugLogger::log_debug( 'User levels retrieved', [
'user_id' => 123,
'levels' => [1, 2, 5]
] );
// Info messages (general information)
DebugLogger::log_info( 'Structure recompute started', [
'structure_id' => 'courses'
] );
// Warning messages (potential issues)
DebugLogger::log_warning( 'Slow query detected', [
'query_time' => 1.5
] );
// Error messages (failures)
DebugLogger::log_error( 'Failed to assign level', [
'user_id' => 123,
'level_id' => 5
] );
Exception Logging
Log exceptions with full context:
use BaselMedia\BricksMembers\Utilities\DebugLogger;
try {
$result = brm_add_user_level( $user_id, $level_id );
} catch ( Exception $e ) {
DebugLogger::log_exception( $e, [
'user_id' => $user_id,
'level_id' => $level_id,
'operation' => 'add_user_level'
] );
}
Performance Logging
Track operation performance:
use BaselMedia\BricksMembers\Utilities\DebugLogger;
$start_time = microtime( true );
// Perform operation (delegates to the unified orchestration service)
$result = brm_schedule_unified_recompute( [
'structures' => [ $structure_id ],
'spm' => true
] );
$duration = microtime( true ) - $start_time;
DebugLogger::log_performance( 'unified_recompute', $duration, [
'structure_id' => $structure_id,
'result' => $result
] );
Log Output
Logs are written to WordPress debug log (usually wp-content/debug.log) with structured information:
[BRM][DEBUG] core-system.php:245 → get_user_levels() | User levels retrieved | user=123 | data={"user_id":123,"levels":[1,2,5]}
[BRM][ERROR] core-system.php:512 → add_user_level() | Failed to assign level | user=123 | data={"user_id":123,"level_id":5}
Using Logs in Your Code
Add logging to your custom code that extends BricksMembers:
use BaselMedia\BricksMembers\Utilities\DebugLogger;
// Log custom operations
add_action('brm_event_access_granted', function($event) {
DebugLogger::log_info('Custom: Access granted via webhook', [
'user_id' => $event->get_user_id(),
'resource_id' => $event->get_resource_id(),
'source' => 'webhook'
]);
// Your custom logic
});
// Log access decisions
add_action('brm_event_access_granted', function($event) {
DebugLogger::log_debug('Access granted', [
'user_id' => $event->get_user_id(),
'resource_id' => $event->get_resource_id(),
]);
});
Best Practices
- Use appropriate log levels: Use debug for detailed info, error for failures
- Include context: Always pass relevant data as the second parameter
- Don’t log sensitive data: Never log passwords, API keys, or personal information
- Disable in production: Only enable
BRM_DEBUGduring development - Review logs regularly: Check logs to identify performance issues or errors