5

My rails4 application is running with docker. It runs a rake task for fetching messages from AWS SQS.

The problem I met is that logs can't show up in a console in time. The console doesn't show anything until exception/error comes. In other words, if my application works fine, no logs come to console. But if application went wrong, all the logs(info, warn and error) come together!

I already configure the config/production.rb as blow:

config.logger = Logger.new(STDOUT)
config.logger.level = Logger.const_get('INFO')
config.log_level = :info

I google 'rake task log was not working', but nothing useful. Is this a rails log problem or a rake task log problem, or maybe a docker problem?

Hoping that get some advice!

Eason Caizhen Liu
  • 459
  • 1
  • 5
  • 12
  • I'm seeing a similar issue where any rake task that I invoke doesn't output to STDOUT until the rake task has exited. I have tested this with the following code in a rake task: `loop { puts "in loop"; sleep 2 }`. While the task is running nothing appears in STDOUT, however once it completes both instances of "in loop" get output immediately. I haven't found why this is, but I'll post back here if I figure it out. – alexpls Feb 19 '17 at 11:05

2 Answers2

9

Try disabling output buffering to STDOUT. You can do this by adding this line to your rake task:

$stdout.sync = true
alexpls
  • 1,914
  • 20
  • 29
  • This worked for me. Note that I found this solution because `docker-compose logs -f ` was acting like it was buffered for me and I never saw the output. So the problem was Ruby 1.9.3's output buffering (not sure if Ruby's default buffering has been fixed in later versions, but I prefer to always do everything synchronously like in PHP because determinism is generally more important than performance, at least during development). More info at https://github.com/docker/compose/issues/1549 and https://github.com/midori-rb/midori.rb/issues/150#issuecomment-359744382 – Zack Morris Jun 21 '18 at 19:16
  • Forgot to mention that the defaults are: `$stdin.sync`: false, `$stdout.sync`: false, `$stderr.sync`: true. Although I'm not sure input buffering does anything. – Zack Morris Jun 21 '18 at 19:42
  • This also works on Rails 3, and can be put inside `development.rb` (or your preferred configuration file for your environment) – Nico Brenner Dec 13 '21 at 19:55
0

For Rails 4.x the log level is configuration

# Enable stdout logger
config.logger = Logger.new(STDOUT)

# Set log level
config.log_level = :ERROR

The logger level is set on the logger instance from config.log_level at: (https://github.com/rails/rails/blob/v4.2.4/railties/lib/rails/application/bootstrap.rb#L70)

JohnPaul
  • 714
  • 5
  • 14
  • 1
    Thanks, man! But I don't think this would fix my problem. The logs, including info, warning and error, wouldn't show up in time. They only come to the console when exception happends. – Eason Caizhen Liu Apr 05 '16 at 05:57
  • **ENV Configuration** # default :ERROR config.log_level = ENV.fetch("LOG_LEVEL", "ERROR") **Run test from shell** # Log level :INFO (the value is uppercased in bootstrap.rb) $ LOG_LEVEL=info rake test # Log level :ERROR $ LOG_LEVEL=debug rake test – JohnPaul Apr 05 '16 at 06:07