1: <?php
2: /**
3: * File Storage stream for Logging
4: *
5: * PHP 5
6: *
7: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8: * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
9: *
10: * Licensed under The MIT License
11: * For full copyright and license information, please see the LICENSE.txt
12: * Redistributions of files must retain the above copyright notice.
13: *
14: * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
15: * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
16: * @package Cake.Log.Engine
17: * @since CakePHP(tm) v 1.3
18: * @license http://www.opensource.org/licenses/mit-license.php MIT License
19: */
20:
21: App::uses('BaseLog', 'Log/Engine');
22: App::uses('Hash', 'Utility');
23:
24: /**
25: * File Storage stream for Logging. Writes logs to different files
26: * based on the type of log it is.
27: *
28: * @package Cake.Log.Engine
29: */
30: class FileLog extends BaseLog {
31:
32: /**
33: * Path to save log files on.
34: *
35: * @var string
36: */
37: protected $_path = null;
38:
39: /**
40: * Constructs a new File Logger.
41: *
42: * Config
43: *
44: * - `types` string or array, levels the engine is interested in
45: * - `scopes` string or array, scopes the engine is interested in
46: * - `file` log file name
47: * - `path` the path to save logs on.
48: *
49: * @param array $options Options for the FileLog, see above.
50: */
51: public function __construct($config = array()) {
52: parent::__construct($config);
53: $config = Hash::merge(array(
54: 'path' => LOGS,
55: 'file' => null,
56: 'types' => null,
57: 'scopes' => array(),
58: ), $this->_config);
59: $config = $this->config($config);
60: $this->_path = $config['path'];
61: $this->_file = $config['file'];
62: if (!empty($this->_file) && substr($this->_file, -4) !== '.log') {
63: $this->_file .= '.log';
64: }
65: }
66:
67: /**
68: * Implements writing to log files.
69: *
70: * @param string $type The type of log you are making.
71: * @param string $message The message you want to log.
72: * @return boolean success of write.
73: */
74: public function write($type, $message) {
75: $debugTypes = array('notice', 'info', 'debug');
76:
77: if (!empty($this->_file)) {
78: $filename = $this->_path . $this->_file;
79: } elseif ($type === 'error' || $type === 'warning') {
80: $filename = $this->_path . 'error.log';
81: } elseif (in_array($type, $debugTypes)) {
82: $filename = $this->_path . 'debug.log';
83: } else {
84: $filename = $this->_path . $type . '.log';
85: }
86: $output = date('Y-m-d H:i:s') . ' ' . ucfirst($type) . ': ' . $message . "\n";
87: return file_put_contents($filename, $output, FILE_APPEND);
88: }
89:
90: }
91: