Plop
A simple logging library for PHP
File.php
1 <?php
2 /*
3  This file is part of Plop, a simple logging library for PHP.
4 
5  Copyright © 2010-2014 François Poirotte
6 
7  Plop is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Plop is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Plop. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 namespace Plop\Handler;
22 
29 {
31  protected $baseFilename;
32 
34  protected $mode;
35 
51  public function __construct(
52  $filename,
53  $mode = 'at',
54  $delay = false
55  ) {
56  $this->baseFilename = $filename;
57  $this->mode = $mode;
58  if ($delay) {
59  parent::__construct(false);
60  } else {
61  $stream = $this->open();
62  parent::__construct($stream);
63  }
64  }
65 
67  public function __destruct()
68  {
69  $this->close();
70  }
71 
79  protected function open()
80  {
81  return fopen($this->baseFilename, $this->mode);
82  }
83 
85  protected function emit(\Plop\RecordInterface $record)
86  {
87  if (!$this->stream) {
88  $this->stream = $this->open();
89  }
90  parent::emit($record);
91  }
92 
99  protected function close()
100  {
101  if (is_resource($this->stream)) {
102  $this->flush();
103  fclose($this->stream);
104  }
105  $this->stream = false;
106  }
107 }
__construct($filename, $mode= 'at', $delay=false)
Definition: File.php:51
$mode
Opening mode for the log file.
Definition: File.php:34
Interface for a log record.
An handler that writes log messages to a file.
Definition: File.php:28
emit(\Plop\RecordInterface $record)
Definition: File.php:85
$stream
The stream where log messages will be write to.
Definition: Stream.php:34
An handler that writes log messages to a PHP stream.
Definition: Stream.php:31
$baseFilename
Path to the log file this handler writes to.
Definition: File.php:31
__destruct()
Free the resources used by this handler.
Definition: File.php:67