Packages

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

  • DebugBar
  • DebugBlueScreen
  • Debugger
  • DebugHelpers
  • FireLogger
  • Logger

Interfaces

  • IBarPanel
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  * @package Nette\Diagnostics
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * Logger.
 17:  *
 18:  * @author     David Grudl
 19:  * @package Nette\Diagnostics
 20:  */
 21: class Logger extends Object
 22: {
 23:     const DEBUG = 'debug',
 24:         INFO = 'info',
 25:         WARNING = 'warning',
 26:         ERROR = 'error',
 27:         CRITICAL = 'critical';
 28: 
 29:     /** @var int interval for sending email is 2 days */
 30:     public static $emailSnooze = 172800;
 31: 
 32:     /** @var callable handler for sending emails */
 33:     public $mailer = array(__CLASS__, 'defaultMailer');
 34: 
 35:     /** @var string name of the directory where errors should be logged; FALSE means that logging is disabled */
 36:     public $directory;
 37: 
 38:     /** @var string email to sent error notifications */
 39:     public $email;
 40: 
 41: 
 42:     /**
 43:      * Logs message or exception to file and sends email notification.
 44:      * @param  string|array
 45:      * @param  int     one of constant INFO, WARNING, ERROR (sends email), CRITICAL (sends email)
 46:      * @return bool    was successful?
 47:      */
 48:     public function log($message, $priority = self::INFO)
 49:     {
 50:         if (!is_dir($this->directory)) {
 51:             throw new DirectoryNotFoundException("Directory '$this->directory' is not found or is not directory.");
 52:         }
 53: 
 54:         if (is_array($message)) {
 55:             $message = implode(' ', $message);
 56:         }
 57:         $res = error_log(trim($message) . PHP_EOL, 3, $this->directory . '/' . strtolower($priority) . '.log');
 58: 
 59:         if (($priority === self::ERROR || $priority === self::CRITICAL) && $this->email && $this->mailer
 60:             && @filemtime($this->directory . '/email-sent') + self::$emailSnooze < time() // @ - file may not exist
 61:             && @file_put_contents($this->directory . '/email-sent', 'sent') // @ - file may not be writable
 62:         ) {
 63:             Callback::create($this->mailer)->invoke($message, $this->email);
 64:         }
 65:         return $res;
 66:     }
 67: 
 68: 
 69:     /**
 70:      * Default mailer.
 71:      * @param  string
 72:      * @param  string
 73:      * @return void
 74:      */
 75:     public static function defaultMailer($message, $email)
 76:     {
 77:         $host = php_uname('n');
 78:         foreach (array('HTTP_HOST','SERVER_NAME', 'HOSTNAME') as $item) {
 79:             if (isset($_SERVER[$item])) {
 80:                 $host = $_SERVER[$item]; break;
 81:             }
 82:         }
 83: 
 84:         $parts = str_replace(
 85:             array("\r\n", "\n"),
 86:             array("\n", PHP_EOL),
 87:             array(
 88:                 'headers' => implode("\n", array(
 89:                     "From: noreply@$host",
 90:                     'X-Mailer: Nette Framework',
 91:                     'Content-Type: text/plain; charset=UTF-8',
 92:                     'Content-Transfer-Encoding: 8bit',
 93:                 )) . "\n",
 94:                 'subject' => "PHP: An error occurred on the server $host",
 95:                 'body' => "[" . @date('Y-m-d H:i:s') . "] $message", // @ - timezone may not be set
 96:             )
 97:         );
 98: 
 99:         mail($email, $parts['subject'], $parts['body'], $parts['headers']);
100:     }
101: 
102: }
103: 
Nette Framework 2.0.12 (for PHP 5.2, un-prefixed) API API documentation generated by ApiGen 2.8.0