3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
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
26 * Logger implementation that sends indented messages (depth option) to one file
28 * TODO: Finish phpdocs
30 class file_logger extends base_logger {
32 protected $fullpath; // Full path to OS file where contents will be stored
33 protected $fhandle; // File handle where all write operations happen
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);
39 if (!is_writable(dirname($fullpath))) {
40 throw new base_logger_exception('file_not_writable', $fullpath);
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);
49 parent::__construct($level, $showdate, $showlevel);
52 public function __destruct() {
53 @fclose($this->fhandle); // Blindy close the file handler (no exceptions in destruct)
56 public function __sleep() {
57 @fclose($this->fhandle); // Blindy close the file handler before serialization
58 return array('level', 'showdate', 'showlevel', 'next', 'fullpath');
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);
70 * Close the logger resources (file handle) if still open.
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;
82 // Protected API starts here
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;
91 $content = $prefix . str_repeat(' ', $depth) . htmlentities($message, ENT_QUOTES, 'UTF-8') . '<br/>' . PHP_EOL;
93 if (false === fwrite($this->fhandle, $content)) {
94 throw new base_logger_exception('error_writing_file', $this->fullpath);