source: branches/comu-ver2/data/module/log4php/php5/log4php/appenders/LoggerAppenderMailEvent.php @ 18220

Revision 18220, 4.3 KB checked in by yokkuns, 11 years ago (diff)

#149 ロガークラス作成

Line 
1<?php
2/**
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements.  See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 *
19 * @package log4php
20 * @subpackage appenders
21 */
22
23/**
24 * @ignore
25 */
26if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
27
28/**
29 */
30require_once(LOG4PHP_DIR . '/LoggerAppenderSkeleton.php');
31require_once(LOG4PHP_DIR . '/LoggerLog.php');
32
33/**
34 * Log events to an email address. It will be created an email for each event.
35 *
36 * <p>Parameters are
37 * {@link $smtpHost} (optional),
38 * {@link $port} (optional),
39 * {@link $from} (optional),
40 * {@link $to},
41 * {@link $subject} (optional).</p>
42 * <p>A layout is required.</p>
43 *
44 * @author  Domenico Lordi <lordi@interfree.it>
45 * @author  Marco Vassura
46 * @version $Revision: 635069 $
47 * @package log4php
48 * @subpackage appenders
49 */
50class LoggerAppenderMailEvent extends LoggerAppenderSkeleton {
51
52    /**
53     * @var string 'from' field
54     */
55    var $from           = null;
56
57    /**
58     * @var integer 'from' field
59     */
60    var $port           = 25;
61
62    /**
63     * @var string hostname.
64     */
65    var $smtpHost       = null;
66
67    /**
68     * @var string 'subject' field
69     */
70    var $subject        = '';
71
72    /**
73     * @var string 'to' field
74     */
75    var $to             = null;
76   
77    /**
78     * @access private
79     */
80    var $requiresLayout = true;
81
82    /**
83     * Constructor.
84     *
85     * @param string $name appender name
86     */
87    function LoggerAppenderMailEvent($name)
88    {
89        $this->LoggerAppenderSkeleton($name);
90    }
91
92    function activateOptions()
93    {
94        $this->closed = false;
95    }
96   
97    function close()
98    {
99        $this->closed = true;
100    }
101
102    /**
103     * @return string
104     */
105    function getFrom()      { return $this->from; }
106   
107    /**
108     * @return integer
109     */
110    function getPort()      { return $this->port; }
111   
112    /**
113     * @return string
114     */
115    function getSmtpHost()  { return $this->smtpHost; }
116   
117    /**
118     * @return string
119     */
120    function getSubject()   { return $this->subject; }
121
122    /**
123     * @return string
124     */
125    function getTo()        { return $this->to; }
126
127    function setFrom($from)             { $this->from = $from; }
128    function setPort($port)             { $this->port = (int)$port; }
129    function setSmtpHost($smtpHost)     { $this->smtpHost = $smtpHost; }
130    function setSubject($subject)       { $this->subject = $subject; }
131    function setTo($to)                 { $this->to = $to; }
132
133    function append($event)
134    {
135        $from = $this->getFrom();
136        $to   = $this->getTo();
137        if (empty($from) or empty($to))
138            return;
139   
140        $smtpHost = $this->getSmtpHost();
141        $prevSmtpHost = ini_get('SMTP');
142        if (!empty($smtpHost)) {
143            ini_set('SMTP', $smtpHost);
144        } else {
145            $smtpHost = $prevSmtpHost;
146        }
147
148        $smtpPort = $this->getPort();
149        $prevSmtpPort= ini_get('smtp_port');       
150        if ($smtpPort > 0 and $smtpPort < 65535) {
151            ini_set('smtp_port', $smtpPort);
152        } else {
153            $smtpPort = $prevSmtpPort;
154        }
155       
156        LoggerLog::debug(
157            "LoggerAppenderMailEvent::append()" .
158            ":from=[{$from}]:to=[{$to}]:smtpHost=[{$smtpHost}]:smtpPort=[{$smtpPort}]"
159        );
160       
161        if (!@mail( $to, $this->getSubject(),
162            $this->layout->getHeader() . $this->layout->format($event) . $this->layout->getFooter($event),
163            "From: {$from}\r\n"
164        )) {
165            LoggerLog::debug("LoggerAppenderMailEvent::append() mail error");
166        }
167           
168        ini_set('SMTP',         $prevSmtpHost);
169        ini_set('smtp_port',    $prevSmtpPort);
170    }
171}
172
Note: See TracBrowser for help on using the repository browser.