How log rotation works in rails

16 views
Skip to first unread message

Loganathan Sellapa

unread,
Jan 1, 2015, 9:01:27 AM1/1/15
to singap...@googlegroups.com
Hi All, 

I have implemented the log rotation feature for my application, and I am seeing unexpected behaviors in the log rotation.

​config.logger =​
 
​::​
Logger.new('log/production.log', 3, 50.kilobytes)

ASSUMPTIONS:  

I have assumed that the above code will rotate the log whenever the log file reaches 50KB, as same as the below manner

1. When I restart the application with log rotation changes, old log file needs to be moved to backup logs if it is has content more than 50KB
2. production.log reaches 50 KB first time
3. production.log content will be moved to new file called  production.log.0
4. production.log again reaches 50 KB
5. production.log.0 content will be moved completely to production.log.1
6. production.log content moved to  production.log.0 and the production.log becomes  empty.
7. The contents of the the log files production.log, production.log.0 and production.log.1 are needs to be in sequentia order like below
  •      production.log has content FROM 11AM to 12PM
  •      production.log.0  has content FROM 10AM to 11AM
  •      production.log.1  has content FROM 9AM to 10AM

Actual behavior:

1. When I start the application after log rotation settings, my current log is cleared completely and no backup is taken
2. Then I process some request in my application
3. production.log reaches 50 KB first time
4. Part of the content of  production.log is moved to production.log.0
5. Part of the content of  production.log.0 is moved to production.log.1
6. The contents of the  log files production.log,  production.log.0 and production.log.1 are not in sequential order, instead it has the content as like below

  •      production.log has content FROM 11AM to 12PM
  •      production.log.0  has content FROM 10AM to 1AM
  •      production.log.1  has content FROM 9AM to 10AM, and some contents from 11.20AM to 11.25AM
Can anybody let me know
​the way that log rotation works?

regards,
Loganathan
Mob: +91 7760780741 | +91 9944414388
Skype: loganathan.sellappa
ViewMe



Winston Teo

unread,
Jan 1, 2015, 9:31:43 AM1/1/15
to singap...@googlegroups.com
Interesting. I didn't know you can do `
​config.logger =​
  
​::​
Logger.new('log/production.log', 3, 50.kilobytes)
`!

Anyway, I just tried this on development, and it does works as per your assumptions.

It is strange that you have a split of content into two different log files though.. Hmm..


Cheers,
Winston

--
You received this message because you are subscribed to the Google Groups "Singapore Ruby Brigade" group.
To unsubscribe from this group and stop receiving emails from it, send an email to singapore-rb...@googlegroups.com.
To post to this group, send email to singap...@googlegroups.com.
Visit this group at http://groups.google.com/group/singapore-rb.
For more options, visit https://groups.google.com/d/optout.

Meng Weng Wong

unread,
Jan 1, 2015, 10:52:29 AM1/1/15
to Singapore Rb
it sounds like your code might be executing more than once, hence clobbering – is it possible you've got parallel processes trying to do the same rotation?

anyway, syslog-style (0,1,2) log rotation is so 20th century. i bet by now someone out there has written a post titled "log rotation considered harmful".

i would prefer idempotent logging – if the application decides the previous log exceeds threshold then just start writing to a new logfile.yyyyMMdd-hhmmss, close the old output filehandle, and update the "logfile" symlink to the new logfile.

this approach has various properties which uses native filesystem atomicity to eliminate application-level race conditions.

if you have multiple writers you could have a logfile.$$ symlink. if you have multiple writers you pretty much need a third party to marshal for synchronicity. why not just use Syslog?


Reply all
Reply to author
Forward
0 new messages