Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add psr/log v3 compatibility #21

Merged
merged 2 commits into from
Oct 11, 2024
Merged
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
18 changes: 18 additions & 0 deletions compatibility/DefaultLoggerPsrLogV1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use Psr\Log\LoggerInterface;
use Psr\Log\AbstractLogger;

class DefaultLoggerPsrLogV1 extends AbstractLogger implements LoggerInterface
{
public function log($level, $message, array $context = [])
{
$line = $level . " " . $message;
if (!empty($context)) {
$line = " " . json_encode($context, JSON_PRETTY_PRINT);
}
echo $line . "\n";
}
}

class_alias("DefaultLoggerPsrLogV1", "UCloud\Core\Logger\DefaultLogger");
21 changes: 21 additions & 0 deletions compatibility/DefaultLoggerPsrLogV3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Psr\Log\LoggerInterface;
use Psr\Log\AbstractLogger;

class DefaultLoggerPsrLogV3 extends AbstractLogger implements LoggerInterface
{
public function log(
$level,
string|\Stringable $message,
array $context = []
): void {
$line = $level . " " . $message;
if (!empty($context)) {
$line = " " . json_encode($context, JSON_PRETTY_PRINT);
}
echo $line . "\n";
}
}

class_alias("DefaultLoggerPsrLogV3", "UCloud\Core\Logger\DefaultLogger");
14 changes: 14 additions & 0 deletions compatibility/DisabledLoggerPsrLogV1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use Psr\Log\LoggerInterface;
use Psr\Log\AbstractLogger;

class DisabledLoggerPsrLogV1 extends AbstractLogger implements LoggerInterface
{
public function log($level, $message, array $context = [])
{
// Do nothing
}
}

class_alias("DisabledLoggerPsrLogV1", "UCloud\Core\Logger\DisabledLogger");
17 changes: 17 additions & 0 deletions compatibility/DisabledLoggerPsrLogV3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use Psr\Log\LoggerInterface;
use Psr\Log\AbstractLogger;

class DisabledLoggerPsrLogV3 extends AbstractLogger implements LoggerInterface
{
public function log(
$level,
string|\Stringable $message,
array $context = []
): void {
// Do nothing
}
}

class_alias("DisabledLoggerPsrLogV3", "UCloud\Core\Logger\DisabledLogger");
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
"authors": [
{
"name": "ucloud",
"email": "yufei.li@ucloud.cn"
"email": "renzheng.wang@ucloud.cn"
}
],
"require": {
"php": ">=5.6",
"guzzlehttp/guzzle": "^6.2.1|^7.0",
"psr/log": "^1.1"
"psr/log": "^1.1 || v3.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
Expand Down
20 changes: 9 additions & 11 deletions src/Core/Logger/DefaultLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

namespace UCloud\Core\Logger;

use Psr\Log\LoggerInterface;
use Psr\Log\AbstractLogger;
use ReflectionMethod;

class DefaultLogger extends AbstractLogger implements LoggerInterface
{
public function log($level, $message, array $context = [])
{
$line = $level . " " . $message;
if (!empty($context)) {
$line = " " . json_encode($context, JSON_PRETTY_PRINT);
}
echo $line . "\n";
$logMethodReflection = new ReflectionMethod("Psr\Log\AbstractLogger", "log");
if (version_compare(PHP_VERSION, '8.0.0', '<')) {
include "compatibility/DefaultLoggerPsrLogV1.php";
} else {
if ($logMethodReflection->hasReturnType()) {
include "compatibility/DefaultLoggerPsrLogV3.php";
} else {
include "compatibility/DefaultLoggerPsrLogV1.php";
}
}
17 changes: 9 additions & 8 deletions src/Core/Logger/DisabledLogger.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?php


namespace UCloud\Core\Logger;

use Psr\Log\AbstractLogger;
use Psr\Log\LoggerInterface;
use ReflectionMethod;

class DisabledLogger extends AbstractLogger implements LoggerInterface
{
public function log($level, $message, array $context = [])
{
// Do nothing
$logMethodReflection = new ReflectionMethod("Psr\Log\AbstractLogger", "log");
if (version_compare(PHP_VERSION, '8.0.0', '<')) {
include "compatibility/DisabledLoggerPsrLogV1.php";
} else {
if ($logMethodReflection->hasReturnType()) {
include "compatibility/DisabledLoggerPsrLogV3.php";
} else {
include "compatibility/DisabledLoggerPsrLogV1.php";
}
}
Loading