Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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 indented messages (depth option) to one file
  27   *
  28   * TODO: Finish phpdocs
  29   */
  30  class file_logger extends base_logger {
  31  
  32      protected $fullpath; // Full path to OS file where contents will be stored
  33      protected $fhandle;  // File handle where all write operations happen
  34  
  35      public function __construct($level, $showdate = false, $showlevel = false, $fullpath = null) {
  36          if (empty($fullpath)) {
  37              throw new base_logger_exception('missing_fullpath_parameter', $fullpath);
  38          }
  39          if (!is_writable(dirname($fullpath))) {
  40              throw new base_logger_exception('file_not_writable', $fullpath);
  41          }
  42          // Open the OS file for writing (append)
  43          $this->fullpath = $fullpath;
  44          if ($level > backup::LOG_NONE) { // Only create the file if we are going to log something
  45              if (! $this->fhandle = fopen($this->fullpath, 'a')) {
  46                  throw new base_logger_exception('error_opening_file', $fullpath);
  47              }
  48          }
  49          parent::__construct($level, $showdate, $showlevel);
  50      }
  51  
  52      public function __destruct() {
  53          @fclose($this->fhandle); // Blindy close the file handler (no exceptions in destruct)
  54      }
  55  
  56      public function __sleep() {
  57          @fclose($this->fhandle); // Blindy close the file handler before serialization
  58          return array('level', 'showdate', 'showlevel', 'next', 'fullpath');
  59      }
  60  
  61      public function __wakeup() {
  62          if ($this->level > backup::LOG_NONE) { // Only create the file if we are going to log something
  63              if (! $this->fhandle = fopen($this->fullpath, 'a')) {
  64                  throw new base_logger_exception('error_opening_file', $this->fullpath);
  65              }
  66          }
  67      }
  68  
  69      /**
  70       * Close the logger resources (file handle) if still open.
  71       *
  72       * @since Moodle 3.1
  73       */
  74      public function close() {
  75          // Close the file handle if hasn't been closed already.
  76          if (is_resource($this->fhandle)) {
  77              fclose($this->fhandle);
  78              $this->fhandle = null;
  79          }
  80      }
  81  
  82  // Protected API starts here
  83  
  84      protected function action($message, $level, $options = null) {
  85          $prefix = $this->get_prefix($level, $options);
  86          $depth = isset($options['depth']) ? $options['depth'] : 0;
  87          // Depending of the type (extension of the file), format differently
  88          if (substr($this->fullpath, -5) !== '.html') {
  89              $content = $prefix . str_repeat('  ', $depth) . $message . PHP_EOL;
  90          } else {
  91              $content = $prefix . str_repeat('&nbsp;&nbsp;', $depth) . htmlentities($message, ENT_QUOTES, 'UTF-8') . '<br/>' . PHP_EOL;
  92          }
  93          if (false === fwrite($this->fhandle, $content)) {
  94              throw new base_logger_exception('error_writing_file', $this->fullpath);
  95          }
  96          return true;
  97      }
  98  }