{"id":8132,"date":"2023-06-21T10:08:20","date_gmt":"2023-06-21T10:08:20","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=8132"},"modified":"2023-08-07T05:28:39","modified_gmt":"2023-08-07T05:28:39","slug":"clear-laravel-logs","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/laravel\/clear-laravel-logs","title":{"rendered":"Ways to Clear Laravel Logs"},"content":{"rendered":"<h3>Introduction:<\/h3>\n<p>Whenever any exceptions are thrown by laravel application, it will be added in laravel.log file on the storage directory. After some time, the size of the file increases and it may create the space issue or memory issue in the server. We will discuss various solutions to clear the laravel.log file to prevent those kinds of issues in the production server.<\/p>\n<h3>Description:<\/h3>\n<p>We can clear or empty the laravel.log file and for that we will discuss various methods to do it.<\/p>\n<h3>Preferred Solution:<\/h3>\n<p>We can make artisan commands so that it will get executed in a certain time period and we don&#8217;t have to remember to execute the commands every time.<\/p>\n<p>Put this in <mark>routes\/console.php<\/mark> then execute <mark>php artisan logs:clear<\/mark><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Artisan::command('logs:remove, function() {    \r\n    exec('rm -f ' . storage_path('logs\/*.log'));\r\n    exec('rm -f ' . base_path('*.log'));    \r\n    $this-&gt;comment('Logs have been removed!');    \r\n})-&gt;describe('Remove log files);\r\n<\/pre>\n<h3>Other Ways to clear logs from terminal:<\/h3>\n<p><strong>1.  Echoing an empty string to logs:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">echo \"\" &gt; storage\/logs\/laravel.log<\/pre>\n<p><strong>2. Try to use <mark>truncate<\/mark> it to make size of zero :<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">truncate -s 0 \/app\/storage\/logs\/laravel.log<\/pre>\n<p><strong>3. Using vim<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">$ vim laravel.log<\/pre>\n<p>Press keys:-  <mark>ggdG<\/mark><br \/>\nPress keys:-  <mark>:wq<\/mark> &#038; save and quit.<\/p>\n<p>Explanation: gg &#8211; moves cursor to first line<br \/>\n  d &#8211; delete<br \/>\n  G &#8211; to the end of the file<\/p>\n<p><strong>4. Using Tinker<\/strong><br \/>\nExecute command in terminal<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">php artisan tinker<\/pre>\n<p>Then execute<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">use Illuminate\\Support\\Facades\\Redis;\r\nRedis::resolve('horizon')-&gt;flushall();<\/pre>\n<p>Expected response:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&gt; Redis::resolve('horizon')-&gt;flushall(); = true<\/pre>\n<p><strong>5 Configure Log Rotation:<\/strong><\/p>\n<ul>\n<li>Laravel supports log rotation, which allows you to automatically manage log files and keep them within a specified size or time limit.<\/li>\n<li>Open the config\/logging.php file and locate the &#8216;channels&#8217; configuration section.<\/li>\n<li>Modify the configuration for the log channel you&#8217;re using (e.g., &#8216;single&#8217;, &#8216;daily&#8217;, etc.) and set the &#8216;days&#8217; or &#8216;max&#8217; value according to your requirements.<\/li>\n<li>For example, to rotate logs daily, you can use the &#8216;daily&#8217; channel with the &#8216;days&#8217; parameter set to a desired number of days:<\/li>\n<li>Code snippet<\/li>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">'channels' =&gt; [\r\n    'daily' =&gt; [\r\n        'driver' =&gt; 'daily',\r\n        'path' =&gt; storage_path('logs\/laravel.log'),\r\n        'days' =&gt; 7, \/\/ Rotate logs every 7 days\r\n    ],\r\n    \/\/ Other channels\r\n],\r\n<\/pre>\n<li>This configuration will automatically rotate logs based on the specified interval.<\/li>\n<\/ul>\n<p><strong>6  Use Laravel&#8217;s Artisan Command:<\/strong><br \/>\nLaravel provides an Artisan command to clear log files. Open your terminal and run the following command within your Laravel project directory:<br \/>\n<mark>php artisan log:clear<\/mark><\/p>\n<p><strong>7  Use Log Management Packages:<\/strong><br \/>\nThere are several third-party packages available that offer enhanced log management features for Laravel.<br \/>\nFor example, you can use the <mark>spatie\/laravel-log-cleanup<\/mark> package to automatically clean up log files based on various criteria such as file size, date, and more.<\/p>\n<p><strong>8 Manually Delete Log Files:<\/strong><br \/>\nIn Linux\/Mac, you can run the following command to delete log files older than a certain number of days (e.g., 7 days):<br \/>\n<mark>find \/path\/to\/your\/laravel\/storage\/logs -type f -mtime +7 -delete<\/mark><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: Whenever any exceptions are thrown by laravel application, it will be added in laravel.log file on the storage directory. After some time, the size of the file increases and it may create the space issue or memory issue in the server. We will discuss various solutions to clear the laravel.log file to prevent those [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8484,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[10],"tags":[],"class_list":["post-8132","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8132"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=8132"}],"version-history":[{"count":12,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8132\/revisions"}],"predecessor-version":[{"id":8144,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8132\/revisions\/8144"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/8484"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=8132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=8132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=8132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}