用于清除symfony 2中的日志文件的命令

tir*_*ula 5 symfony

我想知道是否有命令清除Symfony 2中的日志文件?虽然有php app/console cache:clear清除缓存,但我不知道清除日志文件的任何命令(logs/dev.log和logs/prod.log).我总是手动清除这些日志.谢谢

lxg*_*lxg 10

Symfony中没有明确的命令.但使用外壳单线是没有什么可羞耻的:

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

# … or l33t
> app/logs/dev.log # works at least in bash, haven't tried others
Run Code Online (Sandbox Code Playgroud)


Don*_*sto 7

对于您可以使用的开发环境

cat /dev/null > app/logs/dev.log
Run Code Online (Sandbox Code Playgroud)

并用于生产环境

cat /dev/null > app/logs/prod.log
Run Code Online (Sandbox Code Playgroud)

/dev/null进入unix系统是一个虚拟设备(实际上是虚拟文件,因为所有内容都是unix中的文件),它会丢弃写在其上的每个数据.它也被称为位桶:)

而且,你为什么不考虑利用logrotate
通过这种方式,您可以轻松地分离日志(每周 - 每月 - 等等)并且永远不会丢失"重要"数据.最后但并非最不重要的是,您不必手动清除日志文件


HKa*_*lla 5

这是一个清除Symfony> 2.8的简单的Symfony命令,用于清除日志。清除整个日志目录的区别/好处是,它仅删除指定环境的日志,而不删除可能已添加的自定义日志文件-在我的情况下是必需的。

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));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

服务配置为:

services:
    std.command.clear_logs_command:
        class: Std\AppBundle\Command\ClearLogsCommand
        arguments: ['%kernel.logs_dir%', '%kernel.environment%']
        tags:
           -  { name: console.command }
Run Code Online (Sandbox Code Playgroud)

要执行运行:

app/console std:logs:clear --env=prod
Run Code Online (Sandbox Code Playgroud)

或作为要点:[ https://gist.github.com/HKandulla/5de5a4074a5296b9465b4825431dfff3#file-clearlogscommand-php] [1 ]