Laravel log file based on date - php

By default laravel saves the log file to a single log file called laravel.log located in /storage/logs/laravel.log
my question is how can i get a new log file everyday and store the log files like /storage/logs/laravel-2016-02-23.log for the current date, so i need everyday a new log file saved to /storage/logs/
i think we can do that by extending the default Illuminate\Foundation\Bootstrap\ConfigureLogging bootstraper class but i'm not sure how i can do that
i would really appreciate it if anyone could help me.
Thanks in advance.

In the version of Laravel 5.6 that I am using, the configuration file for logging is config/logging.php
There you will find the following section
'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,
],
...
]
Change the line
'channels' => ['single'],
into
'channels' => ['daily'],
Then it will be like:
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['daily'],
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
],
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'days' => 7,
],
...
]
It will create log files for each day in the format laravel-2018-08-13.log in the logs directory.
The log directory will be like
Previously
After applying rotation configuration the directory is having the log file created for the current date (as circled one which is created for today 2018-08-13).

It's actually a lot simpler than that. In your config/app.php you'll see the line:
'log' => 'single',
closer to the bottom of the file. Laravel by default uses the single method, which stores all errors in a single, expanding file. If you change this line to:
'log' => 'daily',
it will tell Laravel that you'd prefer to have multiple logs, each suffixed with the date of the when the error occurs.
There's a few other methods available, so be sure to check out the official documentation for more info.
This answer is for Laravel 5.2, which is the version specified in the original question. In never versions of Laravel, the Logging config has been moved to it's own config file, as seen by #ShanthaKumara's answer (https://stackoverflow.com/a/51816907/3965631). Please do not suggest edits to change this answer to reflect the new version.

just open .env file and change
LOG_CHANNEL=stack
to
LOG_CHANNEL=daily
then run the command
php artisan config:cache
now i think your problem will solve.

The laravel on daily logging is fine, but on 1st year, you will have 365 files of laravel.log. My approach is to do the following on the 'path' of the 'single' channel.
storage_path('logs/' . date("Y") . '/' . date("m") . '/' . date("d") . '/' . 'laravel.log')
This way you have organize the logs by each year/month/day.
2021/
06/
01/
laravel.log

To generate a new file for everyday in Laravel, all you need to do is change 'channels' value in the \config\logging.php file from 'single' to 'daily'.
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['daily'],
'ignore_exceptions' => false,
],

Related

Rollbar is not seen by Laravel 8.0

I have installed the Rollbar 7.0 into Laravel 8.0. PHP version is 7.4
I am trying to send a test exception message using a simple Console command but that sends me nothing.
My configs are the following:
config/app.php:
return [
'providers' => [
Rollbar\Laravel\RollbarServiceProvider::class
...
]
config/logging.php:
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['other', 'rollbar'],
'ignore_exceptions' => false,
],
'rollbar' => [
'driver' => 'monolog',
'handler' => MonologHandler::class,
'access_token' => env('ROLLBAR_TOKEN'),
'level' => env('ROLLBAR_LEVEL'),
'enabled' => true,
'environment' => env('ROLLBAR_ENVIRONMENT'),
]
....
config/services.php (but seems to be that it doesn't work)
'rollbar' => [
'access_token' => env('ROLLBAR_TOKEN'),
'environment' => env('ROLLBAR_ENVIRONMENT'),
'level' => env('ROLLBAR_LEVEL')
],
app.env:
ROLLBAR_TOKEN=real_token
ROLLBAR_LEVEL=debug
ROLLBAR_ENVIRONMENT=backend_test
And the console command itself has the following view:
public function handle()
{
// Rollbar::init([
// 'driver' => 'monolog',
// 'handler' => MonologHandler::class,
// 'access_token' => env('ROLLBAR_TOKEN'),
// 'level' => env('ROLLBAR_LEVEL'),
// 'enabled' => true,
// 'environment' => env('ROLLBAR_ENVIRONMENT'),
// ]);
try{
$x = 4/0;
} catch(\Exception $exception) {
Rollbar::error('caught demo exception', ["details" => $exception->getMessage()]));
Rollbar::flush();
exit(1);
}
}
So when it is like this, the rollbar stays silent. But if I uncomment the initialisation, that works well, sending a debug message to the rollbar.
That doesn't work all over the project too.
Could you please advice me, what could I do here in order to make it work globally with initialising in every file?
upd: I've also cleared config cache and tried to make a rollbar as a default
Laravel in app/logging.php has a default channel configuration. Normally "default" should mean that there are some other working channel too but here, somehow it the meaning is like "the only used channel". Or I just do not fully understand how should it work. So my rollbar channel seems to be overriden by the another "default" one, that is why the system doesn't use it. So the solution is to switch the default channel:
'default' => env('LOG_CHANNEL', 'stack'),
when the rollbar is included to stack channel or just
'default' => env('LOG_CHANNEL', 'rollbar'),
when it is not.

How to create and use a custom daily log file in Laravel?

