# Rake tasks not writing to log file anymore

**URL:** <https://www.ruby-forum.com/t/rake-tasks-not-writing-to-log-file-anymore/134299>\
**Category:** Rails\
**Created:** [April 9, 2008, 5:46pm UTC](https://www.ruby-forum.com/t/rake-tasks-not-writing-to-log-file-anymore/134299 "2008-04-09T17:46:40Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [April 9, 2008, 5:46pm UTC](https://www.ruby-forum.com/t/rake-tasks-not-writing-to-log-file-anymore/134299/1 "2008-04-09T17:46:40Z")

</div>

I have a Rails app, and some custom rake tasks which are called by  
cron jobs every hour or so. The rake tasks would just call some  
methods on my models to perform some work. The models in turn use  
RAILS\_DEFAULT\_LOGGER.info to emit messages to the log file as needed.

For a good long time everything was working fine, but recently I  
noticed that my rake tasks are not resulting in any log file  
messages. I know that they are running because the database is  
getting get updated as expected.

I then tried to manually run script/console in production mode, and  
manually did this:

> > RAILS\_DEFAULT\_LOGGER.info “hello world”

and this did not appear in the log file either.

Anyone have any ideas?

Thanks!  
Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [April 9, 2008, 6:40pm UTC](https://www.ruby-forum.com/t/rake-tasks-not-writing-to-log-file-anymore/134299/2 "2008-04-09T18:40:20Z")

</div>

Jeff C. wrote:

> I then tried to manually run script/console in production mode, and  
> manually did this:
> 
> > > RAILS\_DEFAULT\_LOGGER.info “hello world”
> 
> and this did not appear in the log file either.
> 
> Anyone have any ideas?

What response to you get in console?

Maybe clear your log file?

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [April 9, 2008, 7:57pm UTC](https://www.ruby-forum.com/t/rake-tasks-not-writing-to-log-file-anymore/134299/3 "2008-04-09T19:57:06Z")

</div>

On Apr 9, 11:40 am, Daniel W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> What response to you get in console?
> 
> ## Maybe clear your log file?

Sorry I should have mentioned that originally. I get a good response  
from the console:

$ script/console production  
Loading production environment (Rails 2.0.2)

> > RAILS\_DEFAULT\_LOGGER.info “hello world”  
> > =\> “hello world\n”

but when I tail the log I don’t see “hello world” anywhere (even a  
full grep fails).

I also tried stopping the mongrel cluster, then rake log:clear, then  
started the cluster again, and still no output.

Output from the Rails app itself is working great - I see all the  
usual rails logging as it processes requests, etc.

Truly stumped on this one.

Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [April 10, 2008, 8:30am UTC](https://www.ruby-forum.com/t/rake-tasks-not-writing-to-log-file-anymore/134299/4 "2008-04-10T08:30:11Z")

</div>

What have you got your log level set to? Try sending your logger  
object the method error “hello world”

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)  
VIDEO #3 out NOW!  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [April 10, 2008, 3:21pm UTC](https://www.ruby-forum.com/t/rake-tasks-not-writing-to-log-file-anymore/134299/5 "2008-04-10T15:21:11Z")

</div>

On Apr 10, 1:29 am, Julian L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What have you got your log level set to?

I haven’t knowingly changed it from the way Rails configures it for  
production. I can do this script/console:

> > RAILS\_DEFAULT\_LOGGER.info?  
> > true

That was a good thought, though… any others? 🙂

Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [April 10, 2008, 4:22pm UTC](https://www.ruby-forum.com/t/rake-tasks-not-writing-to-log-file-anymore/134299/6 "2008-04-10T16:22:14Z")

</div>

On Apr 10, 8:43 am, Thorsten M. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> did you delete an recreate the log file?  
> you may have messed the file permissions.

Another good suggestion! But file permissions seem to be ok.

The good news is I’ve somehow worked around it by putting this as the  
first line in all of my rake tasks:

RAILS\_DEFAULT\_LOGGER.auto\_flushing = true

Now I’m seeing all my output again, yay!

But why should I have to do this? I think (but can’t prove) that this  
broke when I upgraded from 1.2.6 to 2.0.2. But if it was a Rails bug,  
I would think this would be biting a lot of people, not just me.

Also I’m concerned about the effect this change will have on  
performance, so I hope someone can shed some light here.

Thanks!  
Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [April 29, 2008, 4:40am UTC](https://www.ruby-forum.com/t/rake-tasks-not-writing-to-log-file-anymore/134299/7 "2008-04-29T04:40:00Z")

</div>

This bit me too. It started happening when I upgraded from 1.2.6 to  
2.0.0,  
just like you.Thanks for finding the workaround!

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [April 10, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/rake-tasks-not-writing-to-log-file-anymore/134299/8 "2008-04-10T15:43:41Z")

</div>

did you delete an recreate the log file?  
you may have messed the file permissions.

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [May 6, 2008, 11:42pm UTC](https://www.ruby-forum.com/t/rake-tasks-not-writing-to-log-file-anymore/134299/9 "2008-05-06T23:42:20Z")

</div>

I just spent hours trying to figure out why my rake task was not  
logging in production mode. This has to be a Rails bug. I haven’t  
found any tickets on it yet though. The thing about ths is that it  
seems to work OK in development mode. Does auto\_loading get set to  
true somewhere in the development environment? It’s not set in my file  
but I don’t know if it gets set somewhere in the bowels of Rails.

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [May 31, 2008, 6:13pm UTC](https://www.ruby-forum.com/t/rake-tasks-not-writing-to-log-file-anymore/134299/10 "2008-05-31T18:13:26Z")

</div>

Jared Mehle wrote:

> The thing about ths is that it seems to work OK in development mode.  
> Does auto\_loading get set to true somewhere in the development  
> environment?

I guess you’re asking about “auto\_flushing”, not “auto\_loading”?

In 2.0.2, /railties/lib/initializer.rb, line 254:

> <https://github.com/github/rails/blob/c8da518bbfedc2a06b1d96912ddae00e57f21748/railties/lib/initializer.rb#L254>

logger.auto\_flushing = false if configuration.environment ==  
“production”

This overwrites the default as set in active\_support/buffered\_logger,  
line 41:

> <https://github.com/github/rails/blob/c8da518bbfedc2a06b1d96912ddae00e57f21748/activesupport/lib/active_support/buffered_logger.rb#L41>

@auto\_flushing = 1

By the way: in upcoming releases you’ll find an additional different  
default:

[http://github.com/github/rails/tree/master/railties/lib/initializer.rb#L345-348](http://github.com/github/rails/tree/master/railties/lib/initializer.rb#L345-348)

if configuration.environment == “production”  
logger.auto\_flushing = false  
logger.set\_non\_blocking\_io  
end

Arjan.

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [October 4, 2008, 4:44am UTC](https://www.ruby-forum.com/t/rake-tasks-not-writing-to-log-file-anymore/134299/11 "2008-10-04T04:44:53Z")

</div>

Just say

RAILS\_DEFAULT\_LOGGER.flush

at the end of your Rake task.

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [June 5, 2008, 11:32am UTC](https://www.ruby-forum.com/t/rake-tasks-not-writing-to-log-file-anymore/134299/12 "2008-06-05T11:32:33Z")

</div>

“But if it was a Rails bug, I would think this would be biting a lot  
of people, not just me.”

Thanks for your post. It took me all day just to find this page. It  
took the morning just to figure out that this was the problem. Since I  
upgraded to Rail 2.0.2, all “rake db:migrate RAILS\_ENV=production”  
commands will fail if the migration contains any ActiveRecord commands  
(that would normally be logged in development mode) – i.e. commands  
like [Model].find(:all), [object].update\_attribute(…), etc. No  
problem if the migration file contains only regular migration commands  
(e.g. add\_column, drop\_table, etc.).

I tried your suggestion (RAILS\_DEFAULT\_LOGGER.auto\_flushing = true)  
but that only made things worse for me. That is, without that command,  
the rake command would eventually break and die (MySql lock would time  
out). But with this command, rake just hangs there forever.

Why this bug hasn’t been found yet? I suspect it’s a pretty narrow  
case involving ActiveRecord, rake, and logger. My situation would seem  
common enough to have been found by others. But one can alway work  
around elegance to keep the trains moving.

As for me, after 7 hours of banging my head on this obscure issue,  
I’ll just go back to a truly awful workaround (i.e. sftp ‘get’ the  
production db to my local machine, slurp that data into my local  
devlopment db, run the migration locally, sftp ‘put’ the updated db  
back up to the server, slurp it in into the production db). Maybe on  
another day I’ll do more than browse through the rails code in  
bewilderment, and actually find, fix, and publish the bug/fix. But  
that day is not today. 🙂
