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

  • NDebugBar
  • NDebugBlueScreen
  • NDebugger
  • NDebugHelpers
  • NFireLogger
  • NLogger

Interfaces

  • IBarPanel
  • Overview
  • Package
  • 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:  * @package Nette\Diagnostics
11:  */
12: 
13: 
14: 
15: /**
16:  * Logger.
17:  *
18:  * @author     David Grudl
19:  * @package Nette\Diagnostics
20:  */
21: class NLogger extends NObject
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 callback 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:     /**
44:      * Logs message or exception to file and sends email notification.
45:      * @param  string|array
46:      * @param  int     one of constant INFO, WARNING, ERROR (sends email), CRITICAL (sends email)
47:      * @return bool    was successful?
48:      */
49:     public function log($message, $priority = self::INFO)
50:     {
51:         if (!is_dir($this->directory)) {
52:             throw new DirectoryNotFoundException("Directory '$this->directory' is not found or is not directory.");
53:         }
54: 
55:         if (is_array($message)) {
56:             $message = implode(' ', $message);
57:         }
58:         $res = error_log(trim($message) . PHP_EOL, 3, $this->directory . '/' . strtolower($priority) . '.log');
59: 
60:         if (($priority === self::ERROR || $priority === self::CRITICAL) && $this->email && $this->mailer
61:             && @filemtime($this->directory . '/email-sent') + self::$emailSnooze < time() // @ - file may not exist
62:             && @file_put_contents($this->directory . '/email-sent', 'sent') // @ - file may not be writable
63:         ) {
64:             call_user_func($this->mailer, $message, $this->email);
65:         }
66:         return $res;
67:     }
68: 
69: 
70: 
71:     /**
72:      * Default mailer.
73:      * @param  string
74:      * @param  string
75:      * @return void
76:      */
77:     private static function defaultMailer($message, $email)
78:     {
79:         $host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] :
80:                 (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '');
81: 
82:         $parts = str_replace(
83:             array("\r\n", "\n"),
84:             array("\n", PHP_EOL),
85:             array(
86:                 'headers' => "From: noreply@$host\nX-Mailer: Nette Framework\n",
87:                 'subject' => "PHP: An error occurred on the server $host",
88:                 'body' => "[" . @date('Y-m-d H:i:s') . "] $message", // @ - timezone may not be set
89:             )
90:         );
91: 
92:         mail($email, $parts['subject'], $parts['body'], $parts['headers']);
93:     }
94: 
95: }
96: 
Nette Framework 2.0.1 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.7.0