1: <?php
2: /* SVN FILE: $Id$ */
3: /**
4: * Logging.
5: *
6: * Log messages to text files.
7: *
8: * PHP versions 4 and 5
9: *
10: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
11: * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
12: *
13: * Licensed under The MIT License
14: * Redistributions of files must retain the above copyright notice.
15: *
16: * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
17: * @link http://cakephp.org CakePHP(tm) Project
18: * @package cake
19: * @subpackage cake.cake.libs
20: * @since CakePHP(tm) v 0.2.9
21: * @version $Revision$
22: * @modifiedby $LastChangedBy$
23: * @lastmodified $Date$
24: * @license http://www.opensource.org/licenses/mit-license.php The MIT License
25: */
26: /**
27: * Included libraries.
28: *
29: */
30: if (!class_exists('File')) {
31: require LIBS . 'file.php';
32: }
33: /**
34: * Set up error level constants to be used within the framework if they are not defined within the
35: * system.
36: *
37: */
38: if (!defined('LOG_WARNING')) {
39: define('LOG_WARNING', 3);
40: }
41: if (!defined('LOG_NOTICE')) {
42: define('LOG_NOTICE', 4);
43: }
44: if (!defined('LOG_DEBUG')) {
45: define('LOG_DEBUG', 5);
46: }
47: if (!defined('LOG_INFO')) {
48: define('LOG_INFO', 6);
49: }
50: /**
51: * Logs messages to text files
52: *
53: * @package cake
54: * @subpackage cake.cake.libs
55: */
56: class CakeLog {
57: /**
58: * Writes given message to a log file in the logs directory.
59: *
60: * @param string $type Type of log, becomes part of the log's filename
61: * @param string $msg Message to log
62: * @return boolean Success
63: * @access public
64: * @static
65: */
66: function write($type, $msg) {
67: if (!defined('LOG_ERROR')) {
68: define('LOG_ERROR', 2);
69: }
70: if (!defined('LOG_ERR')) {
71: define('LOG_ERR', LOG_ERROR);
72: }
73: $levels = array(
74: LOG_WARNING => 'warning',
75: LOG_NOTICE => 'notice',
76: LOG_INFO => 'info',
77: LOG_DEBUG => 'debug',
78: LOG_ERR => 'error',
79: LOG_ERROR => 'error'
80: );
81:
82: if (is_int($type) && isset($levels[$type])) {
83: $type = $levels[$type];
84: }
85:
86: if ($type == 'error' || $type == 'warning') {
87: $filename = LOGS . 'error.log';
88: } elseif (in_array($type, $levels)) {
89: $filename = LOGS . 'debug.log';
90: } else {
91: $filename = LOGS . $type . '.log';
92: }
93: $output = date('Y-m-d H:i:s') . ' ' . ucfirst($type) . ': ' . $msg . "\n";
94: $log = new File($filename, true);
95: if ($log->writable()) {
96: return $log->append($output);
97: }
98: }
99: }
100: ?>