7

I was wondering if there is a command to clear the log files in Symfony 2 ? While there is the php app/console cache:clear to clear the cache, I don't know any command to clear the log files (logs/dev.log and logs/prod.log). I have always cleared these logs manually. Thank you

1
  • What's your OS? Base on your OS as @lxg says you can write a proper shell script
    – Javad
    Sep 18, 2014 at 13:42

3 Answers 3

12

There's no explicit command in Symfony. But using a shell one-liner is nothing to be ashamed of:

# straightforward …
echo -n '' > app/logs/dev.log

# … or l33t
> app/logs/dev.log # works at least in bash, haven't tried others
8

For development environment you could use

cat /dev/null > app/logs/dev.log

and for production env

cat /dev/null > app/logs/prod.log

/dev/null into unix systems is a virtual device (virual file actually, because everything is a file in unix) that discard every data written on it. It is also called bit bucket :)

Moreover, why don't you think about take advantage of logrotate?
That way you could easily separate your logs (day-week-month and so on) and never lost "important" data. Last but not least, you don't have to clear logs file manually

1
  • Thnk you for response. I didn't know about logrotate, I will have a look at it
    – tiruncula
    Sep 18, 2014 at 15:05
6

Here is a simple Symfony Command for clearing the logs, written for Symfony > 2.8. The difference/benefit over clearing the whole log-dir is that it only deletes the log for the specified environment and not custom log files that might have been added - in my case a requirement.

ConsoleCommand:

namespace Std\AppBundle\Command;

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ClearLogsCommand extends Command
{
    /**
     * @var SymfonyStyle
     */
    private $io;
    /**
     * @var Filesystem
     */
    private $fs;
    private $logsDir;
    private $env;

    /**
     * ClearLogsCommand constructor.
     *
     * @param null|string $logsDir
     * @param             $env
     */
    public function __construct($logsDir, $env)
    {
        parent::__construct();
        $this->logsDir = $logsDir;
        $this->env = $env;
    }

    /**
     * @inheritdoc
     */
    protected function configure()
    {
        $this
            ->setName('std:logs:clear')
            ->setDescription('Deletes all logfiles');
    }

    /**
     * @param InputInterface  $input
     * @param OutputInterface $output
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->io = new SymfonyStyle($input, $output);
        $this->fs = new Filesystem();

        $log = $this->logsDir . '/' . $this->env . '.log';
        $this->io->comment(sprintf('Clearing the logs for the <info>%s</info> environment', $this->env));
        $this->fs->remove($log);
        if (!$this->fs->exists($log)) {
            $this->io->success(sprintf('Logs for the "%s" environment was successfully cleared.', $this->env));
        } else {
            $this->io->error(sprintf('Logs for the "%s" environment could not be cleared.', $this->env));
        }
    }
}

The service config would be:

services:
    std.command.clear_logs_command:
        class: Std\AppBundle\Command\ClearLogsCommand
        arguments: ['%kernel.logs_dir%', '%kernel.environment%']
        tags:
           -  { name: console.command }

To execute run:

app/console std:logs:clear --env=prod

Or as a gist: [https://gist.github.com/HKandulla/5de5a4074a5296b9465b4825431dfff3#file-clearlogscommand-php][1]

2
  • Thanks for this. I will just add to your answer. For me, this services configuration worked on Symfony 4.2.9: App\Command\ClearLogsCommand: arguments: $logsDir: '%kernel.logs_dir%' $env: '%kernel.environment%' Jul 16, 2019 at 15:51
  • 1
    You can also let autowiring do the work for you. All you have to do is add the parameters in a bind section under _defaults like this: $env: "%kernel.environment%" $logsDir: "%kernel.logs_dir%". Also, unlike a minimal bash script, using a SF command prevents permission issues and hard coded parameters.
    – vctls
    Nov 22, 2019 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.