Laravel Modifying the default log file name and location

Source: Internet
Author: User
This article mainly introduces about Laravel modify the default log file name and location, has a certain reference value, now share to everyone, have the need for friends can refer to

Modify the default log location

Our usual development may have kept Laravel's log files in the default location, but if our project is on-line and full-scale, each deployment is Git's newest code, then this time each will empty our log, show this is not what we expect, Fortunately Laravel has a way of providing us with modifications.

Let's take/var/log/nginx/app/phphub/phphub.log as an example.

Add log Processing Class #

First we create a new file, app/foundation/bootstrap/configurelogging.php, the code is as follows:

<?phpnamespace App\foundation\bootstrap;use Illuminate\log\writer;use illuminate\contracts\foundation\ Application;class configurelogging{/** * Set the app's Monolog handler * * @param \illuminate\contracts\foundation\application $app * @param \illuminate\log\writer $log * @return void */Public Function configurehandlers (application $app, Writer $log) { $method = ' Configure '. Ucfirst ($app [' config '] [' app.log ']). ' Handler '; $this->{$method} ($app, $log); }/** * Set to apply Monolog handler in single mode * * @param \illuminate\contracts\foundation\application $app * @param \illuminate\log\w Riter $log * @return void */protected function Configuresinglehandler (application $app, Writer $log) {$config = $app-&gt ; make (' config '); $filename = $config->get (' App.log_path ', '/var/log/nginx/app/system '). '/' . $config->get (' app.log_name ', ' laravel '). '. log '; $log->usefiles ($filename); }/** * Set Monolog handler in daily mode * * * @param \illuminate\contracts\foundation\application $app * @param \illuminate\log\wr ITER $lOG * @return void */protected function Configuredailyhandler (application $app, Writer $log) {$config = $app->make (' Co Nfig '); $filename = $config->get (' App.log_path ', '/var/log/nginx/app/system '). '/' . $config->get (' app.log_name ', ' laravel '). '. log '; $log->usedailyfiles ($filename, $app->make (' config ')->get (' App.log_max_files ', 5)); /** * Set Monolog handler to apply syslog mode * * @param \illuminate\contracts\foundation\application $app * @param \illuminate\log\w Riter $log * @return void */protected function Configuresysloghandler (application $app, Writer $log) {$log->usesyslog ($app->make (' config ')->get (' App.log_name ', ' laravel ')); }/** * Set Monolog handler in errorlog mode * * * @param \illuminate\contracts\foundation\application $app * @param \illuminate\log \writer $log * @return void */protected function Configureerrorloghandler (application $app, Writer $log) {$log->useer Rorlog (); }}

Configure Log Storage Path #

Added in. env

App_name=phphub

Increase in config/app.php

/** * Application Name */' name ' = ' env ' (' app_name ', ' laravel '),/** * Log location */' log_path ' = '/var/log/nginx/app/'. Env (' app_name ', ' laravel '),/** * log file name */' log_name ' + env (' app_name ', ' laravel '),/** * Maximum number of log files */' log_max_files ' =&G T ' 30 ',

Apply our new Processing class #

Modify bootstrap/app.php to add code before return $app

$app->configuremonologusing (function ($monolog) use ($app) {$configureLogging = new app\foundation\bootstrap\ Configurelogging (); $configureLogging->configurehandlers ($app, $app->log);});

At this point we can use the \Log::info('test log info'); test, the log should be recorded in the /var/log/nginx/app/phphub/phphub.log .

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.