Uncaught ReflectionException: Class log does not exist Laravel 5.2

时光怂恿深爱的人放手 提交于 2019-12-17 06:07:19

问题


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.


回答1:


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!




回答2:


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




回答3:


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 .




回答4:


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




回答5:


I was noticing the same behavior after adding a few lines to my .env files. Spaces are not allowed without quotes, and so this can be fixed like:

APP_YOUR_NAME="A value with some spaces"




回答6:


In my case, the missing of PDO extension was the problem. After installing it, the problem was fixed.




回答7:


EDIT::

Because I wasn't satisfied with the rather clumsy debugging and reworking paths, etc to get the VM running smoothly, I reflected on the process and reinstalled the vagrant box laravel/homestead (virtualbox 1.0.1)

For me the issue potentially stemmed from a missing comma in config/app.php. The missing comma likely halted the compilation process and spit out the Uncaught ReflectionException: Class log does not exist error.


This is not a direct answer, but serves more as a guide post to those who venture into this abyss of silent errors

System: macOS Sierra

Vagrant: 1.9.1 (Latest Version at time of writing)

VM: laravel/homestead (virtualbox 0.4.0)

Laravel Version: 5.1.*

PHP: 7.0.*


After repeated attempts to resolve the issue which include:

  1. rewriting, removing .env for potential parse issues
  2. php7.1 and the mcrypt / mbstring issues
  3. installing composer suggested packages
  4. updating homestead.rb
  5. PATH related issues

Critically, (for me) it seemed it was the virtualbox version in the initial set up:

vagrant box add laravel/homestead

Instead try supplying the version number like so:

vagrant box add laravel/homestead --box-version 0.4.0


misc:

I have attempted and failed with the following laravel/homestead virtualbox versions:

  • 1.0.1 (default)

and laravel/homestead-7:

  • 0.2.1



回答8:


I was typing a CMD in the CLI and accidentally typed it in the app config file. I had to follow this procedure in order to find the problem.

Solution, how to find problem:

  • make a backup copy of config folder. try to delete one config at a
  • time and try to run composer/artisan command that failed. When you
  • find file that failed - search for a row that can possibly fail.
  • Reason why it is failed is: You use class constant that is not loaded
  • yet. You use class/function that is not loaded yet. etc

https://laracasts.com/discuss/channels/general-discussion/class-log-does-not-exist?page=2




回答9:


Also this is due to a syntax error in some file in the conf/ directory or .env file. In my case I got this error because I forget to put ::class at the end of the line when adding a service provider and facade, to the array providers and alises in conf/app.php file. I corrected this and the error disappeared.




回答10:


This problem is usually caused by spaces between words in the .env file. Make sure if you have something like

SITE_DESCRIPTION = Social Network for dogs

you replace it with

SITE_DESCRIPTION = 'Social Network for dogs'



回答11:


Uncaught ReflectionException: Class log does not exist

This Error Comes laravel 5.2 & > versions:

My mistakes are:

Tips1: when You missing (;) end of your code in config File

Other syntax Error This error comes.

My Error code:

<?php
return [
'data' => [
    'common' => [
        'AuthKey1' =>  "17086...........9a87a1",
        'AuthKey2' =>  "17086...........9a87a1",
        'AuthKey3'   =>  "17043...........59969531",
    ],
]
]

Correct code: Missing (;) End of return Array

<?php
return [
'data' => [
    'common' => [
        'AuthKey1' =>  "17086...........9a87a1",
        'AuthKey2' =>  "17086...........9a87a1",
        'AuthKey3'   =>  "17043...........59969531",
    ],
]
];



回答12:


This error may be caused by an error in one of the config files. To locate which file is causing it, change the function loadConfigurationFiles in /vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadConfiguration.php to:

protected function loadConfigurationFiles(Application $app, RepositoryContract $repository)
{
    foreach ($this->getConfigurationFiles($app) as $key => $path) {
        var_dump('loading key: ' . $key . ' -- path: ' . $path);
        $repository->set($key, require $path);
    }
}

Run php artisan and the last loading "key" is the config file causing the error. Correct it and don't forget to remove the your var_dump command... Good luck.




回答13:


I had the same issue and none of the solutions here worked out

I've found that there's an exception of permission denied to config folder

After fixing the permission everything worked!


How to figure it?

Put a break-point in vendor/laravel/framework/src/illluminate/Foundation/Http/Kernel.php

line 101 inside the catch of the handle function.

This is how I've found out about the permission denied.




回答14:


When composer updating, i think laravel makes some validations.

  • Install "unzip" package("sudo apt install unzip") if not installed.
  • Check all required extensions such as "mysql", "json", "curl", "xml", "zip", etc. and install("sudo apt install php7.1-mysql") if not installed.
  • Check your file/folder permissions.
  • Check your ".env" file.
  • Check your "config" files.
  • Run "composer update" again.

Installing unzip works for me. Good Luck :)




回答15:


In my case, I had used the route() method in a config file. Obviously, this method does not work because these files don't use the Illuminate Helper, they are simple .php with data.

The configuration files are read before instantiating the Log class, which causes the error that Jakehallas explains very well.




回答16:


Yes as said by @jakehallas . Its not related to what is showing in exception.

Actually if there is any dababase issue or other config varilables issue causes this.

Actually when i tried to change something in database.php i just duplicated same file as database-copy.php. I didn't execpt this causes issue.

After gone through what i have done before i just removed this database-copy.php file its working fine..

Thanks ...




回答17:


Solved this issue after removing space in the .env file (view .env in sublime because vi editor doesn't show space at end of the .env file.) and run below commands.

Commands:

php artisan config:clear
php artisan cache:clear
composer dump-autoload
php artisan clear-compiled



回答18:


I tried a lot of solutions provided here and elsewhere. It did not work for me. I solved it by manually removing /var/www/html/bootstrap/cache/compiled.php and updating the autoload files:

rm /var/www/html/bootstrap/cache/compiled.php
composer dump-autoload



回答19:


This type of error when you missed any of the dependencies of your project. Runphp composer.phar update




回答20:


Run php artisan config:cache solved it for me.

Or if you are on shared hosting without access to the Terminal/CMD, add this to your Routs.php file:

Route::get('/config-cache', function() {
    Artisan::call('config:cache');
    return '<h1>Config cache cleared successfully</h1>';
});

Then go to yourdomain.com/config-cache to run this script.




回答21:


Since no one has mentioned orchestra/testbench yet I thought I'd just add the solution to my problem.

I was writing a Laravel package when I got this error in one of my unit tests. I was using orchestra/testbench to enable testing of Laravel-specific parts of the code.

The problem was simply that the test class was extending PHPUnit\Framework\TestCase instead of Orchestra\Testbench\TestCase.




回答22:


I had some negative value like

BUSINESS_RECONCILIATION_DAYS=-8

I needed to wrap

BUSINESS_RECONCILIATION_DAYS="-8"



回答23:


In my case, a comma is missing in my config file (config/app.php) after I merged my branch to master.

it was a syntax error, but didn't display it.



来源:https://stackoverflow.com/questions/34978828/uncaught-reflectionexception-class-log-does-not-exist-laravel-5-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!