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 : * @package log4php
19 : */
20 :
21 : /**
22 : * ConsoleAppender appends log events to STDOUT or STDERR.
23 : *
24 : * <p><b>Note</b>: Use this Appender with command-line php scripts.
25 : * On web scripts this appender has no effects.</p>
26 : *
27 : * Configurable parameters of this appender are:
28 : *
29 : * - layout - The layout (required)
30 : * - target - "stdout" or "stderr"
31 : *
32 : * An example php file:
33 : *
34 : * {@example ../../examples/php/appender_console.php 19}
35 : *
36 : * An example configuration file:
37 : *
38 : * {@example ../../examples/resources/appender_console.properties 18}
39 : *
40 : * @version $Revision: 1213283 $
41 : * @package log4php
42 : * @subpackage appenders
43 : */
44 : class LoggerAppenderConsole extends LoggerAppender {
45 :
46 : const STDOUT = 'php://stdout';
47 : const STDERR = 'php://stderr';
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 : protected $target = self::STDOUT;
55 :
56 : /**
57 : * @var mixed the resource used to open stdout/stderr
58 : */
59 : protected $fp = null;
60 :
61 : /**
62 : * Set console target.
63 : * @param mixed $value a constant or a string
64 : */
65 : public function setTarget($value) {
66 2 : $v = trim($value);
67 2 : if ($v == self::STDOUT || strtoupper($v) == 'STDOUT') {
68 1 : $this->target = self::STDOUT;
69 2 : } elseif ($v == self::STDERR || strtoupper($v) == 'STDERR') {
70 1 : $this->target = self::STDERR;
71 1 : } else {
72 0 : $value = var_export($value);
73 0 : $this->warn("Invalid value given for 'target' property: [$value]. Property not set.");
74 : }
75 2 : }
76 :
77 : public function getTarget() {
78 3 : return $this->target;
79 : }
80 :
81 : public function activateOptions() {
82 8 : $this->fp = fopen($this->target, 'w');
83 8 : if(is_resource($this->fp) && $this->layout !== null) {
84 8 : fwrite($this->fp, $this->layout->getHeader());
85 8 : }
86 8 : $this->closed = (bool)is_resource($this->fp) === false;
87 8 : }
88 :
89 : public function close() {
90 9 : if($this->closed != true) {
91 9 : if (is_resource($this->fp) && $this->layout !== null) {
92 8 : fwrite($this->fp, $this->layout->getFooter());
93 8 : fclose($this->fp);
94 8 : }
95 9 : $this->closed = true;
96 9 : }
97 9 : }
98 :
99 : public function append(LoggerLoggingEvent $event) {
100 3 : if (is_resource($this->fp) && $this->layout !== null) {
101 3 : fwrite($this->fp, $this->layout->format($event));
102 3 : }
103 3 : }
104 : }
105 :
|