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