add logger

This commit is contained in:
David Remer 2020-07-01 13:03:21 +03:00
parent 3bd887aaa1
commit 8112a9bc83
9 changed files with 162 additions and 2 deletions

View file

@ -0,0 +1,4 @@
/vendor/
composer.lock
phpunit.xml
.phpunit.result.cache

View file

@ -0,0 +1,13 @@
{
"name": "inpsyde/woocommerce-logging",
"type": "inpsyde-module",
"require": {
"dhii/module-interface": "0.2.x-dev",
"psr/log": "^1.1"
},
"autoload": {
"psr-4": {
"Inpsyde\\Woocommerce\\Logging\\": "src/"
}
}
}

View file

@ -0,0 +1,13 @@
<?php declare(strict_types=1);
/**
* phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration
*/
namespace Inpsyde\IZettle\Logging;
use Psr\Container\ContainerInterface;
return [
];

View file

@ -0,0 +1,9 @@
<?php declare(strict_types=1);
namespace Inpsyde\Woocommerce\Logging;
use Dhii\Modular\Module\ModuleInterface;
return function (): ModuleInterface {
return new WoocommerceLoggingModule();
};

View file

@ -0,0 +1,23 @@
<?php declare(strict_types=1);
/**
* phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration
*/
namespace Inpsyde\Woocommerce\Logging;
use Inpsyde\Woocommerce\Logging\Logger\NullLogger;
use Inpsyde\Woocommerce\Logging\Logger\WooCommerceLogger;
use Psr\Container\ContainerInterface;
return [
'woocommerce.logger.woocommerce' => function (ContainerInterface $container) {
if (!class_exists(\WC_Logger::class)) {
return new NullLogger();
}
return new WooCommerceLogger(
wc_get_logger()
);
},
];

View file

@ -0,0 +1,22 @@
<?php declare(strict_types=1);
/**
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
*/
namespace Inpsyde\Woocommerce\Logging\Logger;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;
class NullLogger implements LoggerInterface
{
use LoggerTrait;
/**
* @inheritDoc
*/
public function log($level, $message, array $context = [])
{
}
}

View file

@ -0,0 +1,44 @@
<?php declare(strict_types=1);
namespace Inpsyde\Woocommerce\Logging\Logger;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;
/**
* Class WooCommerceLogger
* WooCommerce includes a logger interface, which is fully compatible to PSR-3,
* but for some reason does not extend/implement it.
*
* This is a decorator that makes any WooCommerce Logger PSR-3-compatible
*
* @package Inpsyde\IZettle\Logging\Logger
*/
class WooCommerceLogger implements LoggerInterface
{
use LoggerTrait;
/**
* @var \WC_Logger_Interface
*/
private $wcLogger;
public function __construct(\WC_Logger_Interface $wcLogger)
{
$this->wcLogger = $wcLogger;
}
/**
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
*
* @inheritDoc
*/
public function log($level, $message, array $context = [])
{
if (!isset($context['source'])) {
$context['source'] = 'izettle-woocommerce';
}
$this->wcLogger->log($level, $message, $context);
}
}

View file

@ -0,0 +1,30 @@
<?php declare(strict_types=1);
namespace Inpsyde\Woocommerce\Logging;
use Dhii\Container\ServiceProvider;
use Dhii\Modular\Module\ModuleInterface;
use Interop\Container\ServiceProviderInterface;
use Psr\Container\ContainerInterface;
class WoocommerceLoggingModule implements ModuleInterface
{
/**
* @inheritDoc
*/
public function setup(): ServiceProviderInterface
{
return new ServiceProvider(
require __DIR__.'/../services.php',
require __DIR__.'/../extensions.php'
);
}
/**
* @inheritDoc
*/
public function run(ContainerInterface $container)
{
}
}