如何在 slim 3 中使用 monolog?

How to use monolog in slim 3?

在我的 app.php 文件中:

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

class App extends \DI\Brifge\Slim\app
{
 public function configureContainer(ContainerBuilder $builder)
 {
  $dependencies = [
            'logger' => function (Container $container) { 
            $logger = new Monolog\Logger('logger');
            $filename = _DIR__ . '/../log/error.log';
            $stream = new Monolog\Handler\StreamHandler($filename, Monolog\Logger::DEBUG);
            $fingersCrossed = new Monolog\Handler\FingersCrossedHandler(
            $stream, Monolog\Logger::ERROR);
            $logger->pushHandler($fingersCrossed);

            return $logger;
        },
      ];
    }
   }

在我的自定义错误处理程序中:

class CustomErrorHandler
{
 protected $logger;

 public function __construct(Logger $logger)
 {
    $this->logger = $logger;
 }

 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $exception)
 {
    $output = ['success'=>0, 'error'=>"Oops! Something's not right "];

    $this->logger->critical($output);

    return $response->withStatus(500)
                    ->withHeader('Content-Type', 'application/json')
                    ->write(json_encode($output));
 }
}

抛出错误时,显示以下消息:

Fatal error: Uncaught Error: Class 'App\Monolog\Logger' not found in C:\xampp\htdocs\backend\app\App.php:103 Stack trace: #0 [internal function]: App\App->App{closure}(Object(DI\Container)) #1 C:\xampp\htdocs\backend\vendor\php-di\invoker\src\Invoker.php(82): call_user_func_array(Object(Closure), Array) #2 C:\xampp\htdocs\backend\vendor\php-di\php-di\src\DI\Definition\Resolver\FactoryResolver.php(81): Invoker\Invoker->call(Object(Closure), Array) #3 C:\xampp\htdocs\backend\vendor\php-di\php-di\src\DI\Definition\Resolver\ResolverDispatcher.php(58): DI\Definition\Resolver\FactoryResolver->resolve(Object(DI\Definition\FactoryDefinition), Array) #4 C:\xampp\htdocs\backend\vendor\php-di\php-di\src\DI\Container.php(287): DI\Definition\Resolver\ResolverDispatcher->resolve(Object(DI\Definition\FactoryDefinition), Array) #5 C:\xampp\htdocs\backend\vendor\php-di\php-di\src\DI\Container.php(124): DI\Container->resolveDefinition(Object(DI\Definition\FactoryDefinition)) #6 C:\xampp\htdocs\backend\app\App.php(95): DI\Container->get('l in C:\xampp\htdocs\backend\app\App.php on line 103

我在 App.php 文件中包含了语句 use Monolog\Logger;,但出现上述错误。

您忘记在 configureContainer 方法中注册依赖项:

使用Monolog\Logger; 使用 Monolog\Handler\StreamHandler;

class App extends \DI\Bridge\Slim\App
{
    public function configureContainer(ContainerBuilder $builder)
    {
        $dependencies = [
            'logger' => function (Container $container) { 
                $logger = new Monolog\Logger('logger');
                $filename = _DIR__ . '/../log/error.log';
                $stream = new Monolog\Handler\StreamHandler($filename, Monolog\Logger::DEBUG);
                $fingersCrossed = new Monolog\Handler\FingersCrossedHandler(
                $stream, Monolog\Logger::ERROR);
                $logger->pushHandler($fingersCrossed);

                return $logger;
            },
        ];

        // Register definitions
        $builder->addDefinitions($dependencies);
    }
}

还有你在App class声明中有错误,是\DI\Bridge\Slim\App,而不是\DI\Brifge\Slim\app

我明白我哪里错了。 无需包含以下行:

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

在实例化时,我包含了完整的路径。