0

I'm writing my first Laravel app that also includes Vue. I'm pretty new to both Laravel and Vue so please be gentle ;-) I'm using Laravel 8.4.x and Vue 2.6.12 on Windows 10.

In my very first axios invocation, I'm trying to write of a single record to a database table in a submit method of my Vue component, I'm getting Http status code 500, internal server error. The response in the console says that Laravel doesn't see a controller with the name ToDoController. I have no idea why that would be since I created it properly with php artisan and I can see it in VS Code.

In looking at other posts to try to understand this problem, I see that people recommend looking in the server logs to find out more information. I'm not sure if the logs will have information not found in the response situated in the browser console but I won't know until I look at it. The problem is that I don't know where to look for my log.

According to the Laravel docs, logging is governed by config/logging.php and that file says:

<?php

use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;

return [

    /*
    |--------------------------------------------------------------------------
    | Default Log Channel
    |--------------------------------------------------------------------------
    |
    | This option defines the default log channel that gets used when writing
    | messages to the logs. The name specified in this option should match
    | one of the channels defined in the "channels" configuration array.
    |
    */

    'default' => env('LOG_CHANNEL', 'stack'),

    /*
    |--------------------------------------------------------------------------
    | Log Channels
    |--------------------------------------------------------------------------
    |
    | Here you may configure the log channels for your application. Out of
    | the box, Laravel uses the Monolog PHP logging library. This gives
    | you a variety of powerful log handlers / formatters to utilize.
    |
    | Available Drivers: "single", "daily", "slack", "syslog",
    |                    "errorlog", "monolog",
    |                    "custom", "stack"
    |
    */

    'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['single'],
            'ignore_exceptions' => false,
        ],

        'single' => [
            'driver' => 'single',
            'path' => storage_path('logs/laravel.log'),
            'level' => env('LOG_LEVEL', 'debug'),
        ],

        'daily' => [
            'driver' => 'daily',
            'path' => storage_path('logs/laravel.log'),
            'level' => env('LOG_LEVEL', 'debug'),
            'days' => 14,
        ],

        'slack' => [
            'driver' => 'slack',
            'url' => env('LOG_SLACK_WEBHOOK_URL'),
            'username' => 'Laravel Log',
            'emoji' => ':boom:',
            'level' => env('LOG_LEVEL', 'critical'),
        ],

        'papertrail' => [
            'driver' => 'monolog',
            'level' => env('LOG_LEVEL', 'debug'),
            'handler' => SyslogUdpHandler::class,
            'handler_with' => [
                'host' => env('PAPERTRAIL_URL'),
                'port' => env('PAPERTRAIL_PORT'),
            ],
        ],

        'stderr' => [
            'driver' => 'monolog',
            'handler' => StreamHandler::class,
            'formatter' => env('LOG_STDERR_FORMATTER'),
            'with' => [
                'stream' => 'php://stderr',
            ],
        ],

        'syslog' => [
            'driver' => 'syslog',
            'level' => env('LOG_LEVEL', 'debug'),
        ],

        'errorlog' => [
            'driver' => 'errorlog',
            'level' => env('LOG_LEVEL', 'debug'),
        ],

        'null' => [
            'driver' => 'monolog',
            'handler' => NullHandler::class,
        ],

        'emergency' => [
            'path' => storage_path('logs/laravel.log'),
        ],
    ],

];

If I'm understanding this correctly, that means that errors at DEBUG or above are being written to logs/laravel.log, which I should be seeing in VS Code. But I do not even have a logs directory, let alone a laravel.log file.

Do I need to do something to create these logs in the first place as part of creating a new project? If so, what? If not, why don't I see my log? A server error is significant enough to get written into the log, right?

Also, while we're on the subject of logs, I have a chicken and egg question: does the value of LOG_CHANNEL in the logging.php file get set from the .env file or does the .env file get the value in the logging.php file? In other words, if I want to change my logging behaviour, which one do I alter?

