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
 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 callback 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:             call_user_func($this->mailer, $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:     private static function defaultMailer($message, $email)
80:     {
81:         $host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] :
82:                 (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '');
83: 
84:         $parts = str_replace(
85:             array("\r\n", "\n"),
86:             array("\n", PHP_EOL),
87:             array(
88:                 'headers' => "From: noreply@$host\nX-Mailer: Nette Framework\n",
89:                 'subject' => "PHP: An error occurred on the server $host",
90:                 'body' => "[" . @date('Y-m-d H:i:s') . "] $message", // @ - timezone may not be set
91:             )
92:         );
93: 
94:         mail($email, $parts['subject'], $parts['body'], $parts['headers']);
95:     }
96: 
97: }
98: 
Nette Framework 2.0.0 API API documentation generated by ApiGen 2.7.0