如何修改lumen storage path/log path

赞赏 2016-07-12

最近接触lumen ,用来做api. 发现他的logs放在 项目里 /storage/logs/lumen.log, 放在项目里有好多弊端,发布代码回冲掉,想把他转移到机器 /var/logs目录。发现Lumen框架没有配置项


代码如下:

  • vendor/laravel/lumen-framework/src/Application.php


    /**
     * Get the Monolog handler for the application.
     *
     * @return \Monolog\Handler\AbstractHandler
     */
    protected function getMonologHandler()
    {
        return (new StreamHandler(storage_path('logs/lumen.log'), Logger::DEBUG))
                            ->setFormatter(new LineFormatter(null, null, true, true));
    }

    /**
     * Get the storage path for the application.
     *
     * @param  string|null  $path
     * @return string
     */
    public function storagePath($path = null)
    {
        return $this->basePath().'/storage'.($path ? '/'.$path : $path);
    }
  • vendor/laravel/lumen-framework/src/helpers.php
if (! function_exists('storage_path')) {
    /**
     * Get the path to the storage folder.
     *
     * @param  string  $path
     * @return string
     */
    function storage_path($path = '')
    {
        return app()->storagePath($path);
    }
}
  • 显然框架是写死的,方案一,改框架里的 storagePath() 不是最优解,对框架造成了污染。
  • 最优方案继承 vendor/laravel/lumen-framework/src/Application.php 覆盖 storagePath(). 修改如下:

  • bootstrap/app.php
/**
 * Class Lapp 新增子类
 */
class Lapp extends Laravel\Lumen\Application {
    public function __construct($basePath = null)
    {
        parent::__construct($basePath = null);
    }

    /**
     * overwrite lumen application storagepath for storagepath configable;
     * @param null $path
     * @return string
     */
    public function storagePath($path = null)
    {
        return (env('STORAGE_PATH') ? env('STORAGE_PATH') : $this->basePath()) . '/storage' . ($path ? '/'.$path : $path);
    }
}
//这里注释掉
//$app = new Laravel\Lumen\Application(
//    realpath(__DIR__.'/../')
//);
  • 注意在项目.env 增加 STORAGE_PATH=yourstorgepath 配置

  • 亲测试通过,有问题点击追问按钮留言交流!
登陆后阅读全文
阅读 3182 赞赏 2 有用 32 没用 0 收藏 1 分享

   


作者声明:本篇文章系本人原创,欢迎分享,但未经许可,谢绝转载。

0 条留言

有经验的老司机的头像

有经验的老司机

年轻人,走!我带你。

他的文章

电脑无线连接安卓手机 进入android命令行模式(完胜adb shell数据线连接方式)

iTerm2配置clone session功能

如何科学上网(翻墙)

php5.6-和php5.6 CURL 上传文件的一个坑

如何修改lumen storage path/log path

php.ini 设置了 error_log 为什么不生效?

IP查询二分法的运用

iframe 的高度自适应 iframe 内容页面的高度解决方案

手机扫一扫
分享文章