Henry
  • 1,395
  • 1
  • 12
  • 31
  • Logs folder is `storage/logs` – brombeer Dec 05 '20 at 16:27
  • Anything that you read via `env('...')` comes from the `.env` file nothing goes to .env from your configuration. – apokryfos Dec 05 '20 at 16:51
  • Since Laravel 8 the default namespace is not set by default in RouteService Provider so in your routes file, you must use the fully qualified name for the Controller. Probably the route declaration doesn't use the FQCN for the controller hence the error you are getting regarding controller not found – Donkarnash Dec 05 '20 at 16:52
  • Does this answer your question? [Where is the laravel error file located? or Is there a default error log in Laravel?](https://stackoverflow.com/questions/41351923/where-is-the-laravel-error-file-located-or-is-there-a-default-error-log-in-lara) – miken32 Dec 05 '20 at 16:57
  • Or https://stackoverflow.com/questions/37535315/where-are-logs-located – miken32 Dec 05 '20 at 16:58
  • Thanks Mike. I suppose my search for existing answers was not as good as it could have been. – Henry Dec 05 '20 at 17:04
  • @Donkarnash - I strongly suspect you've identified the problem. I'm not up on the new features of Laravel 8. What would my Route look like if I have a FQCN for the controller? I've tried all the variations I can think of but nothing works, including url('app/Http/Controllers/ToDoController') – Henry Dec 05 '20 at 17:19
  • Have provided some sample declaration in answer below – Donkarnash Dec 05 '20 at 17:34

2 Answers2

3

The logs directory is located in the storage directory. So it's storage/logs/laravel.log.

Also, you can choose to log anything you want to using the Log facade. See laravel writing log messages [https://laravel.com/docs/8.x/logging#writing-log-messages]. Anything that is wrapped in the env() function is being pulled from the env file. The second param allows you to set a default if that value is not set or available in the env file. 1

  • Thank you! I didn't realize that logs/laravel.log was within the storage directory. Thanks also for clarifying that the file gets its info from the .env file, not the reverse. Unfortunately, the log doesn't contain anything for my problem but Donkarnash has probably nailed the problem! – Henry Dec 05 '20 at 16:59
0

@Donkarnash - I strongly suspect you've identified the problem. I'm not up on the new features of Laravel 8. What would my Route look like if I have a FQCN for the controller? I've tried all the variations I can think of but nothing works, including url('app/Http/Controllers/ToDoController')

Since Laravel 8 the default namespace App\Http\Controllers is not set in the RouteServiceProvider - which is a welcome change.

So now when defining routes in routes files say routes/web.php FQCN of the controller must be used

use App\Http\Controllers\ToDoController;

Route::get('/todos', [ToDoController::class, 'index']);

//OR without importing the use statement
Route::get('/todos', [\App\Http\Controllers\ToDoController::class, 'index']);

If you want you can also use namespace method for route groups


Route::namespace('App\Http\Controllers')
    ->group(function(){
        Route::get('/todos', 'ToDoController@index');
        Route::get('/todos/{todo}', 'ToDoController@show');
    });

Using FQCN as in either importing use statement or inline also provides benefits of easy navigation and code suggestions in IDE's

To revert back to the old convention and set default namespace you should declare $namespace in RouteServiceProvider



    /**
     * The controller namespace for the application.
     *
     * When present, controller route declarations will automatically be prefixed with this namespace.
     *
     * @var string|null
     */
     protected $namespace = 'App\\Http\\Controllers';
Donkarnash
  • 12,433
  • 5
  • 26
  • 37
  • Don, you totally rock! Thank you for the detailed answer instead of leaving this poor noob to figure out a much shorter response. I have to do a meeting right now but I'm looking forward to using this right after the meeting! – Henry Dec 05 '20 at 18:24
  • I'm sorry to bother you again but I'm trying to write a Route::resource route and I am not finding an example that works in Laravel 8. I've tried several variations but nothing works. Can you point me in the right direction? – Henry Dec 05 '20 at 19:47
  • @Henry Please open a new question with the query. Giving code examples for things unrelated to the question is not appropriate. So just open a new question and mark me a message will definitely help you on that – Donkarnash Dec 05 '20 at 19:55