Support the ongoing development of Laravel.io →
posted 9 years ago
Configuration
Last updated 2 years ago.
0

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

0
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.

Last updated 9 years ago.

ishepherd liked this reply

1

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')))
                )
            );
        }
Last updated 8 years ago.
0

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.

0

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';
    }

}
0

Sign in to participate in this thread!

Eventy

Your banner here too?

maxme maxme Joined 5 Feb 2015

Moderators

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

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.