Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught ReflectionException: Class log does not exist Laravel 5.2

I am currently trying to clone an existing project of mine from github. After clone I run composer install during the process I receive the following error:

Uncaught ReflectionException: Class log does not exist

I am running Laravel 5.2 on Centos 7.

I have seen references to:

  • Removing spaces within the .env file.
  • Removing the vendor directory & re-installing
  • Removing certain packages required in composer.json

I have:

  • Replaced my .env with the example.env to avoid any custom config errors.
  • I have removed & re-cloned the repo.
  • I have used the default composer.json shipped with Laravel to see if that makes a difference.

None of the above have brought me any joy. I also have the same environment set up on another machine with the application working fine. The only difference here is the machine (working) wasn't cloned from git - it was the initial build environment.

The stack trace I am receiving:

PHP Fatal error:  Uncaught ReflectionException: Class log does not exist in /var/www/html/Acme/vendor/laravel/framework/src/Illuminate/Container/Container.php:736
    Stack trace:
    #0 /var/www/html/Acme/vendor/laravel/framework/src/Illuminate/Container/Container.php(736): ReflectionClass->__construct('log')
    #1 /var/www/html/Acme/vendor/laravel/framework/src/Illuminate/Container/Container.php(631): Illuminate\Container\Container->build('log', Array)
    #2 /var/www/html/Acme/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(674): Illuminate\Container\Container->make('log', Array)
    #3 /var/www/html/Acme/vendor/laravel/framework/src/Illuminate/Container/Container.php(845): Illuminate\Foundation\Application->make('log')
    #4 /var/www/html/Acme/vendor/laravel/framework/src/Illuminate/Container/Container.php(800): Illuminate\Container\Container->resolveClass(Object(ReflectionParameter))
    #5 /var/www/html/Acme/vendor/laravel/framework/src/Illuminate/Container/Container.php(769): Illuminate\Container\Container->getDependenc in /var/www/html/Acme/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 736

Any help would be much appreciated. Thanks in advance.

like image 229
jakehallas Avatar asked Jan 24 '16 17:01

jakehallas


4 Answers

Okay, after many hours of digging, the solution for my problem has been found. The reason why I say my problem is because the Exception is very mis-leading.

Uncaught ReflectionException: Class log does not exist

This exception simply means Laravel tried to log an error but couldn't instantiate Laravel's Log class. This is not due to the Log class going walk-abouts or hiding. This is because Laravel is still going through its boot process & has yet to load the Log class.

So, this exception is thrown because an error occurred during the boot cycle of Laravel - when this error occurred it tried to throw an exception - but it can't throw an exception because the Log class is yet the be loaded. Hence the reason we get a ReflectionException

This has occurred in all versions of Laravel the only reason we have seen the exception thrown in laravel 5.1 <= is because previously Laravel silently discarded the problem & carried on through its boot process - basically, your app would still break however you would not receive the Log class exception.

In my particular case I didn't have the php-mysql extension installed causing Laravel to break during its boot process.

Ultimately, it is incredibly difficult to debug what you may have done wrong due to the error been very mis-leading.

I hope this helps someone!

EDIT: On how to debug this error and find out WHICH missing extension/component is actually causing the problem take a look at @Ben Johnson's answer.

like image 142
jakehallas Avatar answered Nov 14 '22 01:11

jakehallas


In your .env file make sure you have no spaces for values

for example this is allowed

DB_USERNAME=homestead

this is not allowed

DB_USERNAME=home stead

you can wrap the value in quotes if you have spaces.

DB_USERNAME="home stead"

really wish they used json for the .env file, maybe we should request that feature

like image 41
Richard Torcato Avatar answered Nov 14 '22 02:11

Richard Torcato


The underlying error is revealed by modifying vendor/laravel/framework/src/Illuminate/Container/Container.php and placing the following at the top:

<?php

namespace {
    use Monolog\Logger as Monolog;
    class log extends Illuminate\Log\Writer {
       function __construct()
       {
            $this->monolog = new Monolog("local");
       }
    }
}

//Add curly-braces around the original content, like so:

namespace Illuminate\Container {
    //All original code in this file goes here.

    //...
}

(Credit to https://laracasts.com/discuss/channels/general-discussion/class-log-does-not-exist/replies/160902 for this idea.)

To add to the list of root-causes for this message, defining a closure in a configuration file and calling php artisan config:cache subsequently will cause this (at least in Laravel 5.1). Solution for this manifestation: don't define closures in Laravel configuration files, per https://github.com/laravel/framework/issues/9625 .

like image 34
Ben Johnson Avatar answered Nov 14 '22 01:11

Ben Johnson


Remove file bootstrap/cache/config.php . This file may not be displayed on windows, if so, use double commander for example. Definitelly will work!

EDIT:

It may be caused by caching .env file, if it's your case, try to remove bootstrap/cache/config.php

like image 15
Epsilon47 Avatar answered Nov 14 '22 02:11

Epsilon47