Logging

Logging -- Introduction

Overview

Since version 1.5.0 PEAR::Auth provides a facility for retrieving logs of its internal behaviour. This is implemented using PEAR::Log and its log observer components.

Auth provides two levels of log messages which map to the Log priority levels PEAR_LOG_INFO and PEAR_LOG_DEBUG.

PEAR_LOG_INFO messages provide basic information about Auth's decisions, for example user authentication successful/failed, rendering login screen.

PEAR_LOG_DEBUG messages provide detailed information about what is happening within the internals of Auth, for example which functions are called, logic tracking for the Authentication method, what SQL queries are being run against the database backends.

Example

Пример 30-1. Typical usage example for accessing the logs from PEAR::Auth

require_once "Auth.php";
require_once 'Log.php';
require_once 'Log/observer.php';

// Callback function to display login form
function loginFunction($username = null, $status = null, &$auth = null)
{
	/*
	 * Change the HTML output so that it fits to your
	 * application.
	 */
	echo "<form method=\"post\" action=\"".$_SERVER['PHP_SELF']."\">";
	echo "Username: <input type=\"text\" name=\"username\"><br/>";
	echo "Password: <input type=\"password\" name=\"password\"><br/>";
	echo "<input type=\"submit\">";
	echo "</form>";
}
 class Auth_Log_Observer extends Log_observer {

	var $messages = array();

	function notify($event) {

		$this->messages[] = $event;

	}

}

$options = array(
		'enableLogging' => true,
		'cryptType' => 'md5',
		'users' => array(
			'guest' => md5('password'),
			),
		);
$a = new Auth("Array", $options, "loginFunction");

$infoObserver = new Auth_Log_Observer(PEAR_LOG_INFO);

$a->attachLogObserver($infoObserver);

$debugObserver = new Auth_Log_Observer(PEAR_LOG_DEBUG);

$a->attachLogObserver($debugObserver);

$a->start();

if ($a->checkAuth()) {
	/*
	 * The output of your site goes here.
	 */
	print "Authentication Successful.<br/>";
}

print '<h3>Logging Output:</h3>'
	.'<b>PEAR_LOG_INFO level messages:</b><br/>';

foreach ($infoObserver->messages as $event) {
	print $event['priority'].': '.$event['message'].'<br/>';
}

print '<br/>'
	.'<b>PEAR_LOG_DEBUG level messages:</b><br/>';

foreach ($debugObserver->messages as $event) {
	print $event['priority'].': '.$event['message'].'<br/>';
}

print '<br/>';

To enable logging pass the option "enableLogging" with the value TRUE in the options array in the second parameter to the Auth constructor.

To retrieve the log messages from Auth create a new subclass of Log_Observer that implements a notify() function to perform whatever actions you want on the log messages.

Once defined pass an instance of your new Log_Observer instance to Auth::attachLogObserver().

To limit the types of messages you receive in the Log_Observer pass either PEAR_LOG_INFO or PEAR_LOG_DEBUG as the first parameter to the Log_Observer. The default is PEAR_LOG_INFO. For more information on this filtering see the Log Documentation.

$observer = new My_Log_Observer(PEAR_LOG_DEBUG);

Заметка

This container has only been available since version 1.5.0.

    Поддержать сайт на родительском проекте КГБ