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

Classes

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

Interfaces

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