Laravel's log loggs empty errors - laravel

I'm attempting to deploy my Laravel application, however, I'm currently encountering difficulties. The site doesn't work properly, the page name is correctly displayed in the tab and the alt text of my loader.gif is displayed, however, nothing at all happens. Attempting to tackle this problem, I found out that Laravel logs empty errors:
-- needed pastebin since SO told me my code wasn't properly formatted (despite the code being perfect as it is) -- https://pastebin.com/S9vBg5aW
just like that. Each time I re-access the webpage the errors are expanded by a certain number of errors. I only recently upgraded from Laravel 5.5 to 5.6.

Laravel 5.6 comes with different channels for logging.
in your .env
LOG_CHANNEL=stack
create a file in config/logging.php
paste the below given code.
<?php
return [
'default' => env('LOG_CHANNEL', 'stack'),
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single'],
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
],
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'days' => 7,
],
'slack' => [
'driver' => 'slack',
'url' => env('LOG_SLACK_WEBHOOK_URL'),
'username' => 'Laravel Log',
'emoji' => ':boom:',
'level' => 'critical',
],
'syslog' => [
'driver' => 'syslog',
'level' => 'debug',
],
'errorlog' => [
'driver' => 'errorlog',
'level' => 'debug',
],
],
];
for More informations
https://laravel.com/docs/5.6/logging
Hope this helps

Related

Laravel stack with custom channel not working

I'm running Laravel 6 and I'm trying to log into default stack file and new relic log.
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['daily'],
'ignore_exceptions' => false,
],
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'days' => 5,
],
'newrelic' => [
'driver' => 'custom',
'via' => NewRelicLogger::class,
]
]
Both env LOG_CHANNEL=stack and LOG_CHANNEL=newrelic are work normally.
When I update the stack's channels to 'channels' => ['daily', 'newrelic'] and env use LOG_CHANNEL=stack, it only logs to the file and my custom channel "newrelic" doesn't log anything.

How to prevent laravel changing folder owner

Now and then we face a destructive issue within a Laravel project:
Storage log file
production.ERROR: Unable to create lockable file: /var/www/html/storage/framework/cache/data/... Please ensure you have permission to creto create files in this location.
#0 /var/www/html/vendor/laravel/framework/src/Illuminate/Filesystem/LockableFile.php(43)
When the issue happens ls -l gives
drwxrwsr-x+ 2 apache apache 4096 Sep 2 14:36 logs
To solve the issue we run sudo chown -R ec2-user:apache logs/ which gives
drwxrwsr-x+ 2 ec2-user apache 4096 Sep 2 14:36 logs
But this is a manual fix...
Therefore, I would like to ask:
A) How to prevent the file system suddenly changing owner which breaks the coding?
B) Alternatively how to trigger production error notifications within a Laravel project to be warned about such issues?
Answer to your second query, you can use either of them for error reporting:
sentry
flare
busnag
All the exceptions are handled by App\Exceptions\Handler class and in this class, there is a method named register you can configure any of the above tools in there so that exceptions can be reported there. Else you can build your custom report method like sending an email to you with an exception message or sending a slack notification.
To fix this issue permanently you have to give the right permission in the code so go to config/logging.php you will find something like below:
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['daily'],
'ignore_exceptions' => false,
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
],
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'days' => 14,
],
...
],
add 'permission' => 0775, to the channels used, so it will be like below (I have added it to single and daily channels):
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['daily'],
'ignore_exceptions' => false,
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'permission' => 0775,
],
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'days' => 14,
'permission' => 0775,
],
...
],
This fix Tested on Laravel 6

Integration laravel 7 log to slack

I tried to integration error log laravel to slack notification. But when i tested to send log it can't send massage to slack. I followed this tutorial https://panjeh.medium.com/send-laravel-6-log-to-slack-notification-573a6d95a14e. And I've tested on route too
Here is it the config.php
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single','slack'],
'ignore_exceptions' => false,
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
],
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'days' => 14,
],
'slack' => [
'driver' => 'slack',
'url' => env('LOG_SLACK_WEBHOOK_URL'),
'username' => 'Laravel Log',
'emoji' => ':boom:',
'level' => 'warning',
],
Here is it the route to test
Route::get('slack', function() {
Log::error('Test');
return 'Slack notif';
});
I've got the url too and have it put in .env LOG_SLACK_WEBHOOK_URL=
You can change default log channels to slack.So you have to set
LOG_CHANNEL=stack
Also you can specify channels. Instead of changing channels
Log::channel('slack')->inf("test");
or you can do
Log::stack(['daily', 'slack'])->info("test");
or you can specify channels inside daily array.So that no need to change anythink
'stack' => [
'driver' => 'stack',
'channels' => ['daily', 'slack'],
'ignore_exceptions' => false,
],

Stream current log to storage folder

I am running my Laravel 7 application for testing purposes on a shared host.
My logging.php configuration looks like the following:
<?php
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;
return [
'default' => env('LOG_CHANNEL', 'single'),
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['daily'],
'ignore_exceptions' => false,
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'error',
],
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'days' => 14,
],
'slack' => [
'driver' => 'slack',
'url' => env('LOG_SLACK_WEBHOOK_URL'),
'username' => 'Laravel Log',
'emoji' => ':boom:',
'level' => 'critical',
],
'papertrail' => [
'driver' => 'monolog',
'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' => 'debug',
],
'errorlog' => [
'driver' => 'errorlog',
'level' => 'debug',
],
],
];
I only get the log file from the day before in my storage folder. However, I would like to get the logs that are created when the application runs, like a log stream.
Any suggestions how to do this?
I appreciate your replies!
so far, I am new to logs too. The two lines below can be added to any controller
use Log; #on top along with the other 'use' statements
this 2nd line would be inserted as needed:
Log::info('File: '. __FILE__. ' This would get logged on file: storage/laravel.log');
The result is appended onto laravel.log and would be similar to:
[2020-07-11 05:08:38] local.INFO: File: C:\<someFolders>\laravel\vendor\laravel\framework\src\Illuminate\Routing\Route.php This would get logged on file: storage/laravel.log
'hope it helps.

Laravel Logging - Monolog stderr ignores logLevel

I'm using laravel 5.7 in a docker container.
My goal is to write error-log directly to stderr instead of laravel.log
logging.php
<?php
return [
'default' => env('LOG_CHANNEL', 'stack'),
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['stderr'], //before it was stderr
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'error',
],
//...
'stderr' => [
'driver' => 'monolog',
'handler' => StreamHandler::class,
'level' => 'error',
'with' => [
'stream' => 'php://stderr',
],
],
//...
],
];
The log target itself is working - laravel.log is empty - but it seems like the log level is ignored. In the docker logs I get also the debug messages
docker-compose logs --tail=100 container
...
ESC[36mcontainer_1 |ESC[0m [2019-07-01 09:57:51] local.DEBUG: ...
...
If I just change channels (stack) from stderr to single the logs (but only error level) are appended correctly to laravel.log
Put the 'level' within 'with' prop, since the with prop will be passed to the StreamHandler constructor
'stderr' => [
'driver' => 'monolog',
'handler' => StreamHandler::class,
'with' => [
'stream' => 'php://stderr',
'level' => 'error',
],
],

Resources