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

How to use monolog in slim3 framework correctly?? #1025

Closed
ayushav12 opened this issue Jul 18, 2017 · 3 comments
Closed

How to use monolog in slim3 framework correctly?? #1025

ayushav12 opened this issue Jul 18, 2017 · 3 comments

Comments

@ayushav12
Copy link

ayushav12 commented Jul 18, 2017

In my app.php file:

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;
            },
  ];
 }
}

In my custom error handler:

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));
	}
}

When an error is thrown, following message is displayed:

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

Can anyone help me out as soon as possible??

@ayushav12 ayushav12 changed the title How to monolog in slim3 framework correctly?? How to use monolog in slim3 framework correctly?? Jul 18, 2017
@Seldaek
Copy link
Owner

Seldaek commented Jul 18, 2017

Please ask for slim support using slim channels. I can not help as I am not a slim user...

@Seldaek Seldaek closed this as completed Jul 18, 2017
@Seldaek
Copy link
Owner

Seldaek commented Jul 18, 2017

That said the error message looks like you are missing a use Monolog\Logger; statement wherever you're using Monolog\Logger at App.php line 103.

@ayushav12
Copy link
Author

I have included the line use Monolog\Logger; statement at the beginning of App.php . Then also I get the above error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants