What is the best way to rotate Ruby on Rails log files daily?
I saw posts in the past. They recommended using logrotate and enable copytruncate option to avoid restarting Rails.
However, logs are lost at small time slice between copying the file and truncating it.
My project requirement does not lose logs, so we chose cronolog with ruby's stdlib Logger.
The code like the following:
config.logger = Logger.new(IO.popen("/usr/sbin/cronolog #{config.paths['log'].first}.%Y%m%d", "w"))
It's mostly works correctly, but logs are mixed when logging data size more than PIPE_BUF that because using PIPE.
So what is recommended way to rotate logs in such situation?
I'm new to ROR and I"m having trouble with all the text that rails spits to the terminal window. Primarily, the html from my web pages get repeated in the terminal window and I'm really just wanting to see the important stuff like sql queries and error messages. I'm wasting a lot of time scrolling throughout the terminal window trying to find what I need b/c of all the HTML that fills up the screen.
Is there an option to disable the 'source of your encoding' output?
Thanks.
You could raise your log level
The rails guide gives a good example
http://guides.rubyonrails.org/debugging_rails_applications.html#log-levels
Make sure if you are in development to change it in config/environments/development.rb
I don't know if this will really solve your problem though because I am unsure of exactly what output you are looking for. A higher log level may throw out the baby with the bathwater.
I help to maintain a Rails website. It's running JRuby 1.5.5, Rails 2.3.10, on a Solaris Sparc machine. I have a problem related to logging.
To stop our logfiles growing too large and filling the disk, we're using the log-shifting that's built in to the Logger class. In config/environments/production.rb we have:
config.logger = Logger.new(config.log_path, 10, 100.megabyte)
Which should rotate the logfiles when they reach 100 megabytes, and only keep 10 files.
The problem is two-fold: Rails is not rotating the logs properly, and it is keeping open the old log file to write to it -- but what it is writing is just repeated content of a few requests. So if I do ls -l log I see something like this:
-rw-r--r-- 83040892 Oct 4 15:07 production.log
-rw-r--r-- 3303158664 Oct 4 15:07 production.log.0
-rw-r--r-- 104857616 Oct 2 23:13 production.log.1
-rw-r--r-- 104857618 Oct 1 17:12 production.log.2
Note how the most recently cycled log is still open and still being written to (running pfiles confirms that it the Rails server still has three file handles to the log). Note also that it has reached 3 gigabytes in two days, where usually we do 100MB a day. This is because it is full of repeated requests. I can't easily paste it in here, but the log is full of the same 1000 line chunk of requests from 18:50 on Oct 3 (which I believe is the point at which the logs rotated), printed over and over again. From past experience, the log file will keep filling with this repeated content until the disk fills up.
Is log shifting/Rails logging just plain broken? (There's nothing odd about our logfile usage: we don't do any direct logging, it all just comes from the Rails framework.) The obvious next step is to try something like logrotate, but if Rails is refusing to close old log files and is writing junk to them forever, I suspect it won't solve my problem (because the log will never be closed, and thus the disk space never recovered).
The symptom seems to be that one old logfile still keeps getting used, although you successfully rotated the logs.
The cause is most likely that one or more of your Rails instances or threads is still using the old file handle.
The solution is to make sure that all the Rails instances restart completely after logs are rotated, so they all use the new file handle / name.
Use logrotate instead of config.logger to rotate your logs!
I'd suggest to use the UNIX logrotate to rotate your logs, instead of config.logger.
IMHO that's a better solution, more reliable, you have more control over the log rotation, and you can provide some post-rotation commands to restart your Rails processes. (either via logrotate's postrotate or endscript option)
See:
http://www.opencsw.org/packages/logrotate/ (logrotate Package for Solaris)
http://www.thegeekstuff.com/2010/07/logrotate-examples/ (logrotate tutorial with examples)
http://linux.die.net/man/8/logrotate
Can you use Unicorn?
- Unicorn is has built-in support for re-opening all log files in your application via USR1 signal - this allows logrotate to rotate files atomically...
- Unicorn keeps track of and restarts it's workers! You can kill the workers after log rotation and Unicorn will restart them, making sure they use the new log file.
See: https://github.com/blog/517-unicorn (many advantages for Unicorn over Mongrel)
If you're using Mongrel and can't switch to Unicorn:
use logrotate, and restart your Mongrels via the postrotate option.
hope this helps..
I've always used the platform's log rotation mechanism when dealing with the Rails log files. Following the advice from http://www.nullislove.com/2007/09/10/rotating-rails-log-files/ and because I run Passenger from http://overstimulate.com/articles/logrotate-rails-passenger as well.
The first method uses the logrotate copytruncate method of creating the new log file, so processes that still have a handle to it will always write to the current log file.
Other things to check on the server are:
Make sure that none of the gems or plugins have a handle to the Logger inside the ruby context.
Since you're using JRuby ensure that there isn't a stuck/runaway thread somewhere that's trying to fulfill a request but getting stuck logging.
Just like with Passenger, consider restarting the Rails server processes every now and again. I know this is effectively a hack but it might work.
Neil,
I don't know if this works for your particular situation, but I was having a similar problem and I think I just solved it. In my case, I had two symptoms. The first was the same issue as you -- my log rotation was screwy...in particular, the production.log.1 file was kept open and continued logging to is was happening while production.log was also getting logged to. The second symptom was that the log files ownerships and group memberships would keep changing to root. My Rails app is deployed via Capistrano, using a 'deployer' user, so I'd get all sorts of neat errors whenever the app tried to write to a log file that was no longer owned by deployer.
I'm embarrassed to say how long it took me to realize what the cause of both problems was. Somewhere along the way, I updated cron with the app's crontab as root. This must have been when I messing around at the command prompt...if I had just stayed with my deployment recipe via Capistrano, I wouldn't have inadvertently done that. In any case, I finally looked in /var/spool/cron/crontabs and I found two copies of my crontab file...one for deployer and one for root. So, the processes that cron fired off for my app were getting duplicated -- one was run under deployer and a second under root. It was that second one that was screwing things up. Once I deleted root's crontab, all was better.
Some caveats: on my setup, there were no non-app-related tasks in root's crontab, i.e. it was an exact duplicate of deployer's crontab...so deleting it had no side effects for me. Also, my server is running Ubuntu...the path to your crontabs may be different.
Hope that helps.
David
I think you forgot the 's' in megabytes
or instead use something like this
config.logger = Logger.new(config.log_path, 10, 102400)
also check this link it's very helpful
http://railsillustrated.com/logger-tricks.html
Hey all. I've got a new app up and running on a new ubuntu server. It's working fine generally but i can't get ar_sendmail working. I'm following the instructions on this page:
http://www.ameravant.com/posts/sending-tons-of-emails-in-ruby-on-rails-with-ar_mailer
The setup is all done, ie i can "deliver mails" which just saves records in my Email table. Now i want to get the ar_sendmail daemon running to actually send them. (so i'm at 'Running ar_sendmail in daemon mode' in that web page).
First thing:
ar_sendmail --mailq
>>ar_sendmail: command not found
Ok...so, where is ar_sendmail? I have a look and there's an ar_sendmail file in the bin folder of the ar_mailer plugin, so i add the location of that to my path. I don't know if this was the right thing to do or not.
Ok, so try again.
ar_sendmail --mailq
/var/www/apps/millionaire/vendor/plugins/ar_mailer/bin/ar_sendmail:3:in `require': no such file to load -- action_mailer/ar_sendmail (LoadError)
from /var/www/apps/millionaire/vendor/plugins/ar_mailer/bin/ar_sendmail:3
hmm. Here's the offending file, there's not much there.
#!/usr/bin/env ruby
require 'action_mailer/ar_sendmail'
ActionMailer::ARSendmail.run
ok...so it literally is just trying to require this and can't find it. The file, action_mailer/ar_sendmail.rb is in the ar_mailer plugin, in it's lib folder. So, given that it's being called from inside the plugin, it should be able to see this right?
I've got a feeling that i'm way off the track here and have missed something simple. Can anyone set me straight? I'm using rails 2.3.4 in case that's relevant.
EDIT - i just realised something kind of dumb: when i call ar_sendmail from the command line like this, i'm just loading that one file, which doesn't know where it's supposed to look for the rest of the stuff, i think. Which really makes me think that i'm not trying to run the right thing. Is the ar_sendmail daemon a seperate program altogether, that i would get with apt_get or something?
EDIT2 - i made some progress by installing the ar_mailer gem (which the guide said i shouldn't do) and that does seem to run. It's sending some mail request somewhere and clearing the Email table of pending emails. Running ar_sendmail in -ov (oneshot verbal) mode i see it report this for example:
sent email 00000000019 from from#address.com to to#address.com: #
So, it actually looks like it's working now and i just need to set up the ACTUAL THING WHICH SENDS EMAILS. sigh. still grateful for any advice.
thanks, max
What you need is an smtp server. Checkout www.sendgrid.com.
I am getting up and running with Spree in production on a 256MB Slicehost slice with MySQL, Mongrel, and Nginx. I am getting this error every time I try to access any URL in the app:
SystemStackError (stack level too deep):
compass (0.8.17) lib/compass/app_integration/rails/action_controller.rb:7:in `process'
vendor/plugins/haml/rails/./lib/sass/plugin/rack.rb:44:in `call'
(Some more test url requests are in this pastie)
What are the advanced (but free and open source) debugging strategies/tools I should use while in production mode so I can do better at solving this problem?
It's slightly different each time depending on the controller. I have tried increasing my stack size, but that doesn't help because there's probably just some loop I can't find.
The question is, how do I get a more detailed report of the exact output while in production?
I am using Deprec and Capistrano to execute all server side commands. I am running a mongrel cluster of 3. The log files I see are:
current/log$ ls
development.log
testsite.com-error.log
mongrel.8000.log
mongrel.8001.log
mongrel.8002.log
mongrel.log
production.log
testsite.com-access.log
I'm just looking for some advanced debugging strategies/tools while in production mode so I can do better at solving this problem. Maybe some cap tasks for streaming/debugging (or conditionally break-pointing!) the production app (really, a staging app) locally (do people do that?), or something that spits out every single thing that executes in code?
Thanks
I'd check all of your gems and plugins and any other dependencies your application might have: I'd guess that you're missing something (or have the wrong version of something) and it's failing badly enough that it can't even spit out a can't find require message.
check your action controller. On line 7 is most likely where the loop occurs.
Seems you have initiated some endless recursion on line 7 of your controller. Check if you are calling the same method inside the method.