Namespaces

  • 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

  • Bar
  • BlueScreen
  • Debugger
  • FireLogger
  • Helpers
  • Logger

Interfaces

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