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
25 changes: 24 additions & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: 7.4"
php-version: "7.4"
ini-values: "memory_limit=-1"

- name: "Install dependencies (Composer)"
Expand All @@ -81,3 +81,26 @@ jobs:
- name: "Run unit tests (PHPUnit)"
shell: "bash"
run: "composer test"

unit-tests-psr-log-v3:
name: "Unit tests (psr/log v3)"
runs-on: "ubuntu-latest"
steps:
- name: "Checkout repository"
uses: "actions/checkout@v2"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: "8.1"
ini-values: "memory_limit=-1"

- name: "Install dependencies with psr/log v3"
shell: "bash"
run: |
composer config platform.php 8.1
composer update --with psr/log:^3 --with-all-dependencies --no-interaction

- name: "Run unit tests (PHPUnit)"
shell: "bash"
run: "composer test"
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"psr/http-message": "^1.0",
"psr/http-server-handler": "^1.0",
"psr/http-server-middleware": "^1.0",
"psr/log": "^1.1"
"psr/log": "^1.1 || ^2.0 || ^3.0"
Comment thread
tsoslow marked this conversation as resolved.
Comment thread
tsoslow marked this conversation as resolved.
},
"autoload": {
"psr-4": {
Expand Down
18 changes: 17 additions & 1 deletion src/StderrLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,16 @@ public function __construct(string $minLevel = LogLevel::WARNING, $stream = 'php
}
}

public function log($level, $message, array $context = [])
public function log($level, $message, array $context = []): void
{
if (!is_string($level)) {
if (is_object($level) && method_exists($level, '__toString')) {
$level = (string) $level;
} else {
throw new InvalidArgumentException('Invalid log level: must be a string or stringable object');
}
Comment thread
tsoslow marked this conversation as resolved.
}

if (!isset(self::LOG_LEVEL_MAP[$level])) {
throw new InvalidArgumentException("Invalid log level: {$level}");
Comment thread
tsoslow marked this conversation as resolved.
}
Expand All @@ -67,6 +75,14 @@ public function log($level, $message, array $context = [])
$context['exception'] = explode("\n", (string) $exception);
}

if (!is_string($message)) {
if (is_object($message) && method_exists($message, '__toString')) {
$message = (string) $message;
} else {
throw new InvalidArgumentException('Invalid log message: must be a string or stringable object');
}
Comment thread
tsoslow marked this conversation as resolved.
}

fwrite($this->stream, json_encode(compact('level', 'message', 'context')) . "\n");
}
}
2 changes: 1 addition & 1 deletion tests/Integration/IntegTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected function createHttpServer(ServerRequestInterface $request): HttpServer

protected function failOnLoggedErrors(): void
{
$this->logger->method('error')->willReturnCallback(function (string $message, array $context) {
$this->logger->method('error')->willReturnCallback(function ($message, array $context = []) {
$message = "Logged an error: {$message}\nContext:\n";
foreach ($context as $key => $value) {
$message .= "- {$key}: {$value}\n";
Expand Down
52 changes: 52 additions & 0 deletions tests/Integration/StderrLoggerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace SlackPhp\Framework\Tests\Integration;

use PHPUnit\Framework\TestCase;
use Psr\Log\InvalidArgumentException;
use Psr\Log\LogLevel;
use SlackPhp\Framework\StderrLogger;

class StderrLoggerTest extends TestCase
{
public function testStringableMessageIsRenderedAsStringInJsonLog(): void
{
$stream = fopen('php://temp', 'a+');
$logger = new StderrLogger(LogLevel::DEBUG, $stream);

$message = new class() {
public function __toString(): string
{
return 'stringable-message';
}
};

$logger->log(LogLevel::ERROR, $message, ['foo' => 'bar']);

rewind($stream);
$line = trim((string) stream_get_contents($stream));
$payload = json_decode($line, true);

$this->assertSame('stringable-message', $payload['message']);
$this->assertSame(LogLevel::ERROR, $payload['level']);
$this->assertSame(['foo' => 'bar'], $payload['context']);
}

public function testNonStringLevelThrowsInvalidArgumentException(): void
{
$stream = fopen('php://temp', 'a+');
$logger = new StderrLogger(LogLevel::DEBUG, $stream);

$this->expectException(InvalidArgumentException::class);
$logger->log([], 'hello');
}

public function testNonStringMessageThrowsInvalidArgumentException(): void
{
$stream = fopen('php://temp', 'a+');
$logger = new StderrLogger(LogLevel::DEBUG, $stream);

$this->expectException(InvalidArgumentException::class);
$logger->log(LogLevel::ERROR, []);
}
}
Loading