PHP Classes
elePHPant
Icontem

File: library/EasyLogger/Handler/StreamHandler.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Nikola Posa  >  EasyLogger  >  library/EasyLogger/Handler/StreamHandler.php  >  Download  
File: library/EasyLogger/Handler/StreamHandler.php
Role: Class source
Content type: text/plain
Description: Stream-based log handler
Class: EasyLogger
Log events to files
Author: By
Last change:
Date: 2 years ago
Size: 1,363 bytes
 

Contents

Class file image Download
<?php
/**
 * EasyLogger
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 */

require_once 'EasyLogger/Handler/HandlerInterface.php';

/**
 * Stream-based log handler implementation.
 *
 * @author Nikola Posa <posa.nikola@gmail.com>
 */
class EasyLogger_Handler_StreamHandler implements EasyLogger_Handler_HandlerInterface
{
    private
$_stream;
   
    public function
__construct($streamOrUrl)
    {
       
$this->_stream = $streamOrUrl;
    }
   
    private function
_initStream()
    {
        if (!
is_resource($this->_stream)) {
            if (!
$this->_stream = @fopen($this->_stream, 'a')) {
                throw new
RuntimeException("The stream: $this->_stream can not be opened.");
            }
        }
    }
   
    public function
handle(array $record)
    {
       
$this->_initStream();
       
       
fwrite($this->_stream, $this->_formatRecord($record));
    }
   
    protected function
_formatRecord($record)
    {
       
$record['dateTime'] = date('Y-m-d H:i:s', $record['timestamp']);
       
       
$formatted = "[%dateTime%] %levelName%: %message%\n";
       
        foreach (
$record as $key => $value) {
           
$formatted = str_replace('%'.$key.'%', (string)$value, $formatted);
        }
       
        return
$formatted;
    }
}