{"id":1649,"date":"2017-11-13T14:31:13","date_gmt":"2017-11-13T12:31:13","guid":{"rendered":"https:\/\/www.h-hennes.fr\/blog\/?p=1649"},"modified":"2017-11-13T14:38:57","modified_gmt":"2017-11-13T12:38:57","slug":"magento-2-commande-console-pour-supprimer-les-fichiers-de-logs-et-les-reports","status":"publish","type":"post","link":"https:\/\/www.h-hennes.fr\/blog\/2017\/11\/13\/magento-2-commande-console-pour-supprimer-les-fichiers-de-logs-et-les-reports\/","title":{"rendered":"Magento 2 : Commande console pour supprimer les fichiers de logs et les reports"},"content":{"rendered":"<p>Lors de la phase de d\u00e9veloppement sous magento 2, les dossiers contenants les fichiers de logs et de reports ont tendance \u00e0 augmenter rapidement et il devient difficile de s&rsquo;y retrouver.<br \/>\nIl est possible de supprimer facilement le contenu des ces dossiers via l&rsquo;explorateur de fichier ou via des commandes shell, mais cela peut \u00eatre r\u00e9barbatif ou source d&rsquo;erreurs ( rm -rf sur un mauvais dossier entre autre &#8230; )<\/p>\n<p>Pour simplifier ces actions nous allons donc cr\u00e9er 2 nouvelles commandes dans la console Magento.<br \/>\nCeci nous permets de constater qu&rsquo;ajouter des commandes consoles sur magento 2 c&rsquo;est tr\u00e8s simple \ud83d\ude42<\/p>\n<p>Le module s&rsquo;appellera Hhennes_Tools et sera situ\u00e9 dans app\/code\/Hhennes\/Tools\/<\/p>\n<p>Pour initialiser le module cr\u00e9er le fichier etc\/module.xml avec le contenu suivant<\/p>\n<pre lang=\"xml\" escaped=\"true\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\"&gt;\r\n    &lt;module name=\"Hhennes_Tools\" setup_version=\"0.1.0\"&gt;\r\n    &lt;\/module&gt;\r\n&lt;\/config&gt;<\/pre>\n<p>Et le fichier de registration registration.php avec le contenu suivant :<\/p>\n<pre lang=\"php\" escaped=\"true\">&lt;?php\r\n\\Magento\\Framework\\Component\\ComponentRegistrar::register(\r\n    \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\r\n    'Hhennes_Tools',\r\n    __DIR__\r\n);<\/pre>\n<p>Dans le fichier Hhennes\/Tools\/etc\/di.xml ajoutez le contenu suivant :<\/p>\n<pre lang=\"xml\" escaped=\"true\">&lt;?xml version=\"1.0\"?&gt;\r\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\"&gt;\r\n    &lt;type name=\"Magento\\Framework\\Console\\CommandList\"&gt;\r\n        &lt;arguments&gt;\r\n            &lt;argument name=\"commands\" xsi:type=\"array\"&gt;\r\n                &lt;item name=\"hhennes_tools_cleanreports\" xsi:type=\"object\"&gt;Hhennes\\Tools\\Console\\Command\\CleanReportsCommand&lt;\/item&gt;\r\n                &lt;item name=\"hhennes_tools_cleanlogs\" xsi:type=\"object\"&gt;Hhennes\\Tools\\Console\\Command\\CleanLogsCommand&lt;\/item&gt;\r\n            &lt;\/argument&gt;\r\n        &lt;\/arguments&gt;\r\n    &lt;\/type&gt;\r\n&lt;\/config&gt;<\/pre>\n<p>La logique d&rsquo;ajout des commandes est situ\u00e9 dans ce fichier, nous ajoutons nos commandes \u00e0 la liste des commandes disponible lors de l\u2019initialisation de la classe Magento\\Framework\\Console\\CommandList<\/p>\n<p>Il ne reste plus qu&rsquo;a cr\u00e9er les fichiers php des commandes :<br \/>\nHhennes\/Tools\/Console\/Command\/CleanLogsCommand.php<\/p>\n<pre lang=\"xml\" escaped=\"true\">&lt;?php\r\n\r\nnamespace Hhennes\\Tools\\Console\\Command;\r\n\r\nuse Symfony\\Component\\Console\\Command\\Command;\r\nuse Symfony\\Component\\Console\\Input\\InputInterface;\r\nuse Symfony\\Component\\Console\\Output\\OutputInterface;\r\nuse Magento\\Framework\\App\\Filesystem\\DirectoryList;\r\n\r\nclass CleanLogsCommand extends Command\r\n{\r\n\r\n    \/** @var DirectoryList  *\/\r\n    protected $_directoryList;\r\n\r\n    \/**\r\n     * CleanLogsCommand constructor.\r\n     * @param DirectoryList $directoryList\r\n     *\/\r\n    public function __construct(DirectoryList $directoryList)\r\n    {\r\n        $this-&gt;_directoryList = $directoryList;\r\n        parent::__construct();\r\n    }\r\n\r\n    public function configure()\r\n    {\r\n        $this-&gt;setName('dev:clean:logs')\r\n            -&gt;setDescription('clean logs directory');\r\n    }\r\n\r\n    \/**\r\n     * @param InputInterface $inputInterface\r\n     * @param OutputInterface $outputInterface\r\n     * @return bool\r\n     *\/\r\n    public function execute(InputInterface $inputInterface, OutputInterface $outputInterface)\r\n    {\r\n\r\n        if (!function_exists('exec')) {\r\n            $outputInterface-&gt;writeln('&lt;error&gt;exec command should be available in order to clean log directory&lt;\/error&gt;');\r\n        } else {\r\n            $reportDir = $this-&gt;_directoryList-&gt;getPath(DirectoryList::VAR_DIR) . DIRECTORY_SEPARATOR . 'log';\r\n            if (is_dir($reportDir)) {\r\n                \/\/Suppression du contenu du dossier des logs\r\n                exec('rm -rf ' . $reportDir . '\/*');\r\n                $outputInterface-&gt;writeln('&lt;info&gt;log directory clean with success&lt;\/info&gt;');\r\n            } else {\r\n                $outputInterface-&gt;writeln('&lt;error&gt;log directory does not exists&lt;\/error&gt;');\r\n            }\r\n        }\r\n        return true;\r\n    }\r\n\r\n}<\/pre>\n<p>Hhennes\/Tools\/Console\/Command\/CleanReportsCommand.php<\/p>\n<pre lang=\"xml\" escaped=\"true\">&lt;?php\r\n\r\nnamespace Hhennes\\Tools\\Console\\Command;\r\n\r\nuse Symfony\\Component\\Console\\Command\\Command;\r\nuse Symfony\\Component\\Console\\Input\\InputInterface;\r\nuse Symfony\\Component\\Console\\Output\\OutputInterface;\r\nuse Magento\\Framework\\App\\Filesystem\\DirectoryList;\r\n\r\nclass CleanReportsCommand extends Command\r\n{\r\n\r\n    \/** @var DirectoryList  *\/\r\n    protected $_directoryList;\r\n\r\n    \/**\r\n     * CleanReportsCommand constructor.\r\n     * @param DirectoryList $directoryList\r\n     *\/\r\n    public function __construct(DirectoryList $directoryList)\r\n    {\r\n        $this-&gt;_directoryList = $directoryList;\r\n        parent::__construct();\r\n    }\r\n\r\n    public function configure()\r\n    {\r\n        $this-&gt;setName('dev:clean:reports')\r\n            -&gt;setDescription('clean reports directory');\r\n    }\r\n\r\n    \/**\r\n     * @param InputInterface $inputInterface\r\n     * @param OutputInterface $outputInterface\r\n     * @return bool\r\n     *\/\r\n    public function execute(InputInterface $inputInterface, OutputInterface $outputInterface)\r\n    {\r\n\r\n        if (!function_exists('exec')) {\r\n            $outputInterface-&gt;writeln('&lt;error&gt;exec command should be available in order to clean report directory&lt;\/error&gt;');\r\n        } else {\r\n            $reportDir = $this-&gt;_directoryList-&gt;getPath(DirectoryList::VAR_DIR) . DIRECTORY_SEPARATOR . 'report';\r\n            if (is_dir($reportDir)) {\r\n                \/\/Suppression du contenu des reports\r\n                exec('rm -rf ' . $reportDir . '\/*');\r\n                $outputInterface-&gt;writeln('&lt;info&gt;report directory clean with success&lt;\/info&gt;');\r\n            } else {\r\n                $outputInterface-&gt;writeln('&lt;error&gt;report directory does not exists&lt;\/error&gt;');\r\n            }\r\n        }\r\n        return true;\r\n    }\r\n\r\n}<\/pre>\n<p>Puis d&rsquo; installer le module via la commande :<\/p>\n<pre lang=\"bash\" escaped=\"true\">php bin\/magento module:enable Hhennes_Tools<\/pre>\n<p>Puis de lancer les updates :<\/p>\n<pre lang=\"bash\" escaped=\"true\">php bin\/magento setup:upgrade<\/pre>\n<p>&nbsp;<\/p>\n<p>Relancer la console et les nouvelles commandes sont disponibles \ud83d\ude42<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1650\" src=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2017\/11\/new-commands.jpg\" alt=\"Magento 2 commands\" width=\"961\" height=\"119\" srcset=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2017\/11\/new-commands.jpg 961w, https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2017\/11\/new-commands-300x37.jpg 300w, https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2017\/11\/new-commands-768x95.jpg 768w\" sizes=\"auto, (max-width: 961px) 100vw, 961px\" \/><br \/>\nPour toutes les syntaxes n&rsquo;h\u00e9sitez pas \u00e0 consulter la documentation de la console symfony, car c&rsquo;est elle que Magento utilise \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lors de la phase de d\u00e9veloppement sous magento 2, les dossiers contenants les fichiers de logs et de reports ont tendance \u00e0 augmenter rapidement et il devient difficile de s&rsquo;y retrouver. Il est possible de supprimer facilement le contenu des ces dossiers via l&rsquo;explorateur de fichier ou via des commandes shell, mais cela peut \u00eatre [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[246],"tags":[506,440,150,276],"class_list":["post-1649","post","type-post","status-publish","format-standard","hentry","category-magento-2","tag-command","tag-console","tag-magento","tag-magento2"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/1649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/comments?post=1649"}],"version-history":[{"count":3,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/1649\/revisions"}],"predecessor-version":[{"id":1653,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/1649\/revisions\/1653"}],"wp:attachment":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/media?parent=1649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/categories?post=1649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/tags?post=1649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}