Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Diagnostics
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
      • Diagnostics
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • PhpGenerator
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
  • NetteModule
  • none

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 Nette Framework (https://nette.org)
 5:  * Copyright (c) 2004 David Grudl (https://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 $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|array email or emails to which send 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 = NULL)
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:         $message = preg_replace('#\s*\r?\n\s*#', ' ', trim($message));
55:         $file = $this->directory . '/' . strtolower($priority ?: self::INFO) . '.log';
56:         $res = (bool) file_put_contents($file, $message . PHP_EOL, FILE_APPEND | LOCK_EX);
57: 
58:         if (($priority === self::ERROR || $priority === self::CRITICAL) && $this->email && $this->mailer
59:             && @filemtime($this->directory . '/email-sent') + $this->emailSnooze < time() // @ - file may not exist
60:             && @file_put_contents($this->directory . '/email-sent', 'sent') // @ - file may not be writable
61:         ) {
62:             call_user_func($this->mailer, $message, implode(', ', (array) $this->email));
63:         }
64:         return $res;
65:     }
66: 
67: 
68:     /**
69:      * Default mailer.
70:      * @param  string
71:      * @param  string
72:      * @return void
73:      */
74:     public static function defaultMailer($message, $email)
75:     {
76:         $host = preg_replace('#[^\w.-]+#', '', isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : php_uname('n'));
77:         $parts = str_replace(
78:             array("\r\n", "\n"),
79:             array("\n", PHP_EOL),
80:             array(
81:                 'headers' => implode("\n", array(
82:                     "From: noreply@$host",
83:                     'X-Mailer: Nette Framework',
84:                     'Content-Type: text/plain; charset=UTF-8',
85:                     'Content-Transfer-Encoding: 8bit',
86:                 )) . "\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 2.1 API documentation generated by ApiGen 2.8.0