businessdad icon

Aelia - Sample Monolog handler for WooCommerce log table

businessdad | PRO | 09/15/22 01:41:33 PM UTC (Edited) | 0 ⭐ | 942 👁️ | Never ⏰ | [logger, woocommerce]
PHP |

2.98 KB

|

None

|

0 👍

/

0 👎

<?php
if(!defined('ABSPATH')) { exit; } // Exit if accessed directly
 
use Aelia\Dependencies\Monolog\Logger;
 
/**
 * PROTOTYPE
 * Monolog handler to store entries in WooCommerce's log table.
 *
 * DISCLAIMER
 * Aelia and any member of its staff are not responsible for any data loss or damage incurred
 * when using the code, which you can use at your own risk.
 *
 * GPL DISCLAIMER
 * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
 * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
 * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
 * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
 * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
 *
 * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
 */
class Aelia_DB_Handler extends \Aelia\Dependencies\Monolog\Handler\AbstractProcessingHandler {
    /**
     * @var bool defines whether the MySQL connection is been initialized
     */
    private $initialized = false;
 
    /**
     * @var string the table to store the logs in
     */
    protected $log_table = '';
 
    /**
     * Constructor of this class, sets the PDO and calls parent constructor
     *
     * @param bool|int $level Debug level which this handler should store
     * @param bool $bubble
     */
    public function __construct($level = Logger::DEBUG, $bubble = true) {
        global $wpdb;
        $this->wpdb = $wpdb;
        $this->log_table = $this->wpdb->prefix . 'woocommerce_log';
 
        parent::__construct($level, $bubble);
    }
 
    /**
     * Initializes this handler by creating the table if it not exists
     */
    protected function initialize() {
        $this->initialized = true;
    }
 
    /**
     * Prepares a record that will be inserted in the audit log.
     *
     * @param array record An array of audit data.
     * @return array A record, ready for insertion in the audit log.
     */
    protected function prepare_content_for_insert(array $record) {
        $timestamp = $record['datetime'];
        $insert_record = array(
            'timestamp' => $timestamp->format('Y-m-d H:i:s'),
            'level' => $record['level'],
            'message' => $record['message'],
            'source' => $record['channel'],
        );
 
        $insert_record['context'] = !empty($record['context']) ? serialize($record['context']) : '';
 
        return $insert_record;
    }
 
    /**
     * Writes the record to the audit table.
     *
     * @param array $record
     * @return void
     */
    protected function write(array $record) {
        if(!$this->initialized) {
            $this->initialize();
        }
 
        $format = array(
            '%s',
            '%d',
            '%s',
            '%s',
            '%s', // possible serialized context.
        );
 
        return $this->wpdb->insert($this->log_table, $this->prepare_content_for_insert($record), $format) !== false;
    }
}

Comments