Location: PHPKode > projects > Modularized Information Environment > mie/includes/log.inc
<?php
/**
 * @file includes/log.inc
 * @brief Logging Library
 * @author Kenneth Smith <hide@address.com>
 *
 * Modularized Information Environment (MIE)
 * Copyright (C) 2005-2006 by Kenneth Smith. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA.
 */

defined('VALIE_MIE') || die(_('Direct access not allowed'));
defined('LOG_INC') && exit;
define('LOG_INC', true);

/**
 * @brief Logging Manager
 *
 * This class manages the system logging facility.
 */
class LOG {

	/// Construct Object
	function __construct() {
		$this->old_handler = set_error_handler(array($this, 'handler'));
		//error_reporting(0);
	}

	function insert($_type, $_message, $_severity = 10, $_data = '') {
		global $db, $session;
		$remote = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '???';
		$sql = $db->rewrite('INSERT INTO t{logs} (k{type}, k{user_id}, k{host}, k{stamp}, k{message}, k{severity}, k{data}) VALUES (%c, %i, %c, %s, %c, %i, %c)', $_type, $session->user_id, $remote, time(), $_message, $_severity, $_data);
		$db->query($sql);
	}

	/// PHP Handler Callback
	function handler($_severity, $_message, $_file, $_line, $_context) {
		if($_severity <= mie_conf('error_log', 8)) {
			$data['line'] = $_line;
			$data['file'] = $_file;
			if(mie_conf('error_log_context', 0)) {
				$data['context'] = $_context;
			}
			$data = serialize($data);
			$this->insert('php', $_message, $_severity, $data);
		}
		if($_severity <= mie_conf('error_show', 8)) {
			$err = "<br />$_message - <b>" . _('File') . ":</b> $_file - <b>" . _('Line') . ":</b> $_line<br />";
			if(mie_conf('error_show_context', 0)) {
				$err .= "<pre>\n<b>" . _('Context') . ":</b> \n" . print_r($_context, true) . '</pre>';
			}
			mie_warning('PHP Error', $err);
		}
		if($_severity <= mie_conf('error_die', 1)) {
			die(_('Fatal error'));
		}
	}

	function clean() {
		global $db;
		$sql = $db->rewrite("DELETE FROM t{logs} WHERE k{stamp} < %s", time() - mie_conf('log_maxlife', 7200));
		$db->query($sql);
	}

	/**
	 * @brief All Sessions
	 * Return table of all sessions.
	 */
	function all() {
		global $db;
		$sql = $db->rewrite("SELECT k{id}, k{type}, k{user_id}, k{host}, k{stamp}, k{message}, k{severity}  FROM t{logs} ORDER BY k{stamp} DESC LIMIT 0, 100");
		return (array)$db->table($sql);
	}
}

?>
Return current item: Modularized Information Environment