Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
73.68% |
28 / 38 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
| common_log_SingleFileAppender | |
73.68% |
28 / 38 |
|
25.00% |
1 / 4 |
27.29 | |
0.00% |
0 / 1 |
| init | |
63.64% |
7 / 11 |
|
0.00% |
0 / 1 |
11.08 | |||
| initFile | |
28.57% |
2 / 7 |
|
0.00% |
0 / 1 |
14.11 | |||
| doLog | |
94.44% |
17 / 18 |
|
0.00% |
0 / 1 |
4.00 | |||
| __destruct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
| 19 | * (under the project TAO & TAO2); |
| 20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 21 | * (under the project TAO-TRANSFER); |
| 22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 24 | * 2013 (update and modification) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * Basic Appender that writes into a single file |
| 30 | * If the file exceeds maxFileSize the part of file is truncated. |
| 31 | * Size of part for truncate defines in reduceRatio property. |
| 32 | * If ratio == 0 file will empty when size reaches max value. |
| 33 | * When ratio >= 1 will be used default value equal 0.5. |
| 34 | * |
| 35 | * @access public |
| 36 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 37 | * @package generis |
| 38 | */ |
| 39 | class common_log_SingleFileAppender extends common_log_BaseAppender |
| 40 | { |
| 41 | /** |
| 42 | * Name of file with log entries |
| 43 | * |
| 44 | * @access protected |
| 45 | * @var string |
| 46 | */ |
| 47 | protected $filename = ''; |
| 48 | |
| 49 | /** |
| 50 | * Format for each log line |
| 51 | * |
| 52 | * %d datestring |
| 53 | * %m description(message) |
| 54 | * %s severity |
| 55 | * %b backtrace |
| 56 | * %r request |
| 57 | * %f file from which the log was called |
| 58 | * %l line from which the log was called |
| 59 | * %t timestamp |
| 60 | * %u user |
| 61 | * %g tags |
| 62 | * |
| 63 | * @access protected |
| 64 | * @var string |
| 65 | */ |
| 66 | protected $format = '%d [%s] \'%m\' %f %l'; |
| 67 | |
| 68 | /** |
| 69 | * Prefix for each log line |
| 70 | * |
| 71 | * @var string |
| 72 | */ |
| 73 | protected $prefix = ''; |
| 74 | |
| 75 | /** |
| 76 | * Maximum size of the logfile in bytes |
| 77 | * |
| 78 | * @access protected |
| 79 | * @var int |
| 80 | */ |
| 81 | protected $maxFileSize = 1048576; |
| 82 | |
| 83 | /** |
| 84 | * Ratio value that using for reducing logfile when size of it reach max value |
| 85 | * |
| 86 | * @var float |
| 87 | */ |
| 88 | protected $reduceRatio = 0.5; |
| 89 | |
| 90 | /** |
| 91 | * File descriptor for R/W operations |
| 92 | * |
| 93 | * @access protected |
| 94 | * @var resource |
| 95 | */ |
| 96 | protected $filehandle = null; |
| 97 | |
| 98 | /** |
| 99 | * |
| 100 | * @access public |
| 101 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 102 | * @param array $configuration |
| 103 | * @return boolean |
| 104 | */ |
| 105 | public function init($configuration) |
| 106 | { |
| 107 | if (isset($configuration['file'])) { |
| 108 | $this->filename = $configuration['file']; |
| 109 | } |
| 110 | |
| 111 | if (isset($configuration['format'])) { |
| 112 | $this->format = $configuration['format']; |
| 113 | } |
| 114 | |
| 115 | if (isset($configuration['prefix'])) { |
| 116 | $this->prefix = $configuration['prefix']; |
| 117 | } |
| 118 | |
| 119 | if (isset($configuration['max_file_size'])) { |
| 120 | $this->maxFileSize = $configuration['max_file_size']; |
| 121 | } |
| 122 | |
| 123 | if (isset($configuration['rotation-ratio']) && abs($configuration['rotation-ratio']) < 1) { |
| 124 | $this->reduceRatio = 1 - abs($configuration['rotation-ratio']); |
| 125 | } |
| 126 | |
| 127 | return ! empty($this->filename) ? parent::init($configuration) : false; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Initialises the logfile, and checks whenever the file require pruning |
| 132 | * |
| 133 | * @access protected |
| 134 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 135 | * @return mixed |
| 136 | */ |
| 137 | protected function initFile() |
| 138 | { |
| 139 | if ($this->maxFileSize > 0 && file_exists($this->filename) && filesize($this->filename) >= $this->maxFileSize) { |
| 140 | // need to reduce the file size |
| 141 | $file = file($this->filename); |
| 142 | $file = array_splice($file, ceil(count($file) * $this->reduceRatio)); |
| 143 | $this->filehandle = @fopen($this->filename, 'w'); |
| 144 | foreach ($file as $line) { |
| 145 | @fwrite($this->filehandle, $line); |
| 146 | } |
| 147 | } else { |
| 148 | $this->filehandle = @fopen($this->filename, 'a'); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Prepares and saves log entries to file |
| 154 | * |
| 155 | * @access public |
| 156 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 157 | * @param common_log_Item $item |
| 158 | * @return mixed |
| 159 | */ |
| 160 | public function doLog(common_log_Item $item) |
| 161 | { |
| 162 | if (is_null($this->filehandle)) { |
| 163 | $this->initFile(); |
| 164 | } |
| 165 | if ($this->filehandle !== false) { |
| 166 | $map = [ |
| 167 | '%d' => gmdate('Y-m-d H:i:s', $item->getDateTime()), |
| 168 | '%m' => $item->getDescription(), |
| 169 | '%p' => $this->prefix, |
| 170 | '%s' => $item->getSeverityDescriptionString(), |
| 171 | '%t' => $item->getDateTime(), |
| 172 | '%r' => $item->getRequest(), |
| 173 | '%f' => $item->getCallerFile(), |
| 174 | '%g' => json_encode($item->getTags()), |
| 175 | '%l' => $item->getCallerLine() |
| 176 | ]; |
| 177 | if (strpos($this->format, '%b')) { |
| 178 | $map['%b'] = 'Backtrace not yet supported'; |
| 179 | } |
| 180 | $str = strtr($this->format, $map) . PHP_EOL; |
| 181 | @fwrite($this->filehandle, $str); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Closes file descriptor when logger object was destroyed |
| 187 | * |
| 188 | * @access public |
| 189 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 190 | * @return mixed |
| 191 | */ |
| 192 | public function __destruct() |
| 193 | { |
| 194 | if (! is_null($this->filehandle) && $this->filehandle !== false) { |
| 195 | @fclose($this->filehandle); |
| 196 | } |
| 197 | } |
| 198 | } |