Log issue in Rails4 with Docker running rake task
Asked Answered
P

2

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!

Public answered 5/4, 2016 at 5:33 Comment(1)
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.Radiant
R
9

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

$stdout.sync = true
Radiant answered 20/2, 2017 at 12:5 Comment(3)
This worked for me. Note that I found this solution because docker-compose logs -f <container> 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 github.com/docker/compose/issues/1549 and github.com/midori-rb/midori.rb/issues/…Snowbird
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.Snowbird
This also works on Rails 3, and can be put inside development.rb (or your preferred configuration file for your environment)Partin
A
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)

Atheism answered 5/4, 2016 at 5:43 Comment(2)
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.Public
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 testAtheism

© 2022 - 2024 — McMap. All rights reserved.