Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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