As mentioned in laravel doc https://laravel.com/docs/8.x/logging i used permission in logging.php configuration as follows
'single' => [
'driver' => 'single',
'tap' => [App\Providers\JsonFormatter::class],
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'permission' => 0777,
],
'daily' => [
'driver' => 'daily',
'tap' => [App\Providers\JsonFormatter::class],
'path' => storage_path('logs/'.php_sapi_name().'/laravel.log'),
'level' => 'debug',
'permission' => 0777,
'days' => 1,
],
then I deleted all previous logs and then stored then logs again but every linked directory has drwxr-xr-x these permissions. Am I doing something wrong here?
Related
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
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,
],
I have my logging set up like this in Laravel:
'default' => env('LOG_CHANNEL', 'stack'),
'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,
'permission' => 0666,
],
This creates logs for each day in storage/logs.
Now, what I want to do, is send only the log messages from my scheduled tasks to a separate log directory altogether. For example, to storage/logs/cron. But I want to keep the other messages in the current setup.
How do I make this happen? I've tried creating a new channel in config but it still logs the messages in the same files.
You can create a new channel in config/logging.php. Ie:
'cronjob' => [
'driver' => 'single',
'path' => storage_path('logs/cronjob.log'),
'level' => 'debug',
],
And then you can use it in your command (or anywhere) adding the channel method:
Log::channel('cronjob')->info('Command started. ');
If you want to log exceptions use a try/catch:
try {
// ...
} catch (\Exception $exception) {
Log::channel('cronjob')->error($exception);
}
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.
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