We have set logger as STDOUT in the rails configuration.
config.log_level = :info
config.logger = Logger.new(STDOUT)
We are expecting these logs in kubectl logs as well as datadog logs but STDOUT is not showing up there. We tried below code to test it.
def method_name
system('echo testing logging') - this shows up in kubectl/datadog logs
Rails.logger.info('STDOUT - testing logging') - this does not show up in kubectl/datadog log
end
We have changed the logger setting to output the logs to container file descriptor.
Reference: https://blog.eq8.eu/til/ruby-logs-and-puts-not-shown-in-docker-container-logs.html
Try to use the default config and make sure to set the environment variable RAILS_LOG_TO_STDOUT=true, for your deployment/replica set, and in production mode (RAILS_ENV=production). (In dev mode it always logs to console per default).
Actually, the official rails docker images used to have that set, but the newer recommended ruby docker images - of course - do not have Rails specific environment variables set.
(more: search for RAILS_LOG_TO_STDOUT in the release notes here, and see PR here)
Related
I created a new environment called "superdev", which is just like development.
# environments/superdev.rb
require Rails.root.join("config/environments/development")
Problem is, when I do RAILS_ENV=superdev rails server, I don't get any log output. Even when I set config.log_level = :debug
I've set the Gemfile groups to include superdev, created the DB, and the environment works fine otherwise. Just no log output. What gives?
You may need to enable logging to STDOUT. You can use either an environment variable, or an option to the rails server command:
RAILS_LOG_TO_STDOUT=true rails server -e <your-env>
or
rails server -e <your-env> --log-to-stdout=true
Logging to STDOUT is only enabled by default for the development environment, and needs to be explicitly enabled for others.
I currently have this issue that when there's an error, the logs in my terminal aren't informative. The logs are only writing to the log files but my terminal output doesn't show anything. This was super confusing. How do I change this?
What is the Rails default behavior anyway when it comes to logging?
In Rails, by default, each log is created under Rails.root/log/ and the log file is named after the environment in which the application is running.
But if you want to change you can specify something like below in config/application.rb which would throw out logs on STDOUT.
config.logger = Logger.new(STDOUT)
See the Rails guides for more info.
What I usually found easy was to tail the log on console like $ tail -f log/development.log and force/see the output of log file on console.
I managed to log into a custom log file using this configuration:
Delayed::Worker.logger =
ActiveSupport::BufferedLogger.new("log/#{Rails.env}_delayed_jobs.log", Rails.logger.level)
It's working locally. I can see the file change and all the logs I want.
But the problem is that when I deploy to Heroku and run the job, and try to read the contents of the file through the console, I don't get the expected results.
irb(main):070:0* File.read('/app/log/production_delayed_jobs.log')
=> "# Logfile created on 2013-06-20 08:36:43 +0000 by logger.rb/25413\n"
Note that I set the loggin level to :debug in production. In config/environments/production.rb I have this configuration:
config.log_level = :debug
Basically #yoav is right, you can't write to your own log files on heroku (no file system access). what you can do is described in the heroku logs right here: https://devcenter.heroku.com/articles/logging#writing-to-your-log
I have a Rails app running on Heroku, it runs fine. I had it running with debug level logging, but now I switched back to INFO. What other levels of debugging are available? And when running for production, what level of logging should I use?
On Heroku you can set your log level by using the LOG_LEVEL environment variable
$ heroku config:set LOG_LEVEL=DEBUG
Valid values include DEBUG, INFO, WARN, ERROR, and FATAL. Alternatively you can set this value in your environment config:
config.log_level = :debug
If both are set LOG_LEVEL will take precedence. You'll most likely want INFO in production.
See the rails-stdout-logging gem
Logging on Heroku is just a stream of text lines. So you can log anything you want. You can also filter through that stream, use add-ons for enhanced search, and get a syslog drain of your logs.
Presently I'm running a Rails 3.1.x app atop Heroku Celadon Cedar and it seems that log verbosity is very much lacking. I have set the log level to DEBUG a la heroku config:add LOG_LEVEL=DEBUG --app app_name, which matched up to their recommendation, however beyond that I cannot seem to pull in the log/* file contents.
Changing from Thin to Unicorn did increase verbosity slightly, but only in web worker requests. I still cannot pull down the db requests and so forth.
What is the best way to maximize log verbosity via the heroku "drain" mechanism so that one can pull all instance logs into one cohesive log?
(Ideally I'd like to include a method to dump this into one of my own log servers as this is just a pain the rear not being able to readily look at specific events and surrounding conditions in time.)
In staging.rb, production.rb or whatever environment you're running, you can insert the following:
STDOUT.sync = true
logger = Logger.new(STDOUT)
logger.level = 0 # Must be numeric here - 0 :debug, 1 :info, 2 :warn, 3 :error, and 4 :fatal
# NOTE: with 0 you're going to get all DB calls, etc.
Rails.logger = Rails.application.config.logger = logger
### NOTE: Be sure to comment out these:
# See everything in the log (default is :info)
# config.log_level = :debug # Commented out as per Chris' instructions from Heroku
# Use a different logger for distributed setups
# config.logger = SyslogLogger.new
I think the ActiveRecord logger object sends all SQL query logs with a DEBUG severity level. You may need to adjust the Rails.logger log level to get those in production too.
Since you are interested in dumping all Rails and Heroku logs to an external service, I can suggest you try the Progstr Logger free add-on. (Disclaimer, I work for Progstr). The add-on has a Ruby gem that will collect all Rails logs (everything that usually ends up in log/*). It will also set up a log drain that will fetch all system-level Heroku logs. In addition you get a cool web UI that lets you easily search for logs and some other nice features.