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

Asked

Viewed 472 times

1

I tried using monolog in my application, however, after the installation done according to the documentation, i try to import and use, 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 only used the code above.

  • Using Composer or some other autoload tool?

  • @Andersoncarloswoss I use Composer

  • And the autoload file was inserted into the project at some point?

  • @Andersoncarloswoss Yes, it’s inside the 'Vendor' folder'

  • Inserted in order to give include or require in this file. Excuse me if you know how to use Composer and these questions seem basic, but is that the error seems to be in PHP can not load the class and the absence of include would make perfect sense.

  • It may be that the basic help me, because I don’t use much php, so I don’t know much. No, the autoload file was not loaded, I figured that only use the use Monolog\Logger; was enough to make it work

  • Now it worked. Put an answer telling me to perform a require on autoload. @Andersoncarloswoss

Show 2 more comments

1 answer

4


As discussed in the comments, seeking to better understand the problem, the lack of file loading autoload Composer makes PHP not know how to find the classes. Composer does magic, but not so much. After installation, there must be a directory vendor 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 archive vendor/autoload.php is the Composer standard, which makes all the magic happen.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.