Skip to content

Commit

Permalink
Merge pull request #34 from silinternational/develop
Browse files Browse the repository at this point in the history
Release 4.0.0 -- update PHP and dependencies
  • Loading branch information
briskt authored Jun 18, 2024
2 parents 3f281e3 + b01fb43 commit 80a3642
Show file tree
Hide file tree
Showing 17 changed files with 104 additions and 146 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
vendor/
.idea/
composer.lock
modules/
8 changes: 0 additions & 8 deletions .whitesource

This file was deleted.

10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
it-now: clean install test

clean:
docker-compose kill
docker-compose rm -f
docker compose kill
docker compose rm -f

install:
docker-compose run --rm cli bash -c "composer install"
docker compose run --rm cli bash -c "composer install"

update:
docker-compose run --rm cli bash -c "composer update"
docker compose run --rm cli bash -c "composer update"

test:
docker-compose run --rm cli bash -c "cd /data/tests; ../vendor/bin/phpunit ."
docker compose run --rm cli bash -c "cd /data/tests; ../vendor/bin/phpunit ."
38 changes: 25 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,32 @@ Various PSR3-compatible logging adapters.

## Adapter-specific notes

### Psr3ConsoleLogger
- Since this is ambiguous regarding whether logged messages are affected by
PHP's output buffering, we recommend using either `Psr3EchoLogger` or
`Psr3StdOutLogger` instead.

### Psr3EchoLogger
- This `echo`es out the log messages, allowing the output to be buffered so that
it appears at the expected place within the rest of the output (such as in
tests).

### Prs3StdOutLogger
- This writes to stdout, bypassing any output buffering that PHP might be doing.
A basic PSR-3 compliant logger that merely echoes logs to the console (primarily intended for use in tests).

This `echo`es out the log messages, allowing the output to be buffered so that it appears at the expected place within the rest of the output (such as in tests).

### Psr3FakeLogger

A basic PSR-3 compliant logger that stores log entries in an array. This allows confirmation that logging has occurred using hasLogs and hasSpecificLog methods.

### Psr3SamlLogger

A minimalist wrapper library (for SimpleSAML\Logger) to make it PSR-3 compatible.

### Psr3StdOutLogger

A basic PSR-3 compliant logger that writes logs to stdout. This bypasses any output buffering that PHP may be doing.

### Psr3SyslogLogger

A basic PSR-3 compliant logger that sends logs to syslog.

### Psr3Yii2Logger
- Make sure your Yii config bootstraps the `log` component. In other words,
include something like this in your Yii config:
`'bootstrap' => ['log']`

A basic PSR-3 compliant logger that sends logs to Yii2's logging functions.

NOTE: Yii2 only provides error, warning, info, and trace levels, so the PSR-3 log levels were mapped to those on a best-effort basis.

