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

Classes

Interfaces

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