source: branches/comu-ver2/data/module/log4php/php4/log4php/appenders/LoggerAppenderConsole.php @ 18220

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

#149 ロガークラス作成

Line 
1<?php
2/**
3 * log4php is a PHP port of the log4j java logging package.
4 *
5 * <p>This framework is based on log4j (see {@link http://jakarta.apache.org/log4j log4j} for details).</p>
6 * <p>Design, strategies and part of the methods documentation are developed by log4j team
7 * (Ceki Gülcü as log4j project founder and
8 * {@link http://jakarta.apache.org/log4j/docs/contributors.html contributors}).</p>
9 *
10 * <p>PHP port, extensions and modifications by VxR. All rights reserved.<br>
11 * For more information, please see {@link http://www.vxr.it/log4php/}.</p>
12 *
13 * <p>This software is published under the terms of the LGPL License
14 * a copy of which has been included with this distribution in the LICENSE file.</p>
15 *
16 * @package log4php
17 * @subpackage appenders
18 */
19
20/**
21 * @ignore
22 */
23if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
24 
25/**
26 */
27require_once(LOG4PHP_DIR . '/LoggerAppenderSkeleton.php');
28require_once(LOG4PHP_DIR . '/LoggerLog.php');
29
30
31define('LOG4PHP_LOGGER_APPENDER_CONSOLE_STDOUT', 'php://stdout');
32define('LOG4PHP_LOGGER_APPENDER_CONSOLE_STDERR', 'php://stderr');
33
34/**
35 * ConsoleAppender appends log events to STDOUT or STDERR using a layout specified by the user.
36 *
37 * <p>Optional parameter is {@link $target}. The default target is Stdout.</p>
38 * <p><b>Note</b>: Use this Appender with command-line php scripts.
39 * On web scripts this appender has no effects.</p>
40 * <p>This appender requires a layout.</p> 
41 *
42 * @author VxR <vxr@vxr.it>
43 * @version $Revision: 1.10 $
44 * @package log4php
45 * @subpackage appenders
46 */
47class LoggerAppenderConsole extends LoggerAppenderSkeleton {
48
49    /**
50     * Can be 'php://stdout' or 'php://stderr'. But it's better to use keywords <b>STDOUT</b> and <b>STDERR</b> (case insensitive).
51     * Default is STDOUT
52     * @var string   
53     */
54    var $target = 'php://stdout';
55   
56    /**
57     * @var boolean
58     * @access private     
59     */
60    var $requiresLayout = true;
61
62    /**
63     * @var mixed the resource used to open stdout/stderr
64     * @access private     
65     */
66    var $fp = false;
67   
68    /**
69     * Constructor.
70     *
71     * @param string $name appender name
72     */
73    function LoggerAppenderConsole($name)
74    {
75        $this->LoggerAppenderSkeleton($name);
76    }
77
78    /**
79     * Set console target.
80     * @param mixed $value a constant or a string
81     */
82    function setTarget($value)
83    {
84        $v = trim($value);
85        if ($v == LOG4PHP_LOGGER_APPENDER_CONSOLE_STDOUT or strtoupper($v) == 'STDOUT') {
86            $this->target = LOG4PHP_LOGGER_APPENDER_CONSOLE_STDOUT;
87        } elseif ($v == LOG4PHP_LOGGER_APPENDER_CONSOLE_STDOUT or strtoupper($v) == 'STDERR') {
88            $target = LOG4PHP_LOGGER_APPENDER_CONSOLE_STDOUT;
89        } else {
90            LoggerLog::debug(
91                "LoggerAppenderConsole::targetWarn() ".
92                "Invalid target. Using '".LOG4PHP_LOGGER_APPENDER_CONSOLE_STDOUT."' by default."
93            );       
94        }
95    }
96
97    function getTarget()
98    {
99        return $this->target;
100    }
101
102    function activateOptions()
103    {
104        LoggerLog::debug("LoggerAppenderConsole::activateOptions()");
105           
106        $this->fp = @fopen($this->getTarget(), 'w');
107   
108        if ($this->fp)
109            @fwrite($this->fp, $this->layout->getHeader());
110
111        $this->closed = (bool)($this->fp === false);
112    }
113   
114    /**
115     * @see LoggerAppender::close()
116     */
117    function close()
118    {
119        LoggerLog::debug("LoggerAppenderConsole::close()");
120       
121        if ($this->fp) {       
122            @fwrite($this->fp, $this->layout->getFooter());   
123            @fclose($this->fp);
124        }
125       
126        $this->closed = true;
127    }
128
129    function append($event)
130    {
131        if ($this->fp and $this->layout !== null) {
132   
133            LoggerLog::debug("LoggerAppenderConsole::append()");
134       
135            @fwrite($this->fp, $this->layout->format($event));
136        }
137    }
138}
139
140?>
Note: See TracBrowser for help on using the repository browser.