PHP code example of smartdesk / debug-logger

1. Go to this page and download the library: Download smartdesk/debug-logger library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

smartdesk / debug-logger example snippets


use SmartDesk\Utils\DebugLogger;

// Simple logging
DebugLogger::debug(['user_id' => 123], 'init', 'User Login');

// With hook status
DebugLogger::log($data, ['plugins_loaded', 'init'], 'Startup Data');

use SmartDesk\Utils\DebugLogger;

// Enable only in development
if (defined('WP_DEBUG') && WP_DEBUG === true) {
    DebugLogger::setMinLevel(LogLevel::DEBUG);
}

// Custom log directory
$logDir = WP_CONTENT_DIR . '/uploads/smartdesk-logs';
DebugLogger::setWriter(
    DebugLogger::makeRotatingWriter($logDir, 'debug.log', 5_000_000, 5)
);

// Set minimum log level
DebugLogger::setMinLevel(LogLevel::WARNING);

// Uses PHP's error_log() function - no additional setup needed
DebugLogger::debug(['data' => 'value'], 'init', 'Debug Info');

// Set up rotating file writer
$logDir = WP_CONTENT_DIR . '/uploads/smartdesk-logs';
DebugLogger::setWriter(
    DebugLogger::makeRotatingWriter(
        $logDir,        // Directory path
        'debug.log',    // Base filename
        5_000_000,      // Max file size in bytes (5MB)
        5               // Number of backup files to keep
    )
);

use SmartDesk\Utils\Handlers\CallbackHandler;

// Custom callback handler
DebugLogger::setWriter(new CallbackHandler(function($message) {
    // Send to external service, database, etc.
    file_put_contents('/custom/path/app.log', $message, FILE_APPEND);
}));

use SmartDesk\Utils\Support\LogLevel;

// Set minimum log level (only logs at this level or higher will be output)
DebugLogger::setMinLevel(LogLevel::WARNING);  // Only WARNING, ERROR, CRITICAL, ALERT, EMERGENCY

// Available levels (from lowest to highest):
// LogLevel::DEBUG     ๐Ÿž
// LogLevel::INFO      โ„น๏ธ  
// LogLevel::NOTICE    ๐Ÿ“‹
// LogLevel::WARNING   โš ๏ธ
// LogLevel::ERROR     โŒ
// LogLevel::CRITICAL  โ˜ ๏ธ
// LogLevel::ALERT     ๐Ÿšจ
// LogLevel::EMERGENCY โ›”

// Define request marker for log correlation
if (!defined('SMARTDESK_REQ')) {
    define(
        'SMARTDESK_REQ',
        'Timestamp: ' . ($_SERVER['REQUEST_TIME_FLOAT'] ?? microtime(true))
        . ' / Request: ' . substr(md5($_SERVER['REQUEST_URI'] ?? ''), 0, 6)
        . ' / User: ' . (get_current_user_id() ?: 'guest')
    );
}

// Rotating file writer
DebugLogger::setWriter(
    DebugLogger::makeRotatingWriter('/path/to/logs', 'app.log', 5_000_000, 5)
);

// Custom callback writer
DebugLogger::setWriter(new CallbackHandler(function($message) {
    error_log($message);
}));

use SmartDesk\Utils\Support\LogLevel;

// Available levels (from lowest to highest):
// LogLevel::DEBUG     ๐Ÿž
// LogLevel::INFO      โ„น๏ธ  
// LogLevel::NOTICE    ๐Ÿ“‹
// LogLevel::WARNING   โš ๏ธ
// LogLevel::ERROR     โŒ
// LogLevel::CRITICAL  โ˜ ๏ธ
// LogLevel::ALERT     ๐Ÿšจ
// LogLevel::EMERGENCY โ›”
DebugLogger::setMinLevel(LogLevel::WARNING);

$writer = DebugLogger::makeRotatingWriter(
    WP_CONTENT_DIR . '/uploads/logs',
    'debug.log',
    2_000_000,  // 2MB
    4           // Keep 4 backup files
);

