Error using monolog: PHP Fatal error: Class 'Monolog \ Logger' not found

0

I tried using monolog in my application, however, after the installation was done according to documentation , I try to import and use it, but there is an error message stating that it could not be found.

<?php

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

// Create the logger
$logger = new Logger('my_logger');

I just used the above code.

    
asked by anonymous 17.02.2017 / 10:59

1 answer

1

As discussed in the comments, trying to better understand the problem, the lack of loading the autoload of Composer file causes PHP to not know how to fetch the classes. Composer makes magic, but not so much. After installation, there should be a vendor directory in your application, with the project dependencies. Before using them, insert the file autoload , at the beginning of each file:

<?php

require __DIR__ . '/vendor/autoload.php';

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

// Create the logger
$logger = new Logger('my_logger');

The vendor/autoload.php file is the default for Composer, which makes all the magic happen.

  

Note : autoload must be included once for each request handled in PHP. That is, if your application requests multiple PHP files, require autoload must be present in all of them. If it is based on some architecture, such as MVC, where all the requests are handled in just one file, all you have to do is simply put in it to be accessible throughout the project.

    
17.02.2017 / 14:19