Building an Error Logger with the Chain of Responsibility Pattern in PHP 5 - Logging specific errors at a lower level
(Page 3 of 4 )
As was explained in the previous section, to construct a new error logger that will be focused only on registering specific events, it's necessary to derive another class from the highly generic "ErrorLogger."
As you may guess, this new class will be responsible for handling all the errors occurring only inside its scope. In addition, in accordance with the logic implemented by the corresponding chain of responsibility, the class will delegate program control to its nearest parent, in this case represented by the "ErrorLogger" class, if a particular error can't be handled properly.
More specifically, since I want this new class to be focused on registering all the errors that happen when validating an email address, I called it "MailErrorLogger." Its definition is as follows:
// define 'MailErrorLogger' class
class MailErrorLogger extends ErrorLogger{
private $errorLogger;
private $parentErrorLogger;
public function __construct(ErrorLogger $parentErrorLogger){
$this->errorLogger=NULL;
$this->parentErrorLogger=$parentErrorLogger;
}
// get error logger
public function getErrorLogger(){
if($this->errorLogger==NULL){
// call parent object in chain of responsibility
return $this->parentErrorLogger->getErrorLogger();
}
else{
return $this->errorLogger;
}
}
// set error logger
public function setErrorLogger($errorLogger){
if($errorLogger!='Email Error Logger'){
throw new Exception('Invalid error logger');
}
$this->errorLogger=$errorLogger;
}
// log error by email to system administrator
public function logError(){
mail('sysadmin@domain.com','Message from: '.$this-
>getErrorLogger(),'An error occurred when validating email
address!');
}
}As shown above, the "MailErrorLogger" class has been provided with a basic ability, handy for logging all the eventual errors raised when checking an email address. In particular, the "logError()" method performs the error logging process by notifying the system administrator when this type of failure occurs, something clearly demonstrated by its corresponding definition.
Also, it should be noticed how the "getErrorLogger()" method implements, in an efficient way, the corresponding chain of responsibility: if there's a situation when an appropriate error logger can't be retrieved, this task is simply transferred to its parent, that is the pertinent "ErrorLogger" class. Now do you understand how the chain of responsibility really works here? I'm sure you do!
The remaining class methods speak for themselves, therefore I'll spend no time explaining how they work. Instead, once the previous "MailErrorLogger" class has been correctly defined, I'd like to show you a concrete example where this error logging class can be used with fairly good results.
That being said, over the next few lines I'll be demonstrating how the "MailErrorLogger" class can be utilized in conjunction with an email checking system to log errors when a specified email address fails to pass the verification process.
Want to find out how this will be done? Go ahead and read the following section.
Next: Logging email-related errors >>
More PHP Articles
More By Alejandro Gervasio