1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-24 10:06:08 +02:00

Initial commit

This commit is contained in:
Jordi Boggiano
2011-02-17 02:08:00 +01:00
commit ed6b0e32a2
11 changed files with 383 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Formatter;
interface FormatterInterface
{
function format($log, $level, $message);
}

View File

@@ -0,0 +1,48 @@
<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Formatter;
class SimpleFormatter implements FormatterInterface
{
const SIMPLE_FORMAT = "[%date%] %log%.%level%: %message%\n";
const SIMPLE_DATE = "Y-m-d H:i:s";
protected $format;
protected $dateFormat;
public function __construct($format = null, $dateFormat = null)
{
$this->format = $format ?: self::SIMPLE_FORMAT;
$this->dateFormat = $dateFormat ?: self::SIMPLE_DATE;
}
public function format($log, $level, $message)
{
$defaults = array(
'log' => $log,
'level' => $level,
'date' => date($this->dateFormat),
);
if (is_array($message)) {
$vars = array_merge($defaults, $message);
} else {
$vars = $defaults;
$vars['message'] = $message;
}
foreach ($vars as $var => $val) {
$message = str_replace('%'.$var.'%', $val, $message);
}
return $message;
}
}

56
src/Monolog/Log.php Normal file
View File

@@ -0,0 +1,56 @@
<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog;
use Monolog\Writer\WriterInterface;
class Log
{
protected $name;
protected $writers;
public function __construct($name, $writers = array())
{
$this->name = $name;
$this->writers = $writers;
}
public function getName()
{
return $this->name;
}
public function addWriter(WriterInterface $writer)
{
$this->writers[] = $writer;
}
public function log($level, $message)
{
if ($level < $this->level) {
return;
}
foreach ($this->writers as $writer) {
$writer->write($this->name, $level, $message);
}
}
public function setLevel($level)
{
$this->level = $level;
}
public function getLevel()
{
return $level;
}
}

70
src/Monolog/Logger.php Normal file
View File

@@ -0,0 +1,70 @@
<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog;
class Logger
{
const DEBUG = 1;
const INFO = 5;
const WARN = 10;
const ERROR = 15;
const FATAL = 20;
protected $logs;
public function __construct($logs = array())
{
$this->logs = $logs;
}
public function addLog(Log $log)
{
$this->logs[$log->getName()] = $log;
}
public function log($level, $message, $log = null)
{
if (null === $log) {
$logs = $this->logs;
} else {
$logs = (array) $log;
}
foreach ($logs as $log => $dummy) {
$this->logs[$log]->log($level, $message);
}
}
public function debug($message, $log = null)
{
$this->log(self::DEBUG, $message, $log);
}
public function info($message, $log = null)
{
$this->log(self::INFO, $message, $log);
}
public function warn($message, $log = null)
{
$this->log(self::WARN, $message, $log);
}
public function error($message, $log = null)
{
$this->log(self::ERROR, $message, $log);
}
public function fatal($message, $log = null)
{
$this->log(self::FATAL, $message, $log);
}
}

View File

@@ -0,0 +1,41 @@
<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Writer;
class FileWriter extends StreamWriter
{
protected $rotation;
protected $maxAge;
public function __construct($file, $rotation = null, $maxAge = null)
{
parent::__construct($file);
$this->rotation = $rotation;
$this->maxAge = $maxAge;
}
public function close()
{
parent::close();
// TODO rotation
}
public function setRotation($rotation)
{
$this->rotation = $rotation;
}
public function setMaxAge($maxAge)
{
$this->maxAge = $maxAge;
}
}

View File

@@ -0,0 +1,29 @@
<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Writer;
use Monolog\Formatter\FormatterInterface;
class NullWriter implements WriterInterface
{
public function write($log, $level, $message)
{
}
public function close()
{
}
public function setFormatter(FormatterInterface $formatter)
{
}
}

View File

@@ -0,0 +1,48 @@
<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Writer;
use Monolog\Formatter\FormatterInterface;
class StreamWriter implements WriterInterface
{
protected $formatter;
protected $stream;
protected $url;
public function __construct($streamUrl)
{
if (is_resource($streamUrl)) {
$this->stream = $streamUrl;
} else {
$this->url = $streamUrl;
}
}
public function write($log, $level, $message)
{
if (null === $this->stream) {
$this->stream = fopen($this->url, 'a');
}
fwrite($this->stream, $this->formatter->format($log, $level, $message));
}
public function close()
{
fclose($this->stream);
}
public function setFormatter(FormatterInterface $formatter)
{
$this->formatter = $formatter;
}
}

View File

@@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Writer;
use Monolog\Formatter\FormatterInterface;
interface WriterInterface
{
function setFormatter(FormatterInterface $formatter);
function write($log, $level, $message);
function close();
}