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

Revision 18220, 3.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 
28require_once(LOG4PHP_DIR . '/LoggerAppenderSkeleton.php');
29require_once(LOG4PHP_DIR . '/LoggerLog.php');
30
31/**
32 * Appends log events to mail using php function {@link PHP_MANUAL#mail}.
33 *
34 * <p>Parameters are {@link $from}, {@link $to}, {@link $subject}.</p>
35 * <p>This appender requires a layout.</p>
36 *
37 * @author  Marco Vassura
38 * @version $Revision: 635069 $
39 * @package log4php
40 * @subpackage appenders
41 */
42class LoggerAppenderMail extends LoggerAppenderSkeleton {
43
44    /**
45     * @var string 'from' field
46     */
47    var $from = null;
48
49    /**
50     * @var string 'subject' field
51     */
52    var $subject = 'Log4php Report';
53   
54    /**
55     * @var string 'to' field
56     */
57    var $to = null;
58
59    /**
60     * @var string used to create mail body
61     * @access private
62     */
63    var $body = '';
64   
65    /**
66     * Constructor.
67     *
68     * @param string $name appender name
69     */
70    public function __construct($name) {
71        parent::__construct($name);
72                $this->requiresLayout = true;
73    }
74
75    public function activateOptions() {
76        $this->closed = false;
77    }
78   
79    public function close() {
80        $from = $this->from;
81        $to = $this->to;
82
83        if (!empty($this->body) and $from !== null and $to !== null and $this->layout !== null) {
84                        $subject = $this->subject;
85            LoggerLog::debug("LoggerAppenderMail::close() sending mail from=[{$from}] to=[{$to}] subject=[{$subject}]");
86            mail(
87                $to, $subject,
88                $this->layout->getHeader() . $this->body . $this->layout->getFooter(),
89                "From: {$from}\r\n"
90            );
91        }
92        $this->closed = true;
93    }
94   
95    /**
96     * @return string
97     */
98    function getFrom()
99    {
100        return $this->from;
101    }
102   
103    /**
104     * @return string
105     */
106    function getSubject()
107    {
108        return $this->subject;
109    }
110
111    /**
112     * @return string
113     */
114    function getTo()
115    {
116        return $this->to;
117    }
118   
119    function setSubject($subject)
120    {
121        $this->subject = $subject;
122    }
123   
124    function setTo($to)
125    {
126        $this->to = $to;
127    }
128
129    function setFrom($from)
130    {
131        $this->from = $from;
132    } 
133
134    function append($event)
135    {
136        if ($this->layout !== null)
137            $this->body .= $this->layout->format($event);
138    }
139}
Note: See TracBrowser for help on using the repository browser.