StreamLogger.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007 by Ivan Y. Khvostishkov                            *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00015     final class StreamLogger extends BaseLogger
00016     {
00017         private $stream = null;
00018         
00019         public function __destruct()
00020         {
00021             try {
00022                 $this->close();
00023             } catch (BaseException $e) {
00024                 // boo.
00025             }
00026         }
00027         
00031         public static function create()
00032         {
00033             return new self;
00034         }
00035         
00039         public function getOutputStream()
00040         {
00041             return $this->stream;
00042         }
00043         
00047         public function setOutputStream(OutputStream $stream)
00048         {
00049             $this->stream = $stream;
00050             
00051             return $this;
00052         }
00053         
00057         public function flush()
00058         {
00059             if ($this->stream)
00060                 $this->stream->flush();
00061             
00062             return $this;
00063         }
00064         
00068         public function close()
00069         {
00070             if ($this->stream) {
00071                 
00072                 $this->flush();
00073                 $this->stream->close();
00074             
00075                 $this->stream = null;
00076             }
00077             
00078             return $this;
00079         }
00080         
00084         protected function publish(LogRecord $record)
00085         {
00086             if (!$this->stream)
00087                 return $this;
00088             
00089             $this->stream->write($record->toString()."\n");
00090             
00091             return $this;
00092         }
00093     }
00094 ?>