We have a Rails 6 app, which is being deployed to an Ubuntu server with the following setup:
Nginx 1.17 & passenger 6, releases are deployed using capistrano v3
I could find the following log files:
Nginx (access & error logs):
/var/log/nginx/access.log & /var/log/nginx/error.log
Passenger logs: (on staging env)
/home/deploy/myapp/current/log/staging.log
The logs written in Passenger logs (which is the one that shall have the application log as I understand), has only Ruby logs for the Table migrations and creation but not ruby application log
So none of the above logs have the Rails application logs (i.e.: the log that shows Rails details like the rails s output in the dev env) & the nginx access log shows only the static assets logs when a page is loaded but not the ruby or the sql logs
The logical place to me, staging.log file, only has stale log data & doesn't get any new Rails logs written to it
Where is the Rails application logs please?
Rails writes its logs by default to the log/ENVIRONMENT.log file (where ENVIRONMENT is replaced by the value of your RAILS_ENV environment variable.
On your production environment, the logs are thus likely written to /home/deploy/myapp/current/log/production.log
Note that Rails tries to create that file if it doesn't exist. If the user running your app doesn't have the necessary permissions to create (or write to) the file, it doesn't write logs at all.
Guided by Nathan comment & credit for solving this goes to him:
The cause of the issue is:
In the staging.rb, I had this config for the logs
& the env variable was defined as export RAILS_LOG_TO_STDOUT='enabled'
# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new
if ENV["RAILS_LOG_TO_STDOUT"].present?
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
end
So removing the env variable:
RAILS_LOG_TO_STDOUT='enabled'
Allowed the Rails log to show as expected in the correct directory:
/home/user/project-name/current/log/staging.log
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 have a Rails 5.0 app running in Passenger with Nginx as the web server.
I can't seem to get the app to output its logs into a file in log/ under the Rails root directory.
RAILS_LOG_TO_STDOUT is NOT set in my environment.
I can confirm that the log file I specify in config/logs/production.rb gets created, but subsequent logger output is not sent to it. My log_level is debug. Instead, the app sends all log output to STDOUT which Passenger dutifully appends to its own log file.
Why?
I figured it out when someone discussing logging for Rails apps mentioned Heroku's rails_12factor gem. I had moved this app from a previous Heroku deployment and didn't realize that gem would capture log output even when not deployed within a Heroku dyno.
The log file is used when the gem is removed.
I have a rails 5 app that I'd like to push to heroku, in which I have a logger that creates a daily log file, which I use to print out various pieces of status information. This works in development, but I get an error when I try to push it to heroku (after pushing everything to git).
The error I receive: "Errno::ENOENT: No such file or directory # rb_sysopen - /tmp/build_e3fe50d2e37e0a51f1bc7d94dd1fc2f3/log/daily_logs_production/2016-12-19.log
"
Here is the relevant portion from production.rb:
if ENV["RAILS_LOG_TO_STDOUT"].present?
logger = ActiveSupport::Logger.new("#{Rails.root}/log/daily_logs_production/#{Time.now.strftime('%Y-%m-%d')}.log", 'daily')
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
end
Any idea what is wrong? Thank you!
whether or not you are getting an error can be neglected.
due to the ephemeral filesystem of heroku dynos you should not use it for such purposes.
install a plugin of the Logging category instead.
I've created a new environment file in my Rails 4 app (config/environments/staging.rb). The settings in this file are exactly those found in the default development.rb file created when the app was generated.
When firing up WEBrick in the console using this environment (rails s -e staging or RAILS_ENV=staging rails s), I don't see anything written to STDOUT. I can see the log file for the staging environment being written to, but nothing in STDOUT. I understand I can tail the file, but I was wondering if there was a config setting to handle this. I can also redirect the Rails logger to output everything to STDOUT, but how can I get my custom environment to write to both STDOUT and the log file?
I can replicate this on both OSX and Windows with a new Rails 4 app, and the default environments work as expected.
Figured it out. Modify your environment config file to use the Rails::Rack::LogTailer middleware:
# config/environments/staging.rb
Rails.application.configure do
config.middleware.insert_before(Rails::Rack::Logger, Rails::Rack::LogTailer)
end
I have several loggers in my rails app which are initialized like this in production:
log_file = File.open("#{Rails.root}/log/my_log_#{Rails.env}.log", 'a')
log_file.sync = !Rails.env.production?
LOG = Logger.new(log_file)
I use the above as follows:
LOG << "my message\n"
Note that log_file.sync is false in production, because I don't want to write to disk on every message logged. The problem I am noticing is that when I stop my rails apache server (sudo /etc/init.d/httpd stop), my LOG is not flushed. As a result, I lose messages. Is this expected behavior?
The odd part is that I troubleshooted by using rails console (production) to write a bunch of log messages to said LOG. When I closed the rails console, my LOG flushed as expected. So this makes me believe there is something wrong with how I am shutting down my rails instance in Passenger.
So how come LOG flushes properly in rails console but not in the actual Passenger server? What am I missing?
I am running Rails 3.2.8 and using Apache w/ Passenger 3.0.17.
Thanks!
Upgrading to Passenger 4.0.45 fixed the issue.