PHP Classes

File: src/SimpleMailLogger.php

Recommend this page to a friend!
  Classes of Vitaly   QuEasy PHP PSR Logger   src/SimpleMailLogger.php   Download  
File: src/SimpleMailLogger.php
Role: Class source
Content type: text/plain
Description: Class source
Class: QuEasy PHP PSR Logger
Log messages to containers compliant with PSR-3
Author: By
Last change:
Date: 3 years ago
Size: 2,050 bytes
 

Contents

Class file image Download
<?php

/*
 * Queasy PHP Framework - Logger
 *
 * (c) Vitaly Demyanenko <vitaly_demyanenko@yahoo.com>
 *
 * For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
 */

namespace queasy\log;

/**
 * Simple email logger
 */
class SimpleMailLogger extends Logger
{
    const
DEFAULT_MAIL_TO = 'admin@example.com';
    const
DEFAULT_SUBJECT = 'Log message';

   
/**
     * File system log method.
     *
     * @param string $level Log level
     * @param string $message Log message
     * @param array|null $context Context
     */
   
public function log($level, $message, array $context = array())
    {
       
$preparedMessage = $this->prepareMessage($level, $message, $context) . PHP_EOL;

       
$additionalHeaders = '';
        if (
$this->mailFrom()) {
           
$additionalHeaders .= sprintf("From: %s\r\n", $this->mailFrom());
        }

       
$configHeaders = isset($this->config['headers'])? $this->config['headers']: array();
        foreach (
$configHeaders as $header) {
           
$additionalHeaders .= sprintf("%s\r\n", $header);
        }

       
mail($this->mailTo(), $this->subject(), $preparedMessage, $additionalHeaders);

        return
parent::log($level, $message, $context);
    }

   
/**
     * Get mail "From:" address
     *
     * @return string Email "From:" address
     */
   
protected function mailFrom()
    {
        return isset(
$this->config['mailFrom'])
            ?
$this->config['mailFrom']
            :
null;
    }

   
/**
     * Get mail "To:" address
     *
     * @return string Email "To:" address
     */
   
protected function mailTo()
    {
        return isset(
$this->config['mailTo'])
            ?
$this->config['mailTo']
            : static::
DEFAULT_MAIL_TO;
    }

   
/**
     * Get mail subject
     *
     * @return string Email subject
     */
   
protected function subject()
    {
        return isset(
$this->config['subject'])
            ?
$this->config['subject']
            : static::
DEFAULT_SUBJECT;
    }
}