LoggerAppenderEcho.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. * LoggerAppenderEcho uses {@link PHP_MANUAL#echo echo} function to output events.
  22. *
  23. * <p>This appender requires a layout.</p>
  24. *
  25. * An example php file:
  26. *
  27. * {@example ../../examples/php/appender_echo.php 19}
  28. *
  29. * An example configuration file:
  30. *
  31. * {@example ../../examples/resources/appender_echo.properties 18}
  32. *
  33. * The above example would print the following:
  34. * <pre>
  35. * Tue Sep 8 22:44:55 2009,812 [6783] DEBUG appender_echo - Hello World!
  36. * </pre>
  37. *
  38. * @version $Revision: 883108 $
  39. * @package log4php
  40. * @subpackage appenders
  41. */
  42. class LoggerAppenderEcho extends LoggerAppender {
  43. /** boolean used internally to mark first append */
  44. private $firstAppend = true;
  45. public function __construct($name = '') {
  46. parent::__construct($name);
  47. $this->requiresLayout = true;
  48. $this->firstAppend = true;
  49. }
  50. public function __destruct() {
  51. $this->close();
  52. }
  53. public function activateOptions() {
  54. $this->closed = false;
  55. }
  56. public function close() {
  57. if($this->closed != true) {
  58. if(!$this->firstAppend) {
  59. echo $this->layout->getFooter();
  60. }
  61. }
  62. $this->closed = true;
  63. }
  64. public function append(LoggerLoggingEvent $event) {
  65. if($this->layout !== null) {
  66. if($this->firstAppend) {
  67. echo $this->layout->getHeader();
  68. $this->firstAppend = false;
  69. }
  70. echo $this->layout->format($event);
  71. }
  72. }
  73. }