如何在苗条3中使用monolog?

时间:2017-07-19 05:30:27

标签: monolog 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));
 }
}

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

  

致命错误:未捕获错误:类' App \ Monolog \ Logger'在C:\ xampp \ htdocs \ backend \ app \ App.php中找不到:103堆栈跟踪:#0 [内部功能]:App \ App-> App {closure}(对象(DI \ Container))#1C :\ 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 \ FactoryDe​​finition),Array)#4 C:\ xampp \ htdocs \ backend \ vendor \ php-di \ php-di \ src \ DI \ Container.php(287):DI \ Definition \ Resolver \ ResolverDispatcher-> resolve(Object(DI \ Definition \ FactoryDe​​finition),Array)# 5 C:\ xampp \ htdocs \ backend \ vendor \ php-di \ php-di \ src \ DI \ Container.php(124):DI \ Container-> resolveDefinition(Object(DI \ Definition \ FactoryDe​​finition))#6 C:\ xampp \ htdocs \ backend \ app \ App.php(95):DI \ Container-> get(&#39 ; l在第103行的C:\ xampp \ htdocs \ backend \ app \ App.php中

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

2 个答案:

答案 0 :(得分:0)

您忘记在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班级声明中也有错,它是\DI\Bridge\Slim\App,而不是\DI\Brifge\Slim\app

答案 1 :(得分:0)

我明白我错了。 不需要包括这些行:

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

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