|
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 /** |
|
20 * LoggerAppenderConsole appends log events either to the standard output |
|
21 * stream (php://stdout) or the standard error stream (php://stderr). |
|
22 * |
|
23 * **Note**: Use this Appender with command-line php scripts. On web scripts |
|
24 * this appender has no effects. |
|
25 * |
|
26 * This appender uses a layout. |
|
27 * |
|
28 * ## Configurable parameters: ## |
|
29 * |
|
30 * - **target** - the target stream: "stdout" or "stderr" |
|
31 * |
|
32 * @version $Revision: 1343601 $ |
|
33 * @package log4php |
|
34 * @subpackage appenders |
|
35 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 |
|
36 * @link http://logging.apache.org/log4php/docs/appenders/console.html Appender documentation |
|
37 */ |
|
38 class LoggerAppenderConsole extends LoggerAppender { |
|
39 |
|
40 /** The standard otuput stream. */ |
|
41 const STDOUT = 'php://stdout'; |
|
42 |
|
43 /** The standard error stream.*/ |
|
44 const STDERR = 'php://stderr'; |
|
45 |
|
46 /** The 'target' parameter. */ |
|
47 protected $target = self::STDOUT; |
|
48 |
|
49 /** |
|
50 * Stream resource for the target stream. |
|
51 * @var resource |
|
52 */ |
|
53 protected $fp = null; |
|
54 |
|
55 public function activateOptions() { |
|
56 $this->fp = fopen($this->target, 'w'); |
|
57 if (is_resource($this->fp) && $this->layout !== null) { |
|
58 fwrite($this->fp, $this->layout->getHeader()); |
|
59 } |
|
60 $this->closed = (bool)is_resource($this->fp) === false; |
|
61 } |
|
62 |
|
63 |
|
64 public function close() { |
|
65 if ($this->closed != true) { |
|
66 if (is_resource($this->fp) && $this->layout !== null) { |
|
67 fwrite($this->fp, $this->layout->getFooter()); |
|
68 fclose($this->fp); |
|
69 } |
|
70 $this->closed = true; |
|
71 } |
|
72 } |
|
73 |
|
74 public function append(LoggerLoggingEvent $event) { |
|
75 if (is_resource($this->fp) && $this->layout !== null) { |
|
76 fwrite($this->fp, $this->layout->format($event)); |
|
77 } |
|
78 } |
|
79 |
|
80 /** |
|
81 * Sets the 'target' parameter. |
|
82 * @param string $target |
|
83 */ |
|
84 public function setTarget($target) { |
|
85 $value = trim($target); |
|
86 if ($value == self::STDOUT || strtoupper($value) == 'STDOUT') { |
|
87 $this->target = self::STDOUT; |
|
88 } elseif ($value == self::STDERR || strtoupper($value) == 'STDERR') { |
|
89 $this->target = self::STDERR; |
|
90 } else { |
|
91 $target = var_export($target); |
|
92 $this->warn("Invalid value given for 'target' property: [$target]. Property not set."); |
|
93 } |
|
94 } |
|
95 |
|
96 /** |
|
97 * Returns the value of the 'target' parameter. |
|
98 * @return string |
|
99 */ |
|
100 public function getTarget() { |
|
101 return $this->target; |
|
102 } |
|
103 } |