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.
Related
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)
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
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
Here is the issue. When running the 'system' command in a rails controller in development it behaves as intended by running the command while in production it does not do anything.
For example the following command:
system 'rails g migration user_generated_migration'
or even:
system 'ls'
work locally (in development) but on the server (production environment) they don`t do anything.
Am i missing something in the configuration files, production.rb maybe? Or is there something that should be enabled on the server?
Update:
The production environment is the default set up maybe with a notable change to how caching is handled:
config.cache_store = :dalli_store, ENV['MEMCACHE_SERVERS']
It is running on apache server through passenger. I suspect it has something to do with what rights the apache user has? I don`t have a lot of experience in the server area so i wouldn`t know what exact details to give you.
I'm trying to run this web aplication of Ruby on Rails with Apache using Phusion Passenger. I already configured the httpd.conf file.
I also have another aplication which runs with 'rails server' commmand, and it's connecting to the development database. However, i don't understand why the aplication which runs with apache it's trying to connect to a production database that i didn't create yet instead development as it should be.
What i have to configure to make my Ruby on Rails application run as development?
Apache with Phusion provide a production level web server environment, rails server (WEBrick) is a simple web server that lets you test locally. You typically use one or the other on a given machine. But not always :-)
But to answer your question, which database is used by Rails is determined by the RAILS_ENV variable, which is by default, one of production, development or test.
When you create a new rails application, a default database configuration is created in the file app/config/database.yml -- there are separate sections that provide necessary parameters for connecting to your database(s). Other environment-specific configurations may be specified either in the environment.rb or in app/config/environments/<name>.rb.
In your Passenger config, you can set the RAILS_ENV variable as documented here http://www.modrails.com/documentation/Users%20guide%20Apache.html#rails_env.
I just realized also that it seems like Passenger is looking for production (expecting the db name to be someweb_production) -- chances are you have to run a bundle exec rake db:migrate in the production environment in order to (create and) initialize the database. You may need to pass the environment parameter in this case also.
For your local config (rails server) the server will look for a shell environment variable named RAILS_ENV, and you can also pass a specific environment on the command line e.g. rails server --environment=development. I think if neither is specified, rails server defaults to development.
The problem was that i didn't include the following lines in the Apache conf file:
RailsEnv development
RackEnv development