Laravel Log Save directory can not be saved to the Storage/logs directory? How to modify?
Can i customize the file name?
Split by date according to document configuration is not working! or add it in the Laravel.log.
Reply content:
Laravel Log Save directory can not be saved to the Storage/logs directory? How to modify?
Can i customize the file name?
Split by date according to document configuration is not working! or add it in the Laravel.log.
Custom logs
Method 1-> Simple Rough
use Monolog\Handler\StreamHandler;use Monolog\Logger;$log = new Logger('vikin');$log->pushHandler( new StreamHandler( storage_path('logs/vikin.log'), Logger::INFO ));$log->addInfo("test");
Method2-> based on configurelogging base class
1, you can use this method to overwrite the Laravel original log "one info, other levels can be self-expanding"
2, as a laravel extension, in the need to record the log in a separate place;
1. Create a class that inherits from Configurelogging
namespace you customize the namespace \configurelogging;use illuminate\foundation\bootstrap\configurelogging as Baseconfigurelogging;use monolog\formatter\lineformatter;use Monolog\handler\streamhandler;use Monolog\Logger; Class Customlog extends baseconfigurelogging{protected function Configuresinglehandler (application $app, Writer $log) {//Same as Method 1, set log path, set log level $path = Storage_path (' Logs/vikin.log '); $level = Logger::info; $logStreamHandler = new Streamhandler ($path, $level); Log format: Using laravel original format://The default output format is "[%datetime%]%channel%.%level_name%:%message%%context%%e xtra%\n "$format =" [%datetime%]%channel%.%level_name%:%message%%context%%extra%\n "; $formatter = new Lineformatter ($format); $logStreamHandler->setformatter ($formatter); Output log $logger = $log->getmonolog (); $logger->pushhandler ($logStreamHandler); }}
Rewriting configureSingleHandler
is equivalent to rewriting a log processor;
3. Automatic loading
To modify a file under the root directory composer.json
"psr-4": { "App\\": "app/", "你自定义命名空间\\": "文件路径/"}
4. Coverage
In the /app/Http/Kernel.php
file, the Kernel
method of constructing the extension class
use Illuminate\Contracts\Foundation\Application;use Illuminate\Contracts\Events\Dispatcher;public function __construct(Application $app, Dispatcher $events){ parent::__construct($app, $events); array_walk($this->bootstrappers, function(&$bootstrapper) { if($bootstrapper === 'Illuminate\Foundation\Bootstrap\ConfigureLogging') { //替换为我们自定义的日志处理器 $bootstrapper = '你自定义命名空间\ConfigureLogging'; } });}
5. Use
Log::info('自定义log处理器');
6. Expansion
Create a serviceprovider and facade (personal habits like to use facade); Add to App Config, use composer to load automatically;