'FileLog'));` * * See the documentation on CakeLog::config() for more detail. * * ### Writing to the log * * You write to the logs using CakeLog::write(). See its documentation for more information. * * @package Cake.Log */ class CakeLog { /** * LogEngineCollection class * * @var LogEngineCollection */ protected static $_Collection; /** * initialize ObjectCollection * * @return void */ protected static function _init() { self::$_Collection = new LogEngineCollection(); } /** * Configure and add a new logging stream to CakeLog * You can use add loggers from app/Log/Engine use app.loggername, or any plugin/Log/Engine using plugin.loggername. * * ### Usage: * * {{{ * CakeLog::config('second_file', array( * 'engine' => 'FileLog', * 'path' => '/var/logs/my_app/' * )); * }}} * * Will configure a FileLog instance to use the specified path. All options that are not `engine` * are passed onto the logging adapter, and handled there. Any class can be configured as a logging * adapter as long as it implements the methods in CakeLogInterface. * * @param string $key The keyname for this logger, used to remove the logger later. * @param array $config Array of configuration information for the logger * @return boolean success of configuration. * @throws CakeLogException */ public static function config($key, $config) { if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $key)) { throw new CakeLogException(__d('cake_dev', 'Invalid key name')); } if (empty($config['engine'])) { throw new CakeLogException(__d('cake_dev', 'Missing logger classname')); } if (empty(self::$_Collection)) { self::_init(); } self::$_Collection->load($key, $config); return true; } /** * Returns the keynames of the currently active streams * * @return array Array of configured log streams. */ public static function configured() { if (empty(self::$_Collection)) { self::_init(); } return self::$_Collection->attached(); } /** * Removes a stream from the active streams. Once a stream has been removed * it will no longer have messages sent to it. * * @param string $streamName Key name of a configured stream to remove. * @return void */ public static function drop($streamName) { if (empty(self::$_Collection)) { self::_init(); } self::$_Collection->unload($streamName); } /** * Checks wether $streamName is enabled * * @param string $streamName to check * @return bool * @throws CakeLogException */ public static function enabled($streamName) { if (empty(self::$_Collection)) { self::_init(); } if (!isset(self::$_Collection->{$streamName})) { throw new CakeLogException(__d('cake_dev', 'Stream %s not found', $streamName)); } return self::$_Collection->enabled($streamName); } /** * Enable stream * * @param string $streamName to enable * @return void * @throws CakeLogException */ public static function enable($streamName) { if (empty(self::$_Collection)) { self::_init(); } if (!isset(self::$_Collection->{$streamName})) { throw new CakeLogException(__d('cake_dev', 'Stream %s not found', $streamName)); } self::$_Collection->enable($streamName); } /** * Disable stream * * @param string $streamName to disable * @return void * @throws CakeLogException */ public static function disable($streamName) { if (empty(self::$_Collection)) { self::_init(); } if (!isset(self::$_Collection->{$streamName})) { throw new CakeLogException(__d('cake_dev', 'Stream %s not found', $streamName)); } self::$_Collection->disable($streamName); } /** * Gets the logging engine from the active streams. * * @see BaseLog * @param string $streamName Key name of a configured stream to get. * @return $mixed instance of BaseLog or false if not found */ public static function stream($streamName) { if (empty(self::$_Collection)) { self::_init(); } if (!empty(self::$_Collection->{$streamName})) { return self::$_Collection->{$streamName}; } return false; } /** * Configures the automatic/default stream a FileLog. * * @return void */ protected static function _autoConfig() { if (empty(self::$_Collection)) { self::_init(); } self::$_Collection->load('error', array( 'engine' => 'FileLog', 'types' => array('error', 'warning'), 'path' => LOGS, )); } /** * Writes the given message and type to all of the configured log adapters. * Configured adapters are passed both the $type and $message variables. $type * is one of the following strings/values. * * ### Types: * * - `LOG_WARNING` => 'warning', * - `LOG_NOTICE` => 'notice', * - `LOG_INFO` => 'info', * - `LOG_DEBUG` => 'debug', * - `LOG_ERR` => 'error', * - `LOG_ERROR` => 'error' * * ### Usage: * * Write a message to the 'warning' log: * * `CakeLog::write('warning', 'Stuff is broken here');` * * @param string $type Type of message being written * @param string $message Message content to log * @return boolean Success */ public static function write($type, $message) { if (empty(self::$_Collection)) { self::_init(); } if (!defined('LOG_ERROR')) { define('LOG_ERROR', 2); } if (!defined('LOG_ERR')) { define('LOG_ERR', LOG_ERROR); } $levels = array( LOG_WARNING => 'warning', LOG_NOTICE => 'notice', LOG_INFO => 'info', LOG_DEBUG => 'debug', LOG_ERR => 'error', LOG_ERROR => 'error' ); if (is_int($type) && isset($levels[$type])) { $type = $levels[$type]; } if (empty(self::$_streams)) { self::_autoConfig(); } foreach (self::$_Collection->enabled() as $streamName) { $logger = self::$_Collection->{$streamName}; $config = $logger->config(); $types = $config['types']; if (empty($types) || in_array($type, $types)) { $logger->write($type, $message); } } return true; } }