PHP code example of wp-spaghetti / wonolog-handler

1. Go to this page and download the library: Download wp-spaghetti/wonolog-handler 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/ */

    

wp-spaghetti / wonolog-handler example snippets




use WpSpaghetti\WonologHandler\Handler\WonologHandler;

return [
    'default' => env('LOG_CHANNEL', 'stack'),

    'channels' => [
        // Recommended: Stack with Wonolog + file backup
        'stack' => [
            'driver' => 'stack',
            'channels' => ['wonolog', 'single'],
            'ignore_exceptions' => false,
        ],

        // Wonolog channel
        'wonolog' => [
            'driver' => 'monolog',
            'handler' => WonologHandler::class,
            'level' => env('LOG_LEVEL', 'debug'),
        ],

        // File backup (optional but recommended)
        'single' => [
            'driver' => 'single',
            'path' => storage_path('logs/laravel.log'),
            'level' => env('LOG_LEVEL', 'debug'),
        ],
    ],
];

use Illuminate\Support\Facades\Log;

// Anywhere in your Laravel + WordPress code
Log::debug('Debugging information');
Log::info('Informational message');
Log::notice('Normal but significant event');
Log::warning('Warning condition');
Log::error('Error condition');
Log::critical('Critical condition');
Log::alert('Action must be taken immediately');
Log::emergency('System is unusable');

Log::error('Payment failed', [
    'user_id' => $userId,
    'amount' => $amount,
    'error' => $exception->getMessage(),
]);

// Wonolog channels - use UPPERCASE by convention
Log::error('Security breach', [
    'channel' => 'SECURITY',  // ✅ Correct
    'ip' => $ipAddress,
]);

// Avoid lowercase - may not be tracked
// Log::error('Breach', ['channel' => 'security']); // ❌ May not work

// Use only Wonolog (no file backup)
Log::channel('wonolog')->error('Critical error');

// Use only file logging
Log::channel('single')->debug('Debug info');

// Use stack (Wonolog + file) - recommended
Log::channel('stack')->warning('Warning message');

// In app/Controllers/App.php or any controller
use Illuminate\Support\Facades\Log;

public function index()
{
    Log::info('Page viewed', ['url' => request()->url()]);
    return $this->view;
}

// In your custom plugins or theme
use Illuminate\Support\Facades\Log;

add_action('init', function() {
    Log::info('WordPress initialized');
});

use Corcel\Model\Post;
use Illuminate\Support\Facades\Log;

$posts = Post::published()->get();
Log::info('Fetched posts', ['count' => $posts->count()]);



return [
    // Custom Wonolog namespace (for wpify/scoper)
    'namespace' => env('WONOLOG_NAMESPACE', 'Inpsyde\\Wonolog'),
    
    // Custom action hook (for wpify/scoper or custom naming)
    'action' => env('WONOLOG_ACTION', 'wonolog.log'),
    
    // Stop propagation when Wonolog is active?
    'stop_propagation' => env('WONOLOG_STOP_PROPAGATION', false),
];

// config/wonolog.php
'namespace' => 'WpSpaghetti\\Deps\\Inpsyde\\Wonolog',

add_filter('wonolog_handler.namespace', function () {
    return 'WpSpaghetti\\Deps\\Inpsyde\\Wonolog';
});

// config/wonolog.php
'action' => 'custom_wonolog.log',

add_filter('wonolog_handler.action', function () {
    return 'custom_wonolog.log';
});

// config/wonolog.php
'stop_propagation' => true,

// User specifies Wonolog channel
Log::error('Error', ['channel' => 'SECURITY', 'ip' => '1.2.3.4']);
// Result: Channel = SECURITY, Context = ['ip' => '1.2.3.4'] (no 'channel' key)

// No channel specified
Log::error('Error');
// Result: Channel = DEBUG (Wonolog's default), Context = [] (empty except datetime/extra)

// Monolog channel is ignored
Log::channel('stack')->error('Error');
// Result: Channel = DEBUG (Wonolog's default), NOT 'stack'

use WpSpaghetti\WonologHandler\Support\WonologDetector;

$detector = app(WonologDetector::class);

if (!$detector->isActive()) {
    echo "Wonolog is not active!";
    echo "Namespace: " . $detector->getNamespace();
    echo "Action: " . $detector->getAction();
}

   Log::error('Security breach', ['channel' => 'SECURITY', 'ip' => '1.2.3.4']);
   // Email: Channel = SECURITY, Context = ['ip' => '1.2.3.4'] (no 'channel' key)
   

   Log::error('Error');
   // Email: Channel = DEBUG (Wonolog's default)
   
   Log::channel('stack')->error('Error');
   // Email: Channel = DEBUG (Wonolog's default - Monolog channel is ignored)
   
   Log::channel('single')->error('Error');
   // Email: Channel = DEBUG (Wonolog's default - Monolog channel is ignored)
   

   // ✅ Correct - uppercase
   Log::error('Breach', ['channel' => 'SECURITY']);
   
   // ❌ May not work - lowercase
   Log::error('Breach', ['channel' => 'security']);
   
   // ❌ May not work - non-configured channel
   Log::error('Error', ['channel' => 'FOO']);
   

'stack' => [
    'driver' => 'stack',
    'channels' => ['wonolog', 'single'], // ← Check this
],