dongliuzi3410
2019-08-09 06:36
浏览 162
已采纳

是否可以在symfony项目目录之外编写monolog日志?

I am using symfony 4.1 and monolog 3.3. I have my symfony project in the following directory: /var/www/html/{project_dir}. In production environment I want my logs to be stored inside /tmp/foo instead of standard symfony directory: {project_dir_path}/var/log.

I changed Kernel::getLogDir() method to the following:

public function getLogDir()
{
    return 'prod' === $this->environment
        ? '/tmp/foo'
        : $this->getProjectDir().'/var/log';
}

My production monolog.yaml path looks like this:

path: "%kernel.logs_dir%/%kernel.environment%.log"

My environment for testing issues is set to 'prod'.

When I am debugging, I can see that monolog's log directory is correctly set to /tmp/foo. /tmp/foo has proper access rights (0766) but when I try to log something, the directory contains nothing.

图片转代码服务由CSDN问答提供 功能建议

我正在使用symfony 4.1和monolog 3.3。 我的symfony项目位于以下目录: / var / www / html / {project_dir} 。 在生产环境中,我希望我的日志存储在 / tmp / foo 中,而不是标准的symfony目录: {project_dir_path} / var / log

我将 Kernel :: getLogDir()方法更改为以下内容:

  public function getLogDir()
 {
 return'prod'=== $ this-> environment 
?  '/ tmp / foo'
:$ this-> getProjectDir()。'/ var / log'; 
} 
   
 
 

我的作品 monolog.yaml 路径如下所示:

  path:“%kernel.logs_dir%/%kernel.environment%.log”
  <  / pre> 
 
 

我的测试问题环境设置为'prod'。

当我调试时,我可以看到monolog的日志目录被正确设置为< 代码>的/ tmp /富 / tmp / foo 具有适当的访问权限(0766),但是当我尝试记录某些内容时,该目录不包含任何内容。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duangou6446 2019-08-10 13:35
    已采纳

    Solved, simple mistake. I was developing on docker container, having my project directory as a volumine, but /tmp/foo wasn't obviously binded, so logs were just sitting on the container.

    打赏 评论
  • douyong4842 2019-08-09 10:40

    Conceptually, you can't for security reason. But, you can bypass it by creating a symlink inside your project directory for the right directory folder.

    Something like:

    public function getLogDir()
    {
        return 'prod' === $this->environment
            ? '/var/symlink-to-tmp-folder'
            : $this->getProjectDir().'/var/log';
    }
    
    打赏 评论

相关推荐 更多相似问题