DebugLogger::debug($data, $hooks, $title);     // ๐Ÿž DEBUG
DebugLogger::info($data, $hooks, $title);      // โ„น๏ธ INFO  
DebugLogger::notice($data, $hooks, $title);    // ๐Ÿ“‹ NOTICE
DebugLogger::warning($data, $hooks, $title);   // โš ๏ธ WARNING
DebugLogger::error($data, $hooks, $title);     // โŒ ERROR
DebugLogger::critical($data, $hooks, $title);  // โ˜ ๏ธ CRITICAL
DebugLogger::alert($data, $hooks, $title);     // ๐Ÿšจ ALERT
DebugLogger::emergency($data, $hooks, $title); // โ›” EMERGENCY

use SmartDesk\Utils\DebugLogger;

add_action('plugins_loaded', function () {
    if (!defined('WP_DEBUG') || WP_DEBUG !== true) return;

    $dir = WP_CONTENT_DIR . '/uploads/smartdesk-logs';
    DebugLogger::setWriter(DebugLogger::makeRotatingWriter($dir, 'dev.log', 2_000_000, 4));

    if (!defined('SMARTDESK_REQ')) {
        define(
            'SMARTDESK_REQ',
            'Timestamp: ' . ($_SERVER['REQUEST_TIME_FLOAT'] ?? microtime(true))
            . ' / Request: ' . substr(md5($_SERVER['REQUEST_URI'] ?? ''), 0, 6)
        );
    }
});

use SmartDesk\Utils\DebugLogger;

// ๐Ÿž Debug Flag
$debug = 1 === 1;

$debug && DebugLogger::log(
    [
        '๐Ÿงช foo' => 'bar',
        '๐Ÿงช list' => ['a', 'b', 'c'],
    ],
    'register',   // preset(s) or single hooks
    'My Block'    // optional title
);

use SmartDesk\Utils\DebugLogger;

DebugLogger::hook(['admin', 'enqueue'], 'Hooks');

// Usage examples with presets
DebugLogger::log($data, 'load', 'Plugin Loading');      // WordPress loading hooks
DebugLogger::log($data, 'register', 'Registration');    // Plugin/theme registration
DebugLogger::log($data, 'admin', 'Admin Area');         // Admin-specific hooks
DebugLogger::log($data, 'frontend', 'Frontend');        // Frontend-specific hooks
DebugLogger::log($data, 'enqueue', 'Script Loading');   // Script/style enqueuing
DebugLogger::log($data, 'woo', 'WooCommerce');          // WooCommerce hooks
DebugLogger::log($data, 'rest', 'REST API');            // REST API hooks
DebugLogger::log($data, 'request', 'Request Handling'); // Request processing

use SmartDesk\Utils\DebugLogger;
use SmartDesk\Utils\Support\LogLevel;

DebugLogger::timerStart('import');
// ... prepare whatever
DebugLogger::timerLap('import', 'after preparing whatever');
// ... work ...
DebugLogger::timerStop('import', 'Import job', LogLevel::NOTICE);

/**
 * Plugin Name: SmartDesk Core
 */

use SmartDesk\Utils\DebugLogger;
use SmartDesk\Utils\Support\LogLevel;

// Composer Autoload
   $logsDir = WP_CONTENT_DIR . '/uploads/smartdesk-logs';

    DebugLogger::setWriter(
        DebugLogger::makeRotatingWriter($logsDir, 'core.log', 2_000_000, 4)
    );
    
    DebugLogger::info([
        'wp' => get_bloginfo('version'),
        'php' => PHP_VERSION,
    ], 'plugins_loaded', 'Core Plugin Loaded');
});

/*
Plugin Name: SmartDesk DebugLogger
License: GPLv3 or later
License URI: https://www.gnu.org/licenses/gpl-3.0.html
*/

[20-Aug-2025 04:58:25 UTC] SmartDesk\Admin\WooMembershipPlanPricing\Main::{closure:SmartDesk\Admin\WooMembershipPlanPricing\Main::__construct():74}() - 04:58:25,56937
'Timestamp: 1755665904.8372 / Request: 394713'
C:\Users\SmartDesk\...\public\wp-wp_loaded (1 times fired)
    AT states count (via getter, after wp_loaded): 9
    person => {
            id => 1
            wp_capabilities => {
                    administrator => true
                    }
            role => administrator
            nickname => admin
            locale => [empty-string]
            show_welcome_panel => false
            ...
            }
---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|-SmartDesk