     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:  * LoggerAppenderEcho uses the PHP echo() function to output events.
    21:  *
    22:  * This appender uses a layout.
    23:  *
    24:  * ## Configurable parameters: ##
    25:  *
    26:  * - **htmlLineBreaks** - If set to true, a <br /> element will be inserted
    27:  *     before each line break in the logged message. Default is false.
    28:  *
    29:  * @version $Revision: 1337820 $
    30:  * @package log4php
    31:  * @subpackage appenders
    32:  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
    33:  * @link http://logging.apache.org/log4php/docs/appenders/echo.html Appender documentation
    34:  */
    35: class LoggerAppenderEcho extends LoggerAppender {
    36:     /**
    37:      * Used to mark first append. Set to false after first append.
    38:      * @var boolean
    39:      */
    40:     protected $firstAppend = true;
    41:
    42:     /**
    43:      * If set to true, a <br /> element will be inserted before each line
    44:      * break in the logged message. Default value is false. @var boolean
    45:      */
    46:     protected $htmlLineBreaks = false;
    47:
    48:     public function close() {
    49:         if($this->closed != true) {
    50:             if(!$this->firstAppend) {
    51:                 echo $this->layout->getFooter();
    52:             }
    53:         }
    54:         $this->closed = true;
    55:     }
    56:
    57:     public function append(LoggerLoggingEvent $event) {
    58:         if($this->layout !== null) {
    59:             if($this->firstAppend) {
    60:                 echo $this->layout->getHeader();
    61:                 $this->firstAppend = false;
    62:             }
    63:             $text = $this->layout->format($event);
    64:
    65:             if ($this->htmlLineBreaks) {
    66:                 $text = nl2br($text);
    67:             }
    68:             echo $text;
    69:         }
    70:     }
    71:
    72:     /**
    73:      * Sets the 'htmlLineBreaks' parameter.
    74:      * @param boolean $value
    75:      */
    76:     public function setHtmlLineBreaks($value) {
    77:         $this->setBoolean('htmlLineBreaks', $value);
    78:     }
    79:
    80:     /**
    81:      * Returns the 'htmlLineBreaks' parameter.
    82:      * @returns boolean
    83:      */
    84:     public function getHtmlLineBreaks() {
    85:         return $this->htmlLineBreaks;
    86:     }
    87: }
    88:
    89: