Rails logging to a clean, custom file - ruby-on-rails

I've put the following line in my config/application.rb:
config.logger = Logger.new("#{Rails.root}/log/app.log")
However both app.log and development.log include all the console "noise".
Is there a way to write on a clean app.log file without the noise?

If your goal is to just limit the amount of information in your log, you'd want to simply adjust your log level. This is typically done at the environment level, not the application level, as you'd want verbose logs in development, but quieter logs in test/production.
config.log_level defines the verbosity of the Rails logger. This
option defaults to :debug for all environments. The available log
levels are: :debug, :info, :warn, :error, :fatal, and :unknown.

Related

Disable rails routing logs in production

I have a application which receives large number of requests, and to reduce log file size I want to remove some logs.
One of the logs that I am trying to remove is :
I, [2015-09-09T19:01:01.372374 #10897] INFO -- : Processing by Api::V1::MyAPIController#show as JSON
Basically this log shows to which controller actions the request is getting routed to. I want to remove this log. Is it possible to remove this specific log or change it's log level to debug. Please provide a way to remove this log, as this log by itself provides little information but is taking up considerable disk space.
Thanks for help,
The simplest way to cut output is to change log_level. In your config/environments/production.rb file locate line:
config.log_level = :debug
and change debug to info, warn, error, or fatal.
http://guides.rubyonrails.org/debugging_rails_applications.html#log-levels
Update To silence only routing logs, we can use silencer gem.
In production.rb
require 'silencer/logger'
config.middleware.swap Rails::Rack::Logger, Silencer::Logger, :silence => [%r{^/}]
This will silence all routing logs by replacing Rails::Rack::Logger with silencer on all routes.
More info on silencer configuration can be found here.

What is the difference between using 'config.log_level' and 'Rails.logger.level'?

I've seen examples of both statements, and both control the level of logging whether using descriptive values (:debug, :info, :warn, :error, :fatal, :unknown) or numeral (1-5).
config.log_level is only used in environment initializer files.
Rails.logger.level can be used almost anywhere.
See this documentation.
I have never set log-level anywhere else than in my env.-initializer files. I guess maybe one possible usecase for using Rails.logger.level could be when for ex. you have a development environment so it has a log-level of :debug but you don't want to bloat you log files with a lot of db-querys and unneccessary debug-info- then you can filter out only info-level log:
Rails.logger.level = 1
# Some code that gives out too much debug information
Rails.logger.level = 0
# Code that's ok for debug information
And respectively in prod-env. if you, for some reason, want to print out debug info in a certain place of the code while environment log level is set to :info.

Logging in Ruby on Rails in Production Mode

I would like to view some variables in controller, it tried the following:
Rails.logger.debug "Year: #{Time.now.year}"
puts "Year: #{Time.now.year}, Month: #{#month}"
where can I see the output for Logger or Puts in production mode? Do I need so set something up to view these somewhere?
The normal log level in production is info, so debug logs are not shown.
Change your logging to
Rails.logger.info "Year: #{Time.now.year}"
to show it in production.log.
Alternatively (but not a good idea) you can raise the logging level in /config/environments/production.rb:
config.log_level = :debug
Update Rails 4.2:
Now the default debug level in all environments is :debug (as #nabilh mentioned).
If you want you production environment less chattery, you can reset your log level in /config/environments/production.rb to the former :info:
config.log_level = :info
While I believe #martin-m is right that you probably don't want to clutter your logs by using config.log_level = :debug in /config/environments/production.rb, I believe the default logging level in all environments is debug as of Rails 4.2. So debug logs (and all levels up) will be shown in production unless you specify otherwise.
So in response to your question, you can now write:
Rails.logger.debug "Year: #{Time.now.year}" and see the results in /log/production.log.
See here for more detailed documentation. Here is the documentation for Rails 4.1 for comparison.
If you have a production app that you may need to temporarily change log levels, you might want to use an environment variable to be able to set level without modifying code and redeployment, though your app would need to be restarted, you could take this approach:
config/environment/production.rb
MyApp::Application.configure do
#...
log_level = :info #default state
[:debug, :info, :warn, :error, :fatal, :unknown].each do |level|
if ENV['LOG_LEVEL']&.to_sym == level
log_level = level
end
end
config.log_level = log_level
#...
end

Is there a way to stop rails logging everything?

I don't want it to log incoming IPs, I don't want it to log any internal happenings.
I just want Rails to process the requests as they come in and that's that.
Is this possible to do?
How can I prevent having a growing development.log or production.log?
You could just replace the Rails logging facilities with loggers that log to /dev/null:
class NullLoggerRailtie < Rails::Railtie
initializer 'null_logger', :before => 'initialize_logger' do |app|
Rails.logger = ActiveRecord::Base.logger = ActionController::Base.logger = ::Logger.new("/dev/null")
end
end
This will reroute all of the Rails logging to the null device, rather than letting it go to a file anywhere. The logging will still happen, but it'll just be immediately trashed.
Set the log Level.
The available log levels are: :debug, :info, :warn, :error and :fatal
If you want to know the current log level you can call the Rails.logger.level method.
To change the log level use config.log_level = :fatal in your environment initializer, or
Rails.logger.level = 0 inline at any time
Another option might be to symbolicly link the log file to /dev/null
Simplifying Chris Heald answer, you can set the logger to /dev/null by environment:
Rails.application.configure do
config.logger = Logger.new("/dev/null")
end
Just place the code in the right file(s):
Development: config/environments/development.rb
Test: config/environments/test.rb
Production: config/environments/production.rb
All environments: congig/applications.rb

Rails Caching Log Level

With the new caching options in Rails 2.1 i get nice entires in my log along the lines of
Cached fragment hit: views/homepage (0.16549)
However they are logged at the :debug level, which is the same level as the SQL output. I want to be able to disable the SQL output, and still see the cache info. How can I do this
well you could instantiate a specific logger for ActiveRecord and set it's log level to :info while leaving the default logger at debug ...
ActiveRecord::Base.logger = Logger.new("#{RAILS_ROOT}/log/#{RAILS_ENV}_database.log")
ActiveRecord::Base.logger.level = Logger::INFO # should set the log_level to info for you
from http://wiki.rubyonrails.org/rails/pages/HowtoConfigureLogging
or you could reopen AbstractAdapter and override the log(sql,name) method so it does nothing
http://api.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/AbstractAdapter.html#M001242

Resources