Vagrant, Symfony Log Files

When developing an application on a Vagrant machine such as PHP Development Using CognacBox, you will certainly have log and cache files. Because of the way Vagrant maps these folders between the virtual environment and the physical Windows host, there can be significant lag. You will often see error messages such as:

For better performances, you should move the cache and log directories to a non-shared folder of the VM.

When running certain Symfony bon/console commands such as cache:clear

Alternatively you may see an error such as:

In Filesystem.php line 200:
Failed to remove directory “/var/www/var/cache/.”: rmdir(/var/www/var/cache/..): Text file busy

When running bin/console cache:clear or console cache:clear.

The easiest way to handle this is to move the var/ path to another location.

This documentation assumed you’re running Ubuntu Linux such as what ships with CognacBox:

cd /var/log
sudo mkdir dev
sudo chmod 777 dev

Then edit the Symfony kernel to use the new path:

    public function getCacheDir(): string
    {
        if($this->environment=="dev"){
            return '/var/log/dev/cache';

        }
        return dirname(__DIR__).'/var/'.$this->environment.'/cache';
    }

    public function getLogDir(): string
    {
        if($this->environment=="dev"){
            return '/var/log/dev/log';
        }
        return dirname(__DIR__).'/var/'.$this->environment.'/log';
    }

This code will specifically use the new /var/log/dev subdirectory for both cache and log files exclusively while the Symfony environment is configured to “dev”. Otherwise it will use the default location, such as in production where hopefully you are not running it in a virtual box setup.