Advertisement
businessdad

Aelia - Sample Monolog handler for WooCommerce log table

Mar 29th, 2019 (edited)
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.98 KB | None | 0 0
  1. <?php
  2. if(!defined('ABSPATH')) { exit; } // Exit if accessed directly
  3.  
  4. use Aelia\Dependencies\Monolog\Logger;
  5.  
  6. /**
  7.  * PROTOTYPE
  8.  * Monolog handler to store entries in WooCommerce's log table.
  9.  *
  10.  * DISCLAIMER
  11.  * Aelia and any member of its staff are not responsible for any data loss or damage incurred
  12.  * when using the code, which you can use at your own risk.
  13.  *
  14.  * GPL DISCLAIMER
  15.  * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
  16.  * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
  17.  * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
  18.  * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
  19.  * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
  20.  *
  21.  * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
  22.  */
  23. class Aelia_DB_Handler extends \Aelia\Dependencies\Monolog\Handler\AbstractProcessingHandler {
  24.     /**
  25.      * @var bool defines whether the MySQL connection is been initialized
  26.      */
  27.     private $initialized = false;
  28.  
  29.     /**
  30.      * @var string the table to store the logs in
  31.      */
  32.     protected $log_table = '';
  33.  
  34.     /**
  35.      * Constructor of this class, sets the PDO and calls parent constructor
  36.      *
  37.      * @param bool|int $level Debug level which this handler should store
  38.      * @param bool $bubble
  39.      */
  40.     public function __construct($level = Logger::DEBUG, $bubble = true) {
  41.         global $wpdb;
  42.         $this->wpdb = $wpdb;
  43.         $this->log_table = $this->wpdb->prefix . 'woocommerce_log';
  44.  
  45.         parent::__construct($level, $bubble);
  46.     }
  47.  
  48.     /**
  49.      * Initializes this handler by creating the table if it not exists
  50.      */
  51.     protected function initialize() {
  52.         $this->initialized = true;
  53.     }
  54.  
  55.     /**
  56.      * Prepares a record that will be inserted in the audit log.
  57.      *
  58.      * @param array record An array of audit data.
  59.      * @return array A record, ready for insertion in the audit log.
  60.      */
  61.     protected function prepare_content_for_insert(array $record) {
  62.         $timestamp = $record['datetime'];
  63.         $insert_record = array(
  64.             'timestamp' => $timestamp->format('Y-m-d H:i:s'),
  65.             'level' => $record['level'],
  66.             'message' => $record['message'],
  67.             'source' => $record['channel'],
  68.         );
  69.  
  70.         $insert_record['context'] = !empty($record['context']) ? serialize($record['context']) : '';
  71.  
  72.         return $insert_record;
  73.     }
  74.  
  75.     /**
  76.      * Writes the record to the audit table.
  77.      *
  78.      * @param array $record
  79.      * @return void
  80.      */
  81.     protected function write(array $record) {
  82.         if(!$this->initialized) {
  83.             $this->initialize();
  84.         }
  85.  
  86.         $format = array(
  87.             '%s',
  88.             '%d',
  89.             '%s',
  90.             '%s',
  91.             '%s', // possible serialized context.
  92.         );
  93.  
  94.         return $this->wpdb->insert($this->log_table, $this->prepare_content_for_insert($record), $format) !== false;
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement