Laravel log save directory problem;

Source: Internet
Author: User

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;

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.