Debug & Utility Tools

Mempermudah proses debugging, logging, dan optimasi performa.

Debugging Functions

1. dump()


// Di Controller
public function example(): void {
    $data = [
        'user' => 'John Doe',
        'roles' => ['admin', 'editor']
    ];
    
    $this->dump($data);  // Menampilkan data dalam format yang mudah dibaca
    $this->dump($data, 'User Data');  // Dengan label
}
    
Note: Fungsi dump() secara otomatis memformat output dengan syntax highlighting dan struktur yang mudah dibaca.

2. debugRequest()


// Debugging request data
public function handleForm(): void {
    $this->debugRequest();  // Menampilkan semua data request (GET, POST, FILES)
    
    // Atau specific data
    $this->debugRequest('POST');  // Hanya data POST
    $this->debugRequest('GET');   // Hanya data GET
}
    

3. Raw()


// Menampilkan raw output tanpa processing template
$this->Raw($data);  // Output data langsung ke browser
$this->Raw($data, true);  // Dengan pretty print untuk array/object
    

Logging System

1. log()


// Basic logging
$this->log('User login attempt');

// Dengan level dan context
$this->log('Payment processed', 'info', [
    'user_id' => 123,
    'amount' => 99.99
]);

// Error logging
try {
    // some code
} catch (Exception $e) {
    $this->log($e->getMessage(), 'error', [
        'file' => $e->getFile(),
        'line' => $e->getLine()
    ]);
}
    

2. Log Levels

  • debug: Detailed debug information
  • info: Interesting events
  • notice: Normal but significant events
  • warning: Exceptional occurrences that are not errors
  • error: Runtime errors
  • critical: Critical conditions
  • alert: Action must be taken immediately
  • emergency: System is unusable

Cache Management

1. clearUICache()


// Clear all UI cache
$this->clearUICache();

// Clear specific cache
$this->clearUICache('templates');
$this->clearUICache('assets');
    

2. Cache Configuration


// In configuration
'cache' => [
    'enabled' => true,
    'path' => 'cache/',
    'lifetime' => 3600,  // 1 hour
    'compress' => true
]
    

HTML Utilities

1. html_standard()


// Standardize HTML output
$html = $this->html_standard($content);

// With options
$html = $this->html_standard($content, [
    'remove_comments' => true,
    'compress' => true,
    'fix_tags' => true
]);
    

Performance Tools

1. Performance Monitoring


// Start monitoring
$this->startTimer('database-query');

// Some database operations
$results = $db->query(...);

// End monitoring
$time = $this->endTimer('database-query');
$this->log("Query execution time: {$time}ms", 'debug');
    

2. Memory Usage


// Get memory usage
$memory = $this->getMemoryUsage();
$this->log("Current memory usage: {$memory}MB");

// Get peak memory
$peak = $this->getPeakMemoryUsage();
$this->log("Peak memory usage: {$peak}MB");
    

Best Practices

  • Gunakan log levels yang sesuai untuk memudahkan filtering
  • Aktifkan debugging hanya di environment development
  • Manfaatkan timer untuk mengidentifikasi bottleneck
  • Clear cache secara regular di production
  • Monitor memory usage untuk optimasi

Configuration


// config/debug.php
return [
    'debug' => [
        'enabled' => env('APP_DEBUG', false),
        'log_level' => env('APP_LOG_LEVEL', 'warning'),
        'display_errors' => env('APP_DISPLAY_ERRORS', false),
        'log_path' => 'storage/logs/',
        'pretty_print' => true,
        'max_log_files' => 30,
        'log_format' => 'Y-m-d H:i:s'
    ]
];
    
Warning: Pastikan untuk menonaktifkan debugging tools di environment production untuk keamanan dan performa.

Troubleshooting

  • Periksa permission file log jika logging gagal
  • Monitor ukuran file log dan rotate secara regular
  • Gunakan proper error handling di production
  • Set level logging yang sesuai dengan environment

Security Considerations

  • Jangan tampilkan error detail di production
  • Enkripsi data sensitif sebelum logging
  • Batasi akses ke file log
  • Regular cleanup untuk file debug dan log