1: <?php
2: /**
3: * Logging.
4: *
5: * Log messages to text files.
6: *
7: * PHP 5
8: *
9: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
10: * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
11: *
12: * Licensed under The MIT License
13: * Redistributions of files must retain the above copyright notice.
14: *
15: * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
16: * @link http://cakephp.org CakePHP(tm) Project
17: * @package Cake.Log
18: * @since CakePHP(tm) v 0.2.9
19: * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
20: */
21:
22: /**
23: * Set up error level constants to be used within the framework if they are not defined within the
24: * system.
25: *
26: */
27: if (!defined('LOG_WARNING')) {
28: define('LOG_WARNING', 3);
29: }
30: if (!defined('LOG_NOTICE')) {
31: define('LOG_NOTICE', 4);
32: }
33: if (!defined('LOG_DEBUG')) {
34: define('LOG_DEBUG', 5);
35: }
36: if (!defined('LOG_INFO')) {
37: define('LOG_INFO', 6);
38: }
39:
40: /**
41: * Logs messages to configured Log adapters. One or more adapters can be configured
42: * using CakeLogs's methods. If you don't configure any adapters, and write to the logs
43: * a default FileLog will be autoconfigured for you.
44: *
45: * ### Configuring Log adapters
46: *
47: * You can configure log adapters in your applications `bootstrap.php` file. A sample configuration
48: * would look like:
49: *
50: * `CakeLog::config('my_log', array('engine' => 'FileLog'));`
51: *
52: * See the documentation on CakeLog::config() for more detail.
53: *
54: * ### Writing to the log
55: *
56: * You write to the logs using CakeLog::write(). See its documentation for more information.
57: *
58: * @package Cake.Log
59: */
60: class CakeLog {
61:
62: /**
63: * An array of connected streams.
64: * Each stream represents a callable that will be called when write() is called.
65: *
66: * @var array
67: */
68: protected static $_streams = array();
69:
70: /**
71: * Configure and add a new logging stream to CakeLog
72: * You can use add loggers from app/Log/Engine use app.loggername, or any plugin/Log/Engine using plugin.loggername.
73: *
74: * ### Usage:
75: *
76: * {{{
77: * CakeLog::config('second_file', array(
78: * 'engine' => 'FileLog',
79: * 'path' => '/var/logs/my_app/'
80: * ));
81: * }}}
82: *
83: * Will configure a FileLog instance to use the specified path. All options that are not `engine`
84: * are passed onto the logging adapter, and handled there. Any class can be configured as a logging
85: * adapter as long as it implements the methods in CakeLogInterface.
86: *
87: * @param string $key The keyname for this logger, used to remove the logger later.
88: * @param array $config Array of configuration information for the logger
89: * @return boolean success of configuration.
90: * @throws CakeLogException
91: */
92: public static function config($key, $config) {
93: if (empty($config['engine'])) {
94: throw new CakeLogException(__d('cake_dev', 'Missing logger classname'));
95: }
96: $loggerName = $config['engine'];
97: unset($config['engine']);
98: $className = self::_getLogger($loggerName);
99: $logger = new $className($config);
100: if (!$logger instanceof CakeLogInterface) {
101: throw new CakeLogException(sprintf(
102: __d('cake_dev', 'logger class %s does not implement a write method.'), $loggerName
103: ));
104: }
105: self::$_streams[$key] = $logger;
106: return true;
107: }
108:
109: /**
110: * Attempts to import a logger class from the various paths it could be on.
111: * Checks that the logger class implements a write method as well.
112: *
113: * @param string $loggerName the plugin.className of the logger class you want to build.
114: * @return mixed boolean false on any failures, string of classname to use if search was successful.
115: * @throws CakeLogException
116: */
117: protected static function _getLogger($loggerName) {
118: list($plugin, $loggerName) = pluginSplit($loggerName, true);
119:
120: App::uses($loggerName, $plugin . 'Log/Engine');
121: if (!class_exists($loggerName)) {
122: throw new CakeLogException(__d('cake_dev', 'Could not load class %s', $loggerName));
123: }
124: return $loggerName;
125: }
126:
127: /**
128: * Returns the keynames of the currently active streams
129: *
130: * @return array Array of configured log streams.
131: */
132: public static function configured() {
133: return array_keys(self::$_streams);
134: }
135:
136: /**
137: * Removes a stream from the active streams. Once a stream has been removed
138: * it will no longer have messages sent to it.
139: *
140: * @param string $streamName Key name of a configured stream to remove.
141: * @return void
142: */
143: public static function drop($streamName) {
144: unset(self::$_streams[$streamName]);
145: }
146:
147: /**
148: * Configures the automatic/default stream a FileLog.
149: *
150: * @return void
151: */
152: protected static function _autoConfig() {
153: self::_getLogger('FileLog');
154: self::$_streams['default'] = new FileLog(array('path' => LOGS));
155: }
156:
157: /**
158: * Writes the given message and type to all of the configured log adapters.
159: * Configured adapters are passed both the $type and $message variables. $type
160: * is one of the following strings/values.
161: *
162: * ### Types:
163: *
164: * - `LOG_WARNING` => 'warning',
165: * - `LOG_NOTICE` => 'notice',
166: * - `LOG_INFO` => 'info',
167: * - `LOG_DEBUG` => 'debug',
168: * - `LOG_ERR` => 'error',
169: * - `LOG_ERROR` => 'error'
170: *
171: * ### Usage:
172: *
173: * Write a message to the 'warning' log:
174: *
175: * `CakeLog::write('warning', 'Stuff is broken here');`
176: *
177: * @param string $type Type of message being written
178: * @param string $message Message content to log
179: * @return boolean Success
180: */
181: public static function write($type, $message) {
182: if (!defined('LOG_ERROR')) {
183: define('LOG_ERROR', 2);
184: }
185: if (!defined('LOG_ERR')) {
186: define('LOG_ERR', LOG_ERROR);
187: }
188: $levels = array(
189: LOG_WARNING => 'warning',
190: LOG_NOTICE => 'notice',
191: LOG_INFO => 'info',
192: LOG_DEBUG => 'debug',
193: LOG_ERR => 'error',
194: LOG_ERROR => 'error'
195: );
196:
197: if (is_int($type) && isset($levels[$type])) {
198: $type = $levels[$type];
199: }
200: if (empty(self::$_streams)) {
201: self::_autoConfig();
202: }
203: foreach (self::$_streams as $logger) {
204: $logger->write($type, $message);
205: }
206: return true;
207: }
208:
209: }
210: