Question

I use Symfony 2.2 and Monolog package.

My handlers in Monolog:

monolog:
    handlers:
        type: stream
        path: "%kernel.logs_dir%/%kernel.environment%/my-path/error.log"
        level: error

But stream handler not auto create directory, if directory not exists. How to auto create directory? Can event dispatcher, but i not see events on monolog ((

Thank.

Was it helpful?

Solution

Solution this problem:

  • Create own stream handler
  • Override base stream handler

Example:

Stream handler:

namespace Acme\DemoBundle\Component\Monolog;

use Monolog\Handler\StreamHandler as BaseStreamHandler;

class StreamHandler extends BaseStreamHandler
{
    /**
     * @{inerhitDoc}
     */
    public function write(array $record)
    {
        if (null === $this->stream) {
            // From original monolog stream handler
            if (!$this->url) {
                throw new \LogicException('Missing stream url, the stream can not be opened. This may be caused by a premature call to close().');
            }

            $dir = dirname($this->url);

            if (!is_dir($dir)) {
                if(!@mkdir($dir, 0777, TRUE)) {
                    // Control errors
                }
            }
        }

        parent::write($record);
    }
}

And override base stream handler in symfony monolog package:

<parameter key="monolog.handler.stream.class">Acme\DemoBundle\Component\Monolog\StreamHandler</parameter>

Attention: This is a base example override monolog handlers in Symfony 2 package!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top