PHP code example of bradmkjr / monolog-wordpress

1. Go to this page and download the library: Download bradmkjr/monolog-wordpress 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/ */

    

bradmkjr / monolog-wordpress example snippets


// Import class
use WordPressHandler\WordPressHandler;

// Create WordPressHandler
$wordPressHandler = new WordPressHandler(null, "log", ['username', 'userid'], \Monolog\Logger::DEBUG);

// Configure maximum number of rows to keep (old entries are deleted when reached)
$wordPressHandler->conf_table_size_limiter( 250000 );

// Setup array of extra fields
$record = ['extra' => []];

// Create database table if needed, add extra fields from above
$wordPressHandler->initialize($record);

// Create Logger
$context = 'channel-name';
$logger = new \Monolog\Logger($context);

// Add WordPressHandler as the Handler for the Logger
$logger->pushHandler($wordPressHandler);

// Now you can use the logger, and further attach additional information
$logger->warning("This is a great message, woohoo!", ['username'  => 'John Doe', 'userid'  => 245]);



// Create the logs table if it doesn't already exist on plugin activation
register_activation_hook(__FILE__, 'my_plugin_activation');
function my_plugin_activation() {
    $handler = new \WordPressHandler\WordPressHandler(
        null, "logs",
        array('username', 'userid'),
        \Monolog\Logger::DEBUG
    );

    // setup array of extra fields
    $record = array('extra' => array());

    // creates database table if needed, add extra fields from above
    $handler->initialize($record);
}

$logger = new \Monolog\Logger('channel');
$handler = new \WordPressHandler\WordPressHandler(
    null, "logs",
    [],
    \Monolog\Logger::DEBUG
);
$handler->initialized = true; // Don't do any extra work - we've already done it.
$logger->pushHandler($handler);

$logger->warning('Some message');

register_uninstall_hook(__FILE__, 'my_plugin_uninstall');
function my_plugin_uninstall()
{
        \Monolog\Logger::DEBUG
    );
    $handler->uninitialize();
}

use WordPressHandler\WordPressHandler;

nction' );
function demo_function(){
    // Create a WordPressHandler instance
    $wordPressHandler = new WordPressHandler(null, "log", ['app', 'version'], \Monolog\Logger::DEBUG);
    
    // Create logger
    $context = 'test-plugin-logging';
    $logger = new \Monolog\Logger($context);
    
    // Add WordPressHandler as the Handler for the Logger
    $logger->pushHandler($wordPressHandler);
    
    // Now you can use the logger, and further attach additional information
    $logger->warning("This is a great message, woohoo!", ['app'  => 'Test Plugin', 'version'  => '2.4.5']);
}