Make sure your Yii config bootstraps the `log` component. In other words, include something like this in your Yii config: `'bootstrap' => ['log']`
15 changes: 8 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"description": "Various PSR3-compatible logging adapters.",
"type": "library",
"license": "MIT",
"version": "4.0.0",
"authors": [
{
"name": "Matt Henderson",
Expand All @@ -11,19 +12,19 @@
],
"minimum-stability": "stable",
"require": {
"php": "^7.4 || ^8.0",
"psr/log": "^1.0"
"php": "^8.1",
"psr/log": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"roave/security-advisories": "dev-master",
"monolog/monolog": "^1.22",
"simplesamlphp/simplesamlphp": "^1.15.2",
"monolog/monolog": "^2.0 | ^3.0",
"simplesamlphp/simplesamlphp": "^2.0",
"yiisoft/yii2": "^2.0"
},
"suggest": {
"monolog/monolog": "^1.22",
"simplesamlphp/simplesamlphp": "^1.15.2",
"monolog/monolog": "^2.0 | ^3.0",
"simplesamlphp/simplesamlphp": "^2.0",
"yiisoft/yii2": "^2.0"
},
"autoload": {
Expand All @@ -41,7 +42,7 @@
"config": {
"allow-plugins": {
"yiisoft/yii2-composer": true,
"simplesamlphp/composer-module-installer": true
"simplesamlphp/composer-module-installer": false
}
}
}
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '2.2'
services:
cli:
image: silintl/php8:8.1
Expand Down
44 changes: 6 additions & 38 deletions src/LoggerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,29 @@
*/
abstract class LoggerBase extends AbstractLogger
{
/**
* Interpolate context values into the message placeholders.
*
* @param string $message The data to be logged.
* @param array $context (Optional:) The array of values to insert into the
* corresponding placeholders.
* @return string The resulting log message.
*/
protected function interpolate($message, array $context = []): string
{
if (is_string($message)) {
return $this->interpolateString($message, $context);
}

return $this->interpolateString(
var_export($message, true),
$context
);
}

/**
* Interpolate context values into the given string.
*
*
* This is based heavily on the example implementation here:
* https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#12-message
*
* @param string $message The message (potentially with placeholders).
*
* @param string|\Stringable $message The message (potentially with placeholders).
* @param array $context (Optional:) The array of values to insert into the
* corresponding placeholders.
* @return string The resulting string.
*/
private function interpolateString(string $message, array $context = []): string
protected function interpolate(string|\Stringable $message, array $context = []): string
{
// Build a replacement array with braces around the context keys.
$replace = [];
foreach ($context as $key => $value) {
// Check that the value can be cast to string.
if (!is_array($value) && (!is_object($value) || method_exists($value, '__toString'))) {
if (!is_array($value) && (!is_object($value) || $value instanceof \Stringable)) {
$replace['{' . $key . '}'] = $value;
}
}

// Interpolate replacement values into the message.
$result = strtr($message, $replace);

if (is_string($result)) {
return $result;
}

/* If something went wrong, return the original message (with a
* warning). */
return sprintf(
'%s (WARNING: Unable to interpolate the context values into the message. %s).',
$message,
var_export($replace, true)
);
return strtr($message, $replace);
}
}
9 changes: 0 additions & 9 deletions src/Psr3ConsoleLogger.php

This file was deleted.

8 changes: 2 additions & 6 deletions src/Psr3EchoLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@
class Psr3EchoLogger extends LoggerBase
{
/**
* Log a message.
*
* @param mixed $level
* @param string $message
* @param array $context
* {@inheritdoc}
*/
public function log($level, $message, array $context = [])
public function log(mixed $level, string|\Stringable $message, array $context = []): void
{
echo sprintf(
'LOG: [%s] %s',
Expand Down
12 changes: 4 additions & 8 deletions src/Psr3FakeLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@
class Psr3FakeLogger extends LoggerBase
{
/** @var string[] $log */
private $log = [];
private array $log = [];

/**
* Log a message.
*
* @param mixed $level
* @param string $message
* @param array $context
* {@inheritdoc}
*/
public function log($level, $message, array $context = [])
public function log(mixed $level, string|\Stringable $message, array $context = []): void
{
$this->log[] = sprintf(
'LOG: [%s] %s',
Expand All @@ -40,7 +36,7 @@ public function hasLogs(): bool
* @param bool $strict
* @return bool
*/
public function hasSpecificLog(string $needle, $strict = false): bool
public function hasSpecificLog(string $needle, bool $strict = false): bool
{
$strictMatch = false;
$looseMatch = false;
Expand Down
10 changes: 2 additions & 8 deletions src/Psr3SamlLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,9 @@
class Psr3SamlLogger extends LoggerBase
{
/**
* Log a message.
*
* @param mixed $level One of the \Psr\Log\LogLevel::* constants.
* @param string $message The message to log, possibly with {placeholder}s.
* @param array $context An array of placeholder => value entries to insert
* into the message.
* @return void
* {@inheritdoc}
*/
public function log($level, $message, array $context = [])
public function log(mixed $level, string|\Stringable $message, array $context = []): void
{
$messageWithContext = $this->interpolate($message, $context);
switch ($level) {
Expand Down
10 changes: 3 additions & 7 deletions src/Psr3StdOutLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@
class Psr3StdOutLogger extends LoggerBase
{
/**
* Log a message.
*
* @param mixed $level
* @param string $message
* @param array $context
* {@inheritdoc}
*/
public function log($level, $message, array $context = [])
public function log(mixed $level, string|\Stringable $message, array $context = []): void
{
$this->writeToStdOut(
sprintf(
Expand All @@ -26,7 +22,7 @@ public function log($level, $message, array $context = [])
);
}

private function writeToStdOut($message)
private function writeToStdOut(string $message): void
{
$fileHandle = fopen('php://stdout', 'w+');
if ($fileHandle === false) {
Expand Down
16 changes: 6 additions & 10 deletions src/Psr3SyslogLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,30 @@
namespace Sil\Psr3Adapters;

use Monolog\Handler\SyslogHandler;
use Monolog\Level;
use Monolog\Logger;

/**
* A basic PSR-3 compliant logger that sends logs to syslog.
*/
class Psr3SyslogLogger extends LoggerBase
{
private $logger;
private Logger $logger;

public function __construct($name = 'name', $ident = 'ident')
{
$this->logger = new Logger($name);
$this->logger->pushHandler(new SyslogHandler(
$ident,
LOG_USER,
Logger::WARNING
Level::Warning
));
}

/**
* Log a message.
*
* @param mixed $level
* @param string $message
* @param array $context
* @return void
* {@inheritdoc}
*/
public function log($level, $message, array $context = [])
public function log(mixed $level, string|\Stringable $message, array $context = []): void
{
$this->logger->log($level, $message, $context);
}
Expand Down
17 changes: 4 additions & 13 deletions src/Psr3Yii2Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,12 @@
class Psr3Yii2Logger extends LoggerBase
{
/**
* Log a message.
*
* @param mixed $level One of the \Psr\Log\LogLevel::* constants.
* @param string|array $message The message to log, possibly with {placeholder}s.
* @param array $context An array of placeholder => value entries to insert
* into the message.
* @return void
* {@inheritdoc}
*/
public function log($level, $message, array $context = [])
public function log(mixed $level, string|\Stringable $message, array $context = []): void
{
if (is_array($message)) {
$messageToLog = array_merge($message, $context);
} else {
$messageToLog = $this->interpolate($message, $context);
}
$messageToLog = $this->interpolate($message, $context);

switch ($level) {
case PsrLogLevel::EMERGENCY:
case PsrLogLevel::ALERT:
Expand Down
Loading

0 comments on commit 80a3642

Please sign in to comment.