Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * @package    moodlecore
  20   * @subpackage backup-logger
  21   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  /**
  26   * Logger implementation that sends messages to database
  27   *
  28   * TODO: Finish phpdocs
  29   */
  30  class database_logger extends base_logger {
  31  
  32      protected $datecol;    // Name of the field where the timestamp will be stored
  33      protected $levelcol;   // Name of the field where the level of the message will be stored
  34      protected $messagecol; // Name of the field where the message will be stored
  35      protected $logtable;   // Table, without prefix where information must be logged
  36      protected $columns;    // Array of columns and values to set in all actions logged
  37  
  38  // Protected API starts here
  39  
  40      public function __construct($level, $datecol = false, $levelcol = false, $messagecol = null, $logtable = null, $columns = null) {
  41          // TODO check $datecol exists
  42          // TODO check $levelcol exists
  43          // TODO check $logtable exists
  44          // TODO check $messagecol exists
  45          // TODO check all $columns exist
  46          $this->datecol    = $datecol;
  47          $this->levelcol   = $levelcol;
  48          $this->messagecol = $messagecol;
  49          $this->logtable   = $logtable;
  50          $this->columns    = $columns;
  51          parent::__construct($level, (bool)$datecol, (bool)$levelcol);
  52      }
  53  
  54      protected function action($message, $level, $options = null) {
  55          $columns = $this->columns;
  56          if ($this->datecol) {
  57              $columns[$this->datecol] = time();
  58          }
  59          if ($this->levelcol) {
  60              $columns[$this->levelcol] = $level;
  61          }
  62          $columns[$this->messagecol] = clean_param($message, PARAM_NOTAGS);
  63          return $this->insert_log_record($this->logtable, $columns);
  64      }
  65  
  66      protected function insert_log_record($table, $columns) {
  67          // TODO: Allow to use an alternate connection (created in constructor)
  68          // based in some CFG->backup_database_logger_newconn = true in order
  69          // to preserve DB logs if the whole backup/restore transaction is
  70          // rollback
  71          global $DB;
  72          return $DB->insert_record($table, $columns, false); // Don't return inserted id
  73      }
  74  }