cake_log.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: cake__log_8php-source.html 580 2008-07-01 14:45:49Z gwoo $ */
00003 /**
00004  * Logging.
00005  *
00006  * Log messages to text files.
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00011  * Copyright 2005-2008, Cake Software Foundation, Inc.
00012  *                              1785 E. Sahara Avenue, Suite 490-204
00013  *                              Las Vegas, Nevada 89104
00014  *
00015  * Licensed under The MIT License
00016  * Redistributions of files must retain the above copyright notice.
00017  *
00018  * @filesource
00019  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00020  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00021  * @package         cake
00022  * @subpackage      cake.cake.libs
00023  * @since           CakePHP(tm) v 0.2.9
00024  * @version         $Revision: 580 $
00025  * @modifiedby      $LastChangedBy: gwoo $
00026  * @lastmodified    $Date: 2008-07-01 09:45:49 -0500 (Tue, 01 Jul 2008) $
00027  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00028  */
00029 /**
00030  * Included libraries.
00031  *
00032  */
00033     if (!class_exists('File')) {
00034         uses('file');
00035     }
00036 /**
00037  * Set up error level constants to be used within the framework if they are not defined within the
00038  * system.
00039  *
00040  */
00041     if (!defined('LOG_WARNING')) {
00042         define('LOG_WARNING', 3);
00043     }
00044     if (!defined('LOG_NOTICE')) {
00045         define('LOG_NOTICE', 4);
00046     }
00047     if (!defined('LOG_DEBUG')) {
00048         define('LOG_DEBUG', 5);
00049     }
00050     if (!defined('LOG_INFO')) {
00051         define('LOG_INFO', 6);
00052     }
00053 /**
00054  * Logs messages to text files
00055  *
00056  * @package     cake
00057  * @subpackage  cake.cake.libs
00058  */
00059 class CakeLog {
00060 /**
00061  * Writes given message to a log file in the logs directory.
00062  *
00063  * @param string $type Type of log, becomes part of the log's filename
00064  * @param string $msg  Message to log
00065  * @return boolean Success
00066  * @access public
00067  */
00068     function write($type, $msg) {
00069         if (!defined('LOG_ERROR')) {
00070             define('LOG_ERROR', 2);
00071         }
00072         if (!defined('LOG_ERR')) {
00073             define('LOG_ERR', LOG_ERROR);
00074         }
00075         $levels = array(
00076             LOG_WARNING => 'warning',
00077             LOG_NOTICE => 'notice',
00078             LOG_INFO => 'info',
00079             LOG_DEBUG => 'debug',
00080             LOG_ERR => 'error',
00081             LOG_ERROR => 'error'
00082         );
00083 
00084         if (is_int($type) && isset($levels[$type])) {
00085             $type = $levels[$type];
00086         }
00087 
00088         if ($type == 'error' || $type == 'warning') {
00089             $filename = LOGS . 'error.log';
00090         } elseif (in_array($type, $levels)) {
00091             $filename = LOGS . 'debug.log';
00092         } else {
00093             $filename = LOGS . $type . '.log';
00094         }
00095         $output = date('Y-m-d H:i:s') . ' ' . ucfirst($type) . ': ' . $msg . "\n";
00096         $log = new File($filename, true);
00097         if ($log->writable()) {
00098             return $log->append($output);
00099         }
00100     }
00101 }
00102 ?>