laravel 流明每日日志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32321785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Lumen Daily Logs
提问by Jhonny Afonso
I want to add to my Lumen project a daily Log.
我想将每日日志添加到我的 Lumen 项目中。
I try this in the app.php (Folder Bootstrap/)
我在 app.php(文件夹 Bootstrap/)中尝试这个
$logFile = 'laravel.log';
Log::useDailyFiles(storage_path().'/logs/'.$logFile);
But this set me that error
但这让我犯了那个错误
Call to undefined method Monolog\logger::useDailyFiles()
调用未定义的方法 Monolog\logger::useDailyFiles()
Any help I appreciate...Thanks
我很感激任何帮助......谢谢
回答by prograhammer
If you look at the framework source code hereyou can see that it will not do daily logs, but rather write to a single log file lumen.log
. There is a public method available configureMonologUsing
seen hereand referenced herethat you can use to override the default behavior without extending the Application.
如果您查看此处的框架源代码,您会发现它不会执行每日日志,而是写入单个日志文件lumen.log
。在此处configureMonologUsing
看到并在此处引用了一个可用的公共方法,您可以使用它来覆盖默认行为,而无需扩展应用程序。
Lumen just sets a handler to monolog, so another good solution is you could do this:
Lumen 只是将处理程序设置为 monolog,因此另一个好的解决方案是您可以这样做:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
class LogServiceProvider extends ServiceProvider
{
/**
* Configure logging on boot.
*
* @return void
*/
public function boot()
{
$maxFiles = 5;
$handlers[] = (new RotatingFileHandler(storage_path("logs/lumen.log"), $maxFiles))
->setFormatter(new LineFormatter(null, null, true, true));
$this->app['log']->setHandlers($handlers);
}
/**
* Register the log service.
*
* @return void
*/
public function register()
{
// Log binding already registered in vendor/laravel/lumen-framework/src/Application.php.
}
}
Then don't forget to add the service provider to your Lumen bootstrap/app.php:
然后不要忘记将服务提供者添加到您的 Lumen bootstrap/app.php:
$app->register(\App\Providers\LogServiceProvider::class);
回答by Ahsan.Amin
In Lumen 5.6 better way is to configure your default setting in .env as LOG_CHANNEL=daily
在 Lumen 5.6 中更好的方法是将 .env 中的默认设置配置为 LOG_CHANNEL=daily
By default the setting is LOG_CHANNEL=stack
which use single file for logging.
默认情况下,设置是LOG_CHANNEL=stack
使用单个文件进行日志记录。
回答by 8ctopus
Starting from version 5.6, configuring the logging system is much easier:
从 5.6 版本开始,配置日志系统要容易得多:
- Create directory config in your project if it doesn't exist
Copy the logging config file from vendor/laravel/lumen-framework/config/logging.php to your project config dir
Edit file config/logging.php and adjust the channels property to your liking.
- 如果项目不存在,则在项目中创建目录配置
将日志配置文件从 vendor/laravel/lumen-framework/config/logging.php 复制到您的项目配置目录
编辑文件 config/logging.php 并根据您的喜好调整 channels 属性。
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['daily'],
],