0

I'm trying to test out the monolog package using composer (full disclosure: this is my first try using composer) and am getting a 500 error when running this code:

<?php

// composer autoloader
require_once 'vendor/autoload.php';


// Shortcuts for simpler usage
use \Monolog\Logger;
use \Monolog\Formatter\LineFormatter;
use \Monolog\Handler\StreamHandler;

// Common logger
$log = new Logger('files');
// Line formatter without empty brackets in the end
$formatter = new LineFormatter(null, null, false, true);
// Debug level handler
$debugHandler = new StreamHandler('debug.log', Logger::DEBUG);
$debugHandler->setFormatter($formatter);
// Error level handler
$errorHandler = new StreamHandler('error.log', Logger::ERROR);
$errorHandler->setFormatter($formatter);
// This will have both DEBUG and ERROR messages
$log->pushHandler($debugHandler);
// This will have only ERROR messages
$log->pushHandler($errorHandler);
// The actual logging
$log->debug('I am debug');
$log->error('I am error', array('productId' => 123));

?>

I see in Dreamweaver that the three 'use' lines in monolog_test.php are highlighted in red:

enter image description here

The error message is:

enter image description here

In my /Applications/MAMP/logs/php_error.log:

[03-Mar-2018 14:10:05 America/Toronto] PHP Fatal error: Class 'Monolog\Logger' not found in /Users/Ross/Dropbox/htdocs/wonderfest/secure/contest/monolog_test.php on line 13

My file system setup looks like this:

enter image description here

I installed Composer globally on my Mac and I know the installation is good because I was able to add some packages using composer require . My composer.json file:

{
    "require": {
        "monolog/monolog": "1.0",
        "phpfastcache/phpfastcache": "^6.1",
        "mpdf/mpdf": "^7.0"
    }
}

And my vendor\autoload.php:

<?php

// autoload.php @generated by Composer

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit348f040c8a2a7d48c0a311fb1af10c08::getLoader();

I guess my question is this: do I have to do anything other than install packages to make their namespaces available?

RossW
  • 215
  • 1
  • 7
  • 16
  • [HTTP 500](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#5xx_Server_Error) is a generic server-side error message. Any time you see this your first step should be to check your error logs for more detail. – ChrisGPT was on strike Mar 03 '18 at 16:41
  • Updated the question with the details from the php_error.log – RossW Mar 03 '18 at 19:12

1 Answers1

1

I updated monolog to the latest stable version and although the 3 'use' lines in Dreamweaver still show up as errors, it works. What threw me was this line on the monolog package page:

Monolog works with PHP 7.0 or above, use Monolog ^1.0 for PHP 5.3+ support.

I thought that meant that I needed to use v1.0 for my PHP 5.6.32, but when I removed that version constraint from the require command everything seems to work - I get the two logs in the same folder as the monolog_test.php file.

My new composer.json (with one additional packages installed):

{
    "require": {
        "monolog/monolog": "^1.23",
        "mpdf/mpdf": "^7.0",
        "phpfastcache/phpfastcache": "^6.1",
        "phpmailer/phpmailer": "^6.0"
    }
}
RossW
  • 215
  • 1
  • 7
  • 16