<?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;
}
}
|