~ubuntu-branches/debian/experimental/php-nette/experimental

« back to all changes in this revision

Viewing changes to Nette-2.0.13/Nette/Diagnostics/Logger.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2013-11-30 08:47:54 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20131130084754-4udf1xsu9085tnfc
Tags: 2.1.0~rc-1
* New upstream branch
* Update copyright

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 * Logger.
19
 
 *
20
 
 * @author     David Grudl
21
 
 */
22
 
class Logger extends Nette\Object
23
 
{
24
 
        const DEBUG = 'debug',
25
 
                INFO = 'info',
26
 
                WARNING = 'warning',
27
 
                ERROR = 'error',
28
 
                CRITICAL = 'critical';
29
 
 
30
 
        /** @var int interval for sending email is 2 days */
31
 
        public static $emailSnooze = 172800;
32
 
 
33
 
        /** @var callable handler for sending emails */
34
 
        public $mailer = array(__CLASS__, 'defaultMailer');
35
 
 
36
 
        /** @var string name of the directory where errors should be logged; FALSE means that logging is disabled */
37
 
        public $directory;
38
 
 
39
 
        /** @var string email to sent error notifications */
40
 
        public $email;
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 Nette\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
 
                        Nette\Callback::create($this->mailer)->invoke($message, $this->email);
65
 
                }
66
 
                return $res;
67
 
        }
68
 
 
69
 
 
70
 
        /**
71
 
         * Default mailer.
72
 
         * @param  string
73
 
         * @param  string
74
 
         * @return void
75
 
         */
76
 
        public static function defaultMailer($message, $email)
77
 
        {
78
 
                $host = php_uname('n');
79
 
                foreach (array('HTTP_HOST','SERVER_NAME', 'HOSTNAME') as $item) {
80
 
                        if (isset($_SERVER[$item])) {
81
 
                                $host = $_SERVER[$item]; break;
82
 
                        }
83
 
                }
84
 
 
85
 
                $parts = str_replace(
86
 
                        array("\r\n", "\n"),
87
 
                        array("\n", PHP_EOL),
88
 
                        array(
89
 
                                'headers' => implode("\n", array(
90
 
                                        "From: noreply@$host",
91
 
                                        'X-Mailer: Nette Framework',
92
 
                                        'Content-Type: text/plain; charset=UTF-8',
93
 
                                        'Content-Transfer-Encoding: 8bit',
94
 
                                )) . "\n",
95
 
                                'subject' => "PHP: An error occurred on the server $host",
96
 
                                'body' => "[" . @date('Y-m-d H:i:s') . "] $message", // @ - timezone may not be set
97
 
                        )
98
 
                );
99
 
 
100
 
                mail($email, $parts['subject'], $parts['body'], $parts['headers']);
101
 
        }
102
 
 
103
 
}