Skip to content

Commit

Permalink
Merge pull request #21 from ucloud/feat/dependency-update
Browse files Browse the repository at this point in the history
add psr/log v3 compatibility
  • Loading branch information
wangrzneu authored Oct 11, 2024
2 parents 312f371 + 75207a3 commit e8507f1
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 21 deletions.
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";
}
}

0 comments on commit e8507f1

Please sign in to comment.