In Laravel I'm defining a custom log file in /config/logger.php:
'mycustomlog' => [
'driver' => 'stack',
'path' => storage_path('logs/mycustomlog.log'),
'level' => 'info',
],
Here's my stack driver for context:
'stack' => [
'driver' => 'stack',
'channels' => ['daily', 'syslog'],
'ignore_exceptions' => false,
],
and I'm calling it as follows:
Log::channel('mycustomlog')->info($e);
What I expect to happen:
A (daily) log file is created and the exception is logged. I.e mycustomlog-2019-11-07.log
What actually happens:
No log file is created, but the following error is logged to laravel.log:
[2019-11-07 10:25:31] laravel.EMERGENCY: Unable to create configured logger. Using emergency logger. {"exception":"[object] (ErrorException(code: 0): Undefined index: channels at /var/www/vendor/laravel/framework/src/Illuminate/Log/LogManager.php:232)
SOLUTION:
'mycustomlog' => [
'driver' => 'daily',
'channels' => ['syslog'],
'path' => storage_path('logs/mycustomlog.log'),
'level' => 'info',
],
You will need to have channels in the config logger.php see here. The point of the stack driver is to report to multiple channels.
'mycustomlog' => [
'driver' => 'stack',
'channels' => ['daily'],
'path' => storage_path('logs/mycustomlog.log'),
'level' => 'info',
],
If you don't want it to be in the stack you can have your config to point to the single driver like this
'mycustomlog' => [
'driver' => 'single',
'path' => storage_path('logs/mycustomlog.log'),
'level' => 'info',
]
Then call it the same way you tried on your own.
Log::channel('mycustomlog')->info($e);

Getting a list of all text that needs to be translated using Yii2 DbMessageSource

I am using Yii2 to work on a project that needs to be translated into various languages. I am using the advanced template and set up my common/main.php like so
return [
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
'language' => 'en',
'sourceLanguage' => 'en',
'components' => [
'i18n' => [
'translations' => [
'app' => [
'class' => 'yii\i18n\DbMessageSource',
'sourceLanguage' => 'en',
],
],
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
],
'as beforeRequest' => [
'class' => 'common\components\CheckLanguage',
],
];
I have added the required database tables source_message and message. Now each text in the website that I need to have translated I use the Yii::t($category,$message) function.
My question is; How can I get a list of all text in my website that needs to be translated into the database? Do I have to manually scan my site for all Yii::t($category,$message) function calls?
Thanks in advance for your time and input
There is console command provided for this scan functionality.
./yii message
See the documentation in the Guide.
Generate configuration file for the scanner:
./yii message/config-template your/path/to/saving/config.php
Adjust the newly created config.php to your needs.
Run the command:
./yii message path/to/config.php

How to print messages on console in Laravel?

How do laravel print out some string on console when running php artisan serve?
I tried Log::info but it isn't working.
It's very simple.
You can call it from anywhere in APP.
$out = new \Symfony\Component\Console\Output\ConsoleOutput();
$out->writeln("Hello from Terminal");
Try with
error_log('message here.');
Read More
If You ant add LOG
Log::info('message');
If LOG with an array
Log::info(json_encode($array));
Import Illuminate\Support\Facades\Log;
Laravel 5.6 simplified this because you now have a logging.php config file you could leverage.
The key thing to know is that you want to output to stdout and php has a stream wrapper built-in called php://stdout. Given that, you could add channel for that wrapper. You would add the stdout "channel" to your channels that you will be logging to.
Here's how the config will basically look:
<?php
return [
'default' => env('LOG_CHANNEL', 'stack'),
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single','stdout'],
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
],
'stdout' => [
'driver' => 'monolog',
'handler' => StreamHandler::class,
'with' => [
'stream' => 'php://stdout',
],
],
];
I have more information here - Laravel 5.6 - Write to the Console
You've to config where laravel to store the logs. Default Log::info() put the log in the log file not the console. you can use tail -f logpath to see the log.
You can call info() method
$this->info("Your Message");

Yii2 - Multi language

I'm trying to set up the website's frontend translation using the i18l thing. Here is my i18l.php file placed on frontend/config
<?php
return [
'sourcePath' => 'frontend',
'languages' => ['en-US', 'pt-BR'] , //Add languages to the array for the language files to be generated.
'translator' => 'Yii::t',
'sort' => false,
'removeUnused' => false,
'only' => ['*.php'],
'except' => [
'.svn',
'.git',
'.gitignore',
'.gitkeep',
'.hgignore',
'.hgkeep',
'/messages',
'/vendor',
],
'format' => 'php',
'messagePath' => 'frontend' . DIRECTORY_SEPARATOR . 'translations',
'overwrite' => true,
];
and here my main.php also on frontend
(...)
'language' => 'en-US',
'components' => [
'i18n' => [
'translations' => [
'app*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => 'frontend/translations',
'fileMap' => [
'app' => 'app.php',
'app/error' => 'error.php',
],
],
],
]
I'm using the <?= Yii::t('app', 'some string') ?> on the sites and layouts and when I run the command ./yii message/extract #frontend/config/i18n.php it creates to me a folder called 'translations' contain other two folders 'en-US' and 'pt-BR' both with app.php which i already had filled with some translations. But still, no translation happens when i change the language on the main.php as it should be (i think).
I would appreciate if someone could give me a hand on that.
Thanks.
Great post, with all the needed details.
I was struggling with the same things, but you did it quite well.
So, if you can run the command and it generates the file, then the sourcePath is correct.
If it doesn't display the translation messages at runtime, despite your setting changes, then, I presume the issue could be on your basePath:
Try using, on your basePath configuration, the following:
'basePath' => '#frontend/translations',

Categories