Extending log functions

Hi,

I need a new column "type" in my log table to distinguish between "right-logs" and "normal-logs". "right-logs" should never be deleted by any cleanjobs.

This is what I’ve done so far. New is the parameter “type” in the log function and that I call my new Class Logger instead of CLogger.


class Yii extends YiiBase

{

	private static $_logger;


	/**

	 * Logs a message.

	 * Messages logged by this method may be retrieved via {@link CLogger::getLogs}

	 * and may be recorded in different media, such as file, email, database, using

	 * {@link CLogRouter}.

	 *

	 * @param string  $msg      message to be logged

	 * @param string  $level    level of the message (e.g. 'trace', 'warning', 'error'). It is case-insensitive.

	 * @param string  $category category of the message (e.g. 'system.web'). It is case-insensitive.

	 * @param integer $type     type of the message (e.g. 'permissions'). It is case-insensitive.

	 */

	public static function log($msg, $level = Logger::LEVEL_INFO, $category = 'application', $type = null)

	{

		if (self::$_logger === null) {

			self::$_logger = new Logger;

		}

		if (YII_DEBUG && YII_TRACE_LEVEL > 0 && $level !== Logger::LEVEL_PROFILE) {

			$traces = debug_backtrace();

			$count  = 0;

			foreach ($traces as $trace) {

				if (isset($trace['file'], $trace['line']) && strpos($trace['file'], YII_PATH) !== 0) {

					$msg .= "\nin " . $trace['file'] . ' (' . $trace['line'] . ')';

					if (++$count >= YII_TRACE_LEVEL) {

						break;

					}

				}

			}

		}

		self::$_logger->log($msg, $level, $category, $type);

	}


	/**

	 * @return Logger message logger

	 */

	public static function getLogger()

	{

		if (self::$_logger !== null) {

			return self::$_logger;

		} else {

			return self::$_logger = new Logger;

		}

	}

}


class Logger extends CLogger

{

	const LEVEL_CRITICAL = 'critical';


	/**

	 * @var integer how many messages should be logged before they are flushed to destinations.

	 * Defaults to 10,000, meaning for every 10,000 messages, the {@link flush} method will be

	 * automatically invoked once. If this is 0, it means messages will never be flushed automatically.

	 * @since 1.1.0

	 */

	public $autoFlush = 10000;

	/**

	 * @var boolean this property will be passed as the parameter to {@link flush()} when it is

	 * called in {@link log()} due to the limit of {@link autoFlush} being reached.

	 * By default, this property is false, meaning the filtered messages are still kept in the memory

	 * by each log route after calling {@link flush()}. If this is true, the filtered messages

	 * will be written to the actual medium each time {@link flush()} is called within {@link log()}.

	 * @since 1.1.8

	 */

	public $autoDump = false;

	/**

	 * @var array log messages

	 */

	private $_logs = [];

	/**

	 * @var integer number of log messages

	 */

	private $_logCount = 0;

	/**

	 * @var array log levels for filtering (used when filtering)

	 */

	private $_levels;

	/**

	 * @var array log categories for filtering (used when filtering)

	 */

	private $_categories;

	/**

	 * @var array log categories for excluding from filtering (used when filtering)

	 */

	private $_except=array();

	/**

	 * @var boolean if we are processing the log or still accepting new log messages

	 * @since 1.1.9

	 */

	private $_processing = false;


	/**

	 * Logs a message.

	 * Messages logged by this method may be retrieved back via {@link getLogs}.

	 *

	 * @param string  $message  message to be logged

	 * @param string  $level    level of the message (e.g. 'Trace', 'Warning', 'Error'). It is case-insensitive.

	 * @param string  $category category of the message (e.g. 'system.web'). It is case-insensitive.

	 * @param integer $type     type of the message (e.g. 'permissions'). It is case-insensitive.

	 *

	 * @see getLogs

	 */

	public function log($message, $level = 'info', $category = 'application', $type = null)

	{

		$this->_logs[] = [$message, $level, $category, microtime(true), $type];

		$this->_logCount++;

		if ($this->autoFlush > 0 && $this->_logCount >= $this->autoFlush && !$this->_processing) {

			$this->_processing = true;

			$this->flush($this->autoDump);

			$this->_processing = false;

		}

	}


	/**

	 * Retrieves log messages.

	 *

	 * Messages may be filtered by log levels and/or categories.

	 * A level filter is specified by a list of levels separated by comma or space

	 * (e.g. 'trace, error'). A category filter is similar to level filter

	 * (e.g. 'system, system.web'). A difference is that in category filter

	 * you can use pattern like 'system.*' to indicate all categories starting

	 * with 'system'.

	 *

	 * If you do not specify level filter, it will bring back logs at all levels.

	 * The same applies to category filter.

	 *

	 * Level filter and category filter are combinational, i.e., only messages

	 * satisfying both filter conditions will be returned.

	 *

	 * @param string       $levels     level filter

	 * @param array|string $categories category filter

	 * @param array|string $except     list of log categories to ignore

	 *

	 * @return array list of messages. Each array element represents one message

	 * with the following structure:

	 * array(

	 *   [0] => message (string)

	 *   [1] => level (string)

	 *   [2] => category (string)

	 *   [3] => timestamp (float, obtained by microtime(true));

	 */

	public function getLogs($levels = '', $categories = [], $except = [])

	{

		$this->_levels = preg_split('/[\s,]+/', strtolower($levels), -1, PREG_SPLIT_NO_EMPTY);


		if (is_string($categories)) {

			$this->_categories = preg_split('/[\s,]+/', strtolower($categories), -1, PREG_SPLIT_NO_EMPTY);

		} else {

			$this->_categories = array_filter(array_map('strtolower', $categories));

		}


		if (is_string($except)) {

			$this->_except = preg_split('/[\s,]+/', strtolower($except), -1, PREG_SPLIT_NO_EMPTY);

		} else {

			$this->_except = array_filter(array_map('strtolower', $except));

		}


		$ret = $this->_logs;


		if (!empty($levels)) {

			$ret = array_values(array_filter($ret, [$this, 'filterByLevel']));

		}


		if (!empty($this->_categories) || !empty($this->_except)) {

			$ret = array_values(array_filter($ret, [$this, 'filterByCategory']));

		}


		return $ret;

	}


	/**

	 * Filter function used by {@link getLogs}

	 * @param array $value element to be filtered

	 * @return boolean true if valid log, false if not.

	 */

	private function filterByLevel($value)

	{

		return in_array(strtolower($value[1]),$this->_levels);

	}

} 

The problem that I have right now is that it only logs when I change some rights. No other action logs anything.

Does anyone have any suggestions?

Thanks in advance.