Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ composer.phar
composer.lock
vendor
.idea
*.code-workspace
33 changes: 14 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,40 @@
Wa72SimpleLogger (collection of PHP logger classes)
===================================================
# SimpleLogger (collection of PHP logger classes) - Forked from/Credits to wasinger/simplelogger https://github.com/wasinger/simplelogger

Wa72SimpleLogger is a collection of very simple logger classes for PHP 5.4 implementing \Psr\Log\LoggerInterface (PSR-3),
SimpleLogger is a collection of very simple logger classes for PHP 5.4 implementing \Psr\Log\LoggerInterface (PSR-3),
the common logger interface standardized by the PHP Framework Interop Group (www.php-fig.org).

Wa72SimpleLogger is intended for small projects or testing purposes if you don't need a full-featured logging solution
SimpleLogger is intended for small projects or testing purposes if you don't need a full-featured logging solution
like Monolog.

If you just need to output a few log messages in a small PHP project but want to stick to the PSR-3 standard this package is for you. When your project grows you can simply replace it by a more advanced logging solution like Monolog.

Loggers
-------
## Loggers

- \Wa72\SimpleLogger\EchoLogger: Just echo the log message
- \Midweste\SimpleLogger\EchoLogger: Just echo the log message

- \Wa72\SimpleLogger\FileLogger: Log to a file
- \Midweste\SimpleLogger\FileLogger: Log to a file

- \Wa72\SimpleLogger\ArrayLogger: Keep log messages in an array for later use (e.g. display it to the user)
- \Midweste\SimpleLogger\ArrayLogger: Keep log messages in an array for later use (e.g. display it to the user)

- \Wa72\SimpleLogger\ConsoleLogger: Log to the Symfony2 console => *DEPRECATED: use `Symfony\Component\Console\Logger\ConsoleLogger` instead*
- \Midweste\SimpleLogger\SessionLogger: Keep log messages in a session for later use (e.g. display it to the user on another page)

- \Midweste\SimpleLogger\ConsoleLogger: Log to the Symfony2 console => _DEPRECATED: use `Symfony\Component\Console\Logger\ConsoleLogger` instead_

Installation
------------
## Installation

- `composer require wa72/simplelogger`
- `composer require midweste/simplelogger`


Usage
-----
## Usage

```php
$logger = new \Wa72\SimpleLogger\FileLogger('/path/to/logfile');
$logger = new \Midweste\SimpleLogger\FileLogger('/path/to/logfile');
$logger->info('This is the first log message');
```

**NEW**: it's now possible to set a minimum log level in the constructor of FileLogger, EchoLogger and ArrayLogger:

```php
$logger = new \Wa72\SimpleLogger\FileLogger('/path/to/logfile', \Psr\Log\LogLevel::ERROR);
$logger = new \Midweste\SimpleLogger\FileLogger('/path/to/logfile', \Psr\Log\LogLevel::ERROR);
$logger->info('This is the first log message'); // this message will be discarded
$logger->error('This is an error message'); // this message will be logged
```
Expand All @@ -52,4 +48,3 @@ In one of my projects there was a "fetcher" class that fetched some information
- if called from the command line, it is given a ConsoleLogger

- if called from the web interface, it is given an ArrayLogger. The output of this logger is then displayed to the user on the web page.

14 changes: 0 additions & 14 deletions Wa72SimpleLogger.php

This file was deleted.

51 changes: 26 additions & 25 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
{
"name":"wa72/simplelogger",
"description":"Wa72SimpleLogger is a collection of very simple loggers 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":"[email protected]",
"homepage":"http://www.webagentur72.de"
}
],
"require":{
"php":">=5.4",
"psr/log":"^1.0.0"
},
"provide": {
"psr/log-implementation": "1.0.0"
},
"autoload":{
"psr-0":{
"Wa72\\SimpleLogger":"."
}
"name": "midweste/simplelogger",
"description": "SimpleLogger is a collection of very simple loggers implementing \\Psr\\Log\\LoggerInterface (PSR-3)",
"keywords": [
"log",
"logger",
"PSR-3"
],
"homepage": "http://github.com/midwest/simplelogger",
"type": "package",
"license": "MIT",
"authors": [
{
"name": "midweste"
}
],
"require": {
"php": ">=5.4",
"psr/log": "^1.0.0"
},
"provide": {
"psr/log-implementation": "1.0.0"
},
"autoload": {
"psr-4": {
"Midweste\\SimpleLogger\\": "src/"
}
}
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
namespace Wa72\SimpleLogger;

namespace Midweste\SimpleLogger;


use Psr\Log\AbstractLogger;
Expand Down Expand Up @@ -50,9 +51,9 @@ protected function interpolate($message, array $context)
} elseif ($val instanceof \DateTimeInterface) {
$replacements["{{$key}}"] = $val->format(\DateTime::RFC3339);
} elseif (\is_object($val)) {
$replacements["{{$key}}"] = '[object '.\get_class($val).']';
$replacements["{{$key}}"] = '[object ' . \get_class($val) . ']';
} else {
$replacements["{{$key}}"] = '['.\gettype($val).']';
$replacements["{{$key}}"] = '[' . \gettype($val) . ']';
}
}

