Wednesday 6 January 2016

Zend Framework 2 Zend Email and Zend _Log

Zend Framework 2 Zend Email and Zend _Log

Question: What is Zend Log?
Zend_Log is a component used for logging in output, database, file, firebug and email etc.

Question: What are different objects in Zend Log?
Following are object which are used zend_log.
  1. A Logger: It is instance of Zend\Log\Logger is the main object for Zend Log.
  2. A Writer: It is inherits from Zend\Log\Writer\AbstractWriter and used to save the log.
  3. A Filter: It is implements Zend\Log\Filter\FilterInterface and used to NOT save the log.
  4. A Formatter: It is implements Zend\Log\Formatter\FormatterInterface and used for Formating when gives to writer.

Question: What are different priorites in Zend_log?
EMERG   = 0;  // Emergency: system is unusable
ALERT   = 1;  // Alert: action must be taken immediately
CRIT    = 2;  // Critical: critical conditions
ERR     = 3;  // Error: error conditions
WARN    = 4;  // Warning: warning conditions
NOTICE  = 5;  // Notice: normal but significant condition
INFO    = 6;  // Informational: informational messages
DEBUG   = 7;  // Debug: debug messages

Question: How to log the data? Give an simple example?
$logger = new \Zend\Log\Logger;
$writer = new \Zend\Log\Writer\Stream('php://output');
$logger->log(\Zend\Log\Logger::INFO, 'Informational message');

Question: How to distroy the log?
$logger = null;

Question: Can use multiple writer?Give an Example?
Yes, We can do.
See Example Below:
$logger = new \Zend\Log\Logger;
$writer1 = new \Zend\Log\Writer\Stream('php://output');
$writer2 = new \Zend\Log\Writer\Stream('/path/to/second/logfile');
$logger->log(\Zend\Log\Logger::INFO, 'Informational message');

Question: Can use we use Filter?Give an Example?
$logger = new \Zend\Log\Logger;
$writer = new \Zend\Log\Writer\Stream('php://output');

$filter = new Zend\Log\Filter\Priority(Logger::CRIT); /** Write only for critical **/

$logger->log(\Zend\Log\Logger::INFO, 'Informational message');

Question: What are different filter available in Zend_log?
  1. Priority
  2. Regex
  3. Timestamp
  4. SuppressFilter
  5. Validator

Question: What are different Formater available in Zend_log?
  1. Simple Formater
  2. Formatting to XML
  3. Formatting to FirePhp

Question: What is Zend_Email?
Zend_Email is component which is used to send email.

Question: Give an Example of Zend_Email?
use \Zend\Mail;
$mail = new Mail\Message();
$mail->setBody('This is email content.');
$mail->setFrom('', 'Email From');
$mail->addTo('', 'Receiver');
$mail->setSubject('This is email subject');

$transport = new \Mail\Transport\Sendmail();

Question: What is Zend\Mail\Transport?
Zend\Mail\Transport is interface which send an email in actual. This interface has only one method i.e send().

Question: How can we send an email using SMTP Transport?
$transport = new SmtpTransport();
$options   = new SmtpOptions(array(
    'name'              => 'localhost.localdomain',
    'host'              => '',
    'connection_class'  => 'login',
    'connection_config' => array(
        'username' => 'username',
        'password' => 'password',

$transport->send( $mail);

Question: Can we use transport to store email in File?
Yes, We can do.