Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Singer committed Jan 30, 2013
0 parents commit eedf4d3
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
composer.phar
composer.lock
vendor
.idea
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Wa72SimpleLogger
================

Wa72SimpleLogger is a very, very simple file logger implementing \Psr\Log\LoggerInterface (PSR-3)

Wa72SimpleLogger is intended for small projects without a framework,
or if your framework does not support PSR-3 yet, and you don't want to use more
sophisticated logging solutions like Monolog. If you just need to output a few log
messages to a log file but want to stick to the PSR-3 standard (thus having the option to upgrade
to another logger) this class is for you.
54 changes: 54 additions & 0 deletions Wa72SimpleLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* This class is a very, very simple file logger implementing \Psr\Log\LoggerInterface (PSR-3)
*
* Wa72SimpleLogger is intended for small projects without a framework,
* or if the used framework does not support PSR-3 yet and you need a PSR-3 compliant logger, and you don't want to use more
* sophisticated logging solutions like Monolog. If you just need to output a few log
* messages to a log file but want to stick to the PSR-3 standard (thus having the option to upgrade
* to another logger) this class is for you.
*
* @author Christoph Singer
* @license MIT
*/
class Wa72SimpleLogger extends \Psr\Log\AbstractLogger
{
protected $logfile;

/**
* @param string $logfile Filename to log messages to (complete path)
* @throws \InvalidArgumentException When logfile cannot be created or is not writeable
*/
public function __construct($logfile)
{
if (!file_exists($logfile)) {
if (!touch($logfile)) throw new \InvalidArgumentException('Log file ' . $logfile . ' cannot be created');
}
if (!is_writable($logfile)) throw new \InvalidArgumentException('Log file ' . $logfile . ' is not writeable');
$this->logfile = $logfile;
}

public function log($level, $message, array $context = array())
{
$logline = '[' . date('Y-m-d h:m:i') . '] ' . strtoupper($level) . ': ' . $this->interpolate($message, $context) . "\n";
file_put_contents($this->logfile, $logline, FILE_APPEND);
}

/**
* Interpolates context values into the message placeholders.
*
* This function is just copied from the example in the PSR-3 spec
*
*/
protected function interpolate($message, array $context = array())
{
// build a replacement array with braces around the context keys
$replace = array();
foreach ($context as $key => $val) {
$replace['{' . $key . '}'] = $val;
}

// interpolate replacement values into the message and return
return strtr($message, $replace);
}
}
27 changes: 27 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name":"wa72/simplelogger",
"minimum-stability":"dev",
"description":"Wa72SimpleLogger is a very, very simple file logger implementing \\Psr\\Log\\LoggerInterface (PSR-3)",
"keywords":["log", "logger", "PSR-3"],
"homepage":"http://github.com/wasinger/simplelogger",
"type":"library",
"license":"MIT",
"authors":[
{
"name":"Christoph Singer",
"email":"singer@webagentur72.de",
"homepage":"http://www.webagentur72.de"
}
],
"require":{
"php":">=5.3.1",
"psr/log":"1.*"
},
"minimum-stability":"dev",
"autoload":{
"psr-0":{
"Wa72SimpleLogger":""
}
}
}

0 comments on commit eedf4d3

Please sign in to comment.