# Purging logs

**URL:** <https://www.ruby-forum.com/t/purging-logs/143247>\
**Category:** Rails\
**Created:** [July 16, 2008, 6:37pm UTC](https://www.ruby-forum.com/t/purging-logs/143247 "2008-07-16T18:37:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 16, 2008, 6:37pm UTC](https://www.ruby-forum.com/t/purging-logs/143247/1 "2008-07-16T18:37:13Z")

</div>

Hello.

Sorry if this is too simple a question. How do I go about  
automatically purging logs of an application in production? The  
application runs on a Windows server.

Thanks.

Pepe

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 16, 2008, 6:44pm UTC](https://www.ruby-forum.com/t/purging-logs/143247/2 "2008-07-16T18:44:49Z")

</div>

$ rake -T log  
(in /Users/philip/Work/Careers.org/web/trunk)  
rake log:clear # Truncates all \*.log files in log/ to zero bytes

Whether or not you need to restart any services after that depends on  
your setup.

-philip

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 16, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/purging-logs/143247/3 "2008-07-16T18:54:46Z")

</div>

I’m sorry but I am not very familiar with rake and I don’t understand  
your posting.

Doesn’t the $ in your command mean that it is run manually? What I  
need is for the application/server to take care of the logs itself and  
delete log information older than, for example, x amount of days.

Also, why would I need to restart services if I am just deleting data  
from a text file such as ‘production.log’ or ‘server.log’? Would the  
files need to be locked to purge them?

Thanks

Pepe

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 16, 2008, 10:53pm UTC](https://www.ruby-forum.com/t/purging-logs/143247/4 "2008-07-16T22:53:51Z")

</div>

Thanks!

Pepe

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 16, 2008, 8:16pm UTC](https://www.ruby-forum.com/t/purging-logs/143247/5 "2008-07-16T20:16:11Z")

</div>

On Jul 16, 2008, at 9:54 AM, pepe wrote:

> I’m sorry but I am not very familiar with rake and I don’t understand  
> your posting.
> 
> Doesn’t the $ in your command mean that it is run manually?

Yes, but you could also run that via cron to make it automatic.

> What I  
> need is for the application/server to take care of the logs itself and  
> delete log information older than, for example, x amount of days.

To do that you need to configure one of the other logging mechanism’s  
to split the logs up by day and automatically purge them. Google  
around for plugins to do this (if it isn’t already built in to rails,  
i don’t remember)

> Also, why would I need to restart services if I am just deleting data  
> from a text file such as ‘production.log’ or ‘server.log’? Would the  
> files need to be locked to purge them?

If another process has a file open when you remove it, the OS doesn’t  
really remove it until that first process closes the file. Which if  
it’s a web server, it may never do. So you think you’ve removed it,  
but it will keep increasing in size.

-philip

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 17, 2008, 9:19am UTC](https://www.ruby-forum.com/t/purging-logs/143247/6 "2008-07-17T09:19:41Z")

</div>

On 16 Jul 2008, at 20:15, Philip H. wrote:

> > What I  
> > need is for the application/server to take care of the logs itself  
> > and  
> > delete log information older than, for example, x amount of days.
> 
> To do that you need to configure one of the other logging mechanism’s  
> to split the logs up by day and automatically purge them. Google  
> around for plugins to do this (if it isn’t already built in to rails,  
> i don’t remember)

That magic mechanism is called logrotate and it’s built into any Unix/  
Linux based system.

In /etc/logrotate.d/, create a new file (you can name it whatever you  
want, the app name for example), then put this in it:

/path/to/your/railsapp/shared/log/production.log {  
copytruncate  
daily  
missingok  
rotate 14  
compress  
delaycompress  
}

There should already be a crontab that fires it every day, since  
that’s what your Linux does to rotate its own logs.

And strangle enough, that information could be found on  
[http://wiki.rubyonrails.org/rails/pages/DeploymentTips](http://wiki.rubyonrails.org/rails/pages/DeploymentTips)  
😉

Best regards

Peter De Berdt
