Back

[Laravel5] How to change logs path?


maxme posted 3 years ago

In L4 we have an ability to change path where laravel stores it's logs (in /app/start/global.php file). This is awesome when you want to store all your logs in one place. But in L5 there is only app.log config where you can set your log's handler. Is it possible to change this path in L5?

frezno replied 3 years ago

you can change the hardcoded path at vendor/laravel/... . but that's truly no elegant solution. there probably is a better solution.

usm4n replied 3 years ago Solution

You can do this by extending the default Illuminate\Foundation\Bootstrap\ConfigureLogging bootstraper class and then registering it with the Kernel. The following code sets the custom paths for daily and single config settings:

file:/// app/Bootstrap/ConfigureLogging.php:

<?php namespace App\Bootstrap;

use Illuminate\Log\Writer;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Bootstrap\ConfigureLogging as BaseLoggingConfiguration;

class ConfigureLogging extends BaseLoggingConfiguration {

	/**
	 * Configure the Monolog handlers for the application.
	 *
	 * @param  \Illuminate\Contracts\Foundation\Application  $app
	 * @param  \Illuminate\Log\Writer  $log
	 * @return void
	 */
	protected function configureSingleHandler(Application $app, Writer $log)
	{      
        //sets the path to custom app/log/single-xxxx-xx-xx.log file.
		$log->useFiles(base_path() . '/log/single.log');
	}

	/**
	 * Configure the Monolog handlers for the application.
	 *
	 * @param  \Illuminate\Contracts\Foundation\Application  $app
	 * @param  \Illuminate\Log\Writer  $log
	 * @return void
	 */
	protected function configureDailyHandler(Application $app, Writer $log)
	{	
        //sets the path to custom app/log/daily-xxxx-xx-xx.log file.
		$log->useDailyFiles(base_path() . '/logs/daily.log', 5);
	}

}

Now register it with the kernel by adding the following code inside your app/Http/Kernel.php file:

    use Illuminate\Routing\Router;
    use Illuminate\Contracts\Foundation\Application;
    .....
    ......
		/**
	 * Create a new HTTP kernel instance.
	 *
	 * @param  \Illuminate\Contracts\Foundation\Application  $app
	 * @param  \Illuminate\Routing\Router  $router
	 * @return void
	 */
	public function __construct(Application $app, Router $router)
	{
		parent::__construct($app, $router);

		array_walk($this->bootstrappers, function(&$bootstrapper)
		{
			if($bootstrapper === 'Illuminate\Foundation\Bootstrap\ConfigureLogging')
			{
				$bootstrapper = 'App\Bootstrap\ConfigureLogging';
			}
		});
	}
    ....
    ....

This will only set the paths for Http Kernel, for console you will need to add the above code inside the app/Console/Kernel.php as well.

Usman.

PS: Make sure that your custom directory is writable by web server user.

lukepolo replied 3 years ago

I did something like this in the app provider

  if(env('APP_LOG_TYPE') == 'single') {
            $this->app->instance('log', new \Illuminate\Log\Writer(
                    (new Logger(
                        $this->app->environment()
                    ))->pushHandler(new StreamHandler(env('APP_LOG_PATH')))
                )
            );
        }
kcasas replied 1 year ago

I don't like the idea of having to edit the Kernel for http and console. For those who share my sentiment, you can look unto the solution I've come up with http://stackoverflow.com/a/43800364/4069872.

Brian Walker replied 1 year ago

For the next person... looks like now you can just extend the Illuminate\Log\LogServiceProvider, but similar to original example.

namespace App\Providers;

use Illuminate\Log\LogServiceProvider;
use Illuminate\Log\Writer;

class CustomLogServiceProvider extends LogServiceProvider {

    /**
     * Configure the Monolog handlers for the application.
     *
     * @param  \Illuminate\Log\Writer  $log
     * @return void
     */
    protected function configureSingleHandler(Writer $log)
    {
        //sets the path to custom app/log/single-xxxx-xx-xx.log file.
        $log->useFiles($this->_getLogPath() . '/single.log');
    }

    /**
     * Configure the Monolog handlers for the application.
     *
     * @param  \Illuminate\Log\Writer  $log
     * @return void
     */
    protected function configureDailyHandler(Writer $log)
    {
        //sets the path to custom app/log/daily-xxxx-xx-xx.log file.
        $log->useDailyFiles($this->_getLogPath() . '/daily.log', 0, config('app.log_level', 'debug'));
    }

    private function _getLogPath() {
        $storage_path = config('app.storage', base_path() . DIRECTORY_SEPARATOR . 'storage');
        return $storage_path . DIRECTORY_SEPARATOR . 'logs';
    }

}

Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us