Uncaught Error: Class "Monolog\Logger" not found
scott-anderson-wc opened this issue · 3 comments
Monolog version 2
Environment: Rocky 9 Linux, running PHP 8.0.27
As a non-root user, I installed composer and Monolog like these:
composer require monolog/monolog
php composer.phar require monolog/monolog
Both result in:
Info from https://repo.packagist.org: #StandWithUkraine
Cannot use monolog/monolog's latest version 3.4.0 as it requires php >=8.1 which is not satisfied by your platform.
./composer.json has been updated
Running composer update monolog/monolog
Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Writing lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating autoload files
1 package you are using is looking for funding.
Use the `composer fund` command to find out more!
No security vulnerability advisories found
Using version ^2.9 for monolog/monolog
The PHP script I used is this:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
echo "<p>start ";
echo __DIR__.'/mono2.log';
use Monolog\Level;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// Create the logger
$log = new Logger('name');
$log->pushHandler(new StreamHandler(__DIR__.'/mono2.log', Level::Warning));
// add records to the log
$log->warning('Foo');
$log->error('Bar');
// You can now use your logger
$logger->info('My logger is now ready');
echo "<p>done";
?>
And the result is:
GET https://cs.wellesley.edu/~sysadmin/casacct/mono2.php
<p>start /home/sysadmin/public_html/casacct/mono2.log<br />
<b>Fatal error</b>: Uncaught Error: Class "Monolog\Logger" not found in /home/sysadmin/public_html/casacct/mono2.php:14
Stack trace:
#0 {main}
thrown in <b>/home/sysadmin/public_html/casacct/mono2.php</b> on line <b>14</b><br />
I'm sure my error is something very simple, but I've searched Stack Overflow and googled without success. The example is nearly identical to the standard documentation, so I don't think I went wrong there.
Thanks for your help!
You are missing the autoloader. Adding include 'vendor/autoload.php';
should help. That said, you are also using the Level class which is Monolog 3.x only, and you have monolog 2 here as you are not running on PHP 8.1+, so you'll need to tweak the code, see docs for Monolog 2 https://github.com/Seldaek/monolog/blob/2.x/README.md
I wouldn't quite call it expected, it depends on the hosting setup, if the application has no write access to the log directory then sure you need to create the file for it.