[18220] | 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 | /** @ignore */ |
---|
| 24 | if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..'); |
---|
| 25 | |
---|
| 26 | /** |
---|
| 27 | */ |
---|
| 28 | require_once(LOG4PHP_DIR . '/LoggerAppenderSkeleton.php'); |
---|
| 29 | require_once(LOG4PHP_DIR . '/LoggerLog.php'); |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | /** |
---|
| 33 | * ConsoleAppender appends log events to STDOUT or STDERR using a layout specified by the user. |
---|
| 34 | * |
---|
| 35 | * <p>Optional parameter is {@link $target}. The default target is Stdout.</p> |
---|
| 36 | * <p><b>Note</b>: Use this Appender with command-line php scripts. |
---|
| 37 | * On web scripts this appender has no effects.</p> |
---|
| 38 | * <p>This appender requires a layout.</p> |
---|
| 39 | * |
---|
| 40 | * @author Marco Vassura |
---|
| 41 | * @author Knut Urdalen <knut.urdalen@gmail.com> |
---|
| 42 | * @version $Revision: 635069 $ |
---|
| 43 | * @package log4php |
---|
| 44 | * @subpackage appender |
---|
| 45 | */ |
---|
| 46 | class LoggerAppenderConsole extends LoggerAppenderSkeleton { |
---|
| 47 | |
---|
| 48 | const STDOUT = 'php://stdout'; |
---|
| 49 | const STDERR = 'php://stderr'; |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | * Can be 'php://stdout' or 'php://stderr'. But it's better to use keywords <b>STDOUT</b> and <b>STDERR</b> (case insensitive). |
---|
| 53 | * Default is STDOUT |
---|
| 54 | * @var string |
---|
| 55 | */ |
---|
| 56 | protected $target = 'php://stdout'; |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | * @var boolean |
---|
| 60 | * @access private |
---|
| 61 | */ |
---|
| 62 | protected $requiresLayout = true; |
---|
| 63 | |
---|
| 64 | /** |
---|
| 65 | * @var mixed the resource used to open stdout/stderr |
---|
| 66 | * @access private |
---|
| 67 | */ |
---|
| 68 | protected $fp = false; |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | * Set console target. |
---|
| 72 | * @param mixed $value a constant or a string |
---|
| 73 | */ |
---|
| 74 | public function setTarget($value) { |
---|
| 75 | $v = trim($value); |
---|
| 76 | if ($v == self::STDOUT || strtoupper($v) == 'STDOUT') { |
---|
| 77 | $this->target = self::STDOUT; |
---|
| 78 | } elseif ($v == self::STDERR || strtoupper($v) == 'STDERR') { |
---|
| 79 | $target = self::STDERR; |
---|
| 80 | } else { |
---|
| 81 | LoggerLog::debug("Invalid target. Using '".self::STDOUT."' by default."); |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | public function getTarget() { |
---|
| 86 | return $this->target; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | public function activateOptions() { |
---|
| 90 | $this->fp = fopen($this->getTarget(), 'w'); |
---|
| 91 | if($this->fp !== false && $this->layout !== null) { |
---|
| 92 | fwrite($this->fp, $this->layout->getHeader()); |
---|
| 93 | } |
---|
| 94 | $this->closed = (bool)($this->fp === false); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * @see LoggerAppender::close() |
---|
| 99 | */ |
---|
| 100 | public function close() { |
---|
| 101 | if ($this->fp && $this->layout !== null) { |
---|
| 102 | fwrite($this->fp, $this->layout->getFooter()); |
---|
| 103 | fclose($this->fp); |
---|
| 104 | } |
---|
| 105 | $this->closed = true; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | protected function append($event) { |
---|
| 109 | if ($this->fp && $this->layout !== null) { |
---|
| 110 | fwrite($this->fp, $this->layout->format($event)); |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | |
---|