Expand All @@ -71,4 +72,4 @@ protected function format($level, $message, $context, $timestamp = null)
if ($timestamp === null) $timestamp = date('Y-m-d H:i:s');
return '[' . $timestamp . '] ' . strtoupper($level) . ': ' . $this->interpolate($message, $context) . "\n";
}
}
}
4 changes: 3 additions & 1 deletion Wa72/SimpleLogger/ArrayLogger.php → src/ArrayLogger.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php
namespace Wa72\SimpleLogger;

namespace Midweste\SimpleLogger;

use Psr\Log\LogLevel;

class ArrayLogger extends AbstractSimpleLogger
Expand Down
13 changes: 8 additions & 5 deletions Wa72/SimpleLogger/ConsoleLogger.php → src/ConsoleLogger.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php
namespace Wa72\SimpleLogger;

namespace Midweste\SimpleLogger;

use Psr\Log\AbstractLogger;
use Symfony\Component\Console\Output\OutputInterface;

Expand All @@ -10,13 +12,15 @@
*
* @deprecated Use Symfony\Component\Console\Logger\ConsoleLogger instead
*/
class ConsoleLogger extends AbstractLogger {
class ConsoleLogger extends AbstractLogger
{
/**
* @var \Symfony\Component\Console\Output\OutputInterface
*/
protected $out;

function __construct(OutputInterface $out) {
function __construct(OutputInterface $out)
{
$this->out = $out;
}
/**
Expand All @@ -30,8 +34,7 @@ function __construct(OutputInterface $out) {
public function log($level, $message, array $context = array())
{

$message = '<'. $level . '>' . $message . '</'. $level . '>';
$message = '<' . $level . '>' . $message . '</' . $level . '>';
$this->out->writeln($message);
}

}
5 changes: 4 additions & 1 deletion Wa72/SimpleLogger/EchoLogger.php → src/EchoLogger.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php

/**
* This is a logger implementing \Psr\Log\LoggerInterface (PSR-3) that just echos the log messages
*
*
* @author Christoph Singer
* @license MIT
*/
namespace Wa72\SimpleLogger;

namespace Midweste\SimpleLogger;

use Psr\Log\LogLevel;

class EchoLogger extends AbstractSimpleLogger
Expand Down
5 changes: 4 additions & 1 deletion Wa72/SimpleLogger/FileLogger.php → src/FileLogger.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php

/**
* This class is a simple file logger implementing \Psr\Log\LoggerInterface (PSR-3)
*
*
* @author Christoph Singer
* @license MIT
*/
namespace Wa72\SimpleLogger;

namespace Midweste\SimpleLogger;

use Psr\Log\LogLevel;

class FileLogger extends AbstractSimpleLogger
Expand Down
100 changes: 100 additions & 0 deletions src/SessionLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Midweste\SimpleLogger;

use Psr\Log\LogLevel;

class SessionLogger extends AbstractSimpleLogger
{

public function __construct($min_level = LogLevel::DEBUG, $name = '')
{
$this->min_level = $min_level;
$this->name = (!empty($name) && is_string($name)) ? 'Midweste\SimpleLogger\\' . $name : 'Midweste\SimpleLogger\SessionLogger';
}

private function &getSession(): array
{
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if (empty($_SESSION[$this->name])) {
$_SESSION[$this->name] = [];
}
return $_SESSION[$this->name];
}

public function log($level, $message, array $context = array())
{
if (!$this->min_level_reached($level)) {
return;
}
$this->getSession()[] = array(
'timestamp' => date('Y-m-d H:i:s'),
'level' => $level,
'message' => $message,
'context' => $context
);
}

/**
* Get all log entries
*
* @param callable|null $formatter An optional formatting function called on every log entry.
* This formatting function gets an array parameter with keys 'timestamp', 'level', 'message', and 'context'
* and must return the new log entry.
* @return array Array of associative log entry arrays with keys 'timestamp', 'level', 'message', and 'context',
* unless the log entries are converted to something else by the $formatter parameter.
*/
public function get($formatter = null)
{
$r = $this->getSession();
if (is_callable($formatter)) {
foreach ($r as $i => $a) {
$r[$i] = call_user_func($formatter, $a);
}
}
return $r;
}

/**
* Get all log entries and clear the log
*
* @param callable|null $formatter An optional formatting function called on every log entry.
* This formatting function gets an array parameter with keys 'timestamp', 'level', 'message', and 'context'
* and must return the new log entry.
* @return array Array of associative log entry arrays with keys 'timestamp', 'level', 'message', and 'context'
* unless the log entries are converted to something else by the $formatter parameter.
*/
public function getClear($formatter = null)
{
$r = $this->getSession();
$this->clear();
if (is_callable($formatter)) {
foreach ($r as $i => $a) {
$r[$i] = call_user_func($formatter, $a);
}
}
return $r;
}

/**
* Clear the log
*
*/
public function clear()
{
unset($_SESSION[$this->name]);
}

/**
* Formatter function that can be used as parameter for the get() and getClear() methods
*
* @param array $a
* @return string
*/
public function formatter(array $a)
{
return $this->format($a['level'], $a['message'], $a['context'], $a['timestamp']);
}
}