/
FileLog.php
212 lines (189 loc) · 6.25 KB
/
FileLog.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @since 1.3.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Log\Engine;
use Cake\Core\Configure;
use Cake\Utility\Text;
/**
* File Storage stream for Logging. Writes logs to different files
* based on the level of log it is.
*/
class FileLog extends BaseLog
{
/**
* Default config for this class
*
* - `levels` string or array, levels the engine is interested in
* - `scopes` string or array, scopes the engine is interested in
* - `file` Log file name
* - `path` The path to save logs on.
* - `size` Used to implement basic log file rotation. If log file size
* reaches specified size the existing file is renamed by appending timestamp
* to filename and new log file is created. Can be integer bytes value or
* human readable string values like '10MB', '100KB' etc.
* - `rotate` Log files are rotated specified times before being removed.
* If value is 0, old versions are removed rather then rotated.
* - `mask` A mask is applied when log files are created. Left empty no chmod
* is made.
*
* @var array
*/
protected $_defaultConfig = [
'path' => null,
'file' => null,
'types' => null,
'levels' => [],
'scopes' => [],
'rotate' => 10,
'size' => 10485760, // 10MB
'mask' => null,
];
/**
* Path to save log files on.
*
* @var string|null
*/
protected $_path;
/**
* The name of the file to save logs into.
*
* @var string|null
*/
protected $_file;
/**
* Max file size, used for log file rotation.
*
* @var int|null
*/
protected $_size;
/**
* Sets protected properties based on config provided
*
* @param array $config Configuration array
*/
public function __construct(array $config = [])
{
parent::__construct($config);
if (!empty($this->_config['path'])) {
$this->_path = $this->_config['path'];
}
if ($this->_path !== null &&
Configure::read('debug') &&
!is_dir($this->_path)
) {
mkdir($this->_path, 0775, true);
}
if (!empty($this->_config['file'])) {
$this->_file = $this->_config['file'];
if (substr($this->_file, -4) !== '.log') {
$this->_file .= '.log';
}
}
if (!empty($this->_config['size'])) {
if (is_numeric($this->_config['size'])) {
$this->_size = (int)$this->_config['size'];
} else {
$this->_size = Text::parseFileSize($this->_config['size']);
}
}
}
/**
* Implements writing to log files.
*
* @param string $level The severity level of the message being written.
* See Cake\Log\Log::$_levels for list of possible levels.
* @param string $message The message you want to log.
* @param array $context Additional information about the logged message
* @return bool success of write.
*/
public function log($level, $message, array $context = [])
{
$message = $this->_format($message, $context);
$output = date('Y-m-d H:i:s') . ' ' . ucfirst($level) . ': ' . $message . "\n";
$filename = $this->_getFilename($level);
if ($this->_size) {
$this->_rotateFile($filename);
}
$pathname = $this->_path . $filename;
$mask = $this->_config['mask'];
if (!$mask) {
return file_put_contents($pathname, $output, FILE_APPEND);
}
$exists = file_exists($pathname);
$result = file_put_contents($pathname, $output, FILE_APPEND);
static $selfError = false;
if (!$selfError && !$exists && !chmod($pathname, (int)$mask)) {
$selfError = true;
trigger_error(vsprintf(
'Could not apply permission mask "%s" on log file "%s"',
[$mask, $pathname]
), E_USER_WARNING);
$selfError = false;
}
return $result;
}
/**
* Get filename
*
* @param string $level The level of log.
* @return string File name
*/
protected function _getFilename($level)
{
$debugTypes = ['notice', 'info', 'debug'];
if ($this->_file) {
$filename = $this->_file;
} elseif ($level === 'error' || $level === 'warning') {
$filename = 'error.log';
} elseif (in_array($level, $debugTypes)) {
$filename = 'debug.log';
} else {
$filename = $level . '.log';
}
return $filename;
}
/**
* Rotate log file if size specified in config is reached.
* Also if `rotate` count is reached oldest file is removed.
*
* @param string $filename Log file name
* @return bool|null True if rotated successfully or false in case of error.
* Null if file doesn't need to be rotated.
*/
protected function _rotateFile($filename)
{
$filePath = $this->_path . $filename;
clearstatcache(true, $filePath);
if (!file_exists($filePath) ||
filesize($filePath) < $this->_size
) {
return null;
}
$rotate = $this->_config['rotate'];
if ($rotate === 0) {
$result = unlink($filePath);
} else {
$result = rename($filePath, $filePath . '.' . time());
}
$files = glob($filePath . '.*');
if ($files) {
$filesToDelete = count($files) - $rotate;
while ($filesToDelete > 0) {
unlink(array_shift($files));
$filesToDelete--;
}
}
return $result;
}
}