Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

Classes

  • Bar
  • BlueScreen
  • Debugger
  • Dumper
  • FireLogger
  • Helpers
  • Logger
  • OutputDebugger

Interfaces

  • IBarPanel
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Tracy (https://tracy.nette.org)
  5:  * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Tracy;
  9: 
 10: use Tracy;
 11: 
 12: 
 13: /**
 14:  * Logger.
 15:  *
 16:  * @author     David Grudl
 17:  */
 18: class Logger
 19: {
 20:     const DEBUG = 'debug',
 21:         INFO = 'info',
 22:         WARNING = 'warning',
 23:         ERROR = 'error',
 24:         EXCEPTION = 'exception',
 25:         CRITICAL = 'critical';
 26: 
 27:     /** @var int interval for sending email is 2 days */
 28:     public $emailSnooze = 172800;
 29: 
 30:     /** @var callable handler for sending emails */
 31:     public $mailer = array(__CLASS__, 'defaultMailer');
 32: 
 33:     /** @var string name of the directory where errors should be logged; FALSE means that logging is disabled */
 34:     public $directory;
 35: 
 36:     /** @var string|array email or emails to which send error notifications */
 37:     public $email;
 38: 
 39: 
 40:     /**
 41:      * Logs message or exception to file and sends email notification.
 42:      * @param  string|array
 43:      * @param  int   one of constant Debugger::INFO, WARNING, ERROR (sends email), EXCEPTION (sends email), CRITICAL (sends email)
 44:      * @return void
 45:      */
 46:     public function log($message, $priority = NULL)
 47:     {
 48:         if (!is_dir($this->directory)) {
 49:             throw new \RuntimeException("Directory '$this->directory' is not found or is not directory.");
 50:         }
 51: 
 52:         if (is_array($message)) {
 53:             $message = implode(' ', $message);
 54:         }
 55:         $message = preg_replace('#\s*\r?\n\s*#', ' ', trim($message));
 56:         $file = $this->directory . '/' . strtolower($priority ?: self::INFO) . '.log';
 57:         if (!@file_put_contents($file, $message . PHP_EOL, FILE_APPEND | LOCK_EX)) {
 58:             throw new \RuntimeException("Unable to write to log file '$file'. Is directory writable?");
 59:         }
 60: 
 61:         if (in_array($priority, array(self::ERROR, self::EXCEPTION, self::CRITICAL), TRUE)
 62:             && $this->email && $this->mailer
 63:             && @filemtime($this->directory . '/email-sent') + $this->emailSnooze < time() // @ - file may not exist
 64:             && @file_put_contents($this->directory . '/email-sent', 'sent') // @ - file may not be writable
 65:         ) {
 66:             call_user_func($this->mailer, $message, implode(', ', (array) $this->email));
 67:         }
 68:     }
 69: 
 70: 
 71:     /**
 72:      * Default mailer.
 73:      * @param  string
 74:      * @param  string
 75:      * @return void
 76:      * @internal
 77:      */
 78:     public static function defaultMailer($message, $email)
 79:     {
 80:         $host = preg_replace('#[^\w.-]+#', '', isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : php_uname('n'));
 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: Tracy',
 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.2 API documentation generated by ApiGen 2.8.0