Send errors to another log - Ruby on rails - ruby-on-rails

I wanted to know if I can send all the errors that happend while my app is in 'production' environment to a new 'log' instead of sending ALL the actions to the 'production.log' send it to 'production_errors.log'.
And if it is possible, tell me how please !
Thank you very much for reading !

In config/environments/production.rb, within the configuration block:
config.logger = Logger.new(Rails.root.join(%w(log production_errors.log)), :error)

Related

Unknown logs interrupt me while `binding.pry` on Rails

I’m developing with Ruby on Rails. When I start an application server with Puma, the following logs continue to show every a few seconds.
{"method":{},"path":{},"format":{},"params":{},"controller":"ApplicationCable::Connection","action":"connect","status":200,"duration":8.75,"backtrace":null,"host":null,"user_id":null,"user_type":null,"remote_ip":null,"user_agent":null,"os":null,"os_version":null,"browser":null,"browser_version":null,"#timestamp":"2021-07-28T10:24:34.068Z","#version":"1","message":"[200] (ApplicationCable::Connection#connect)"}
{"method":{},"path":{},"format":{},"params":{},"controller":"ApplicationCable::Connection","action":"disconnect","status":200,"duration":0.58,"backtrace":null,"host":null,"user_id":null,"user_type":null,"remote_ip":null,"user_agent":null,"os":null,"os_version":null,"browser":null,"browser_version":null,"#timestamp":"2021-07-28T10:24:34.069Z","#version":"1","message":"[200] (ApplicationCable::Connection#disconnect)"}
This interrupts binding.pry prompts as follow, so I can’t debug an application properly.
[1] pry(#<SomeController>)> {"method":{},"path":{},"format":{},"params":{},"controller":"ApplicationCable::Connection","action":"connect","status":200,"duration":8.75,"backtrace":null,"host":null,"user_id":null,"user_type":null,"remote_ip":null,"user_agent":null,"os":null,"os_version":null,"browser":null,"browser_version":null,"#timestamp":"2021-07-28T10:24:34.068Z","#version":"1","message":"[200] (ApplicationCable::Connection#connect)"}
{"method":{},"path":{},"format":{},"params":{},"controller":"ApplicationCable::Connection","action":"disconnect","status":200,"duration":0.58,"backtrace":null,"host":null,"user_id":null,"user_type":null,"remote_ip":null,"user_agent":null,"os":null,"os_version":null,"browser":null,"browser_version":null,"#timestamp":"2021-07-28T10:24:34.069Z","#version":"1","message":"[200] (ApplicationCable::Connection#disconnect)"}
I wasn’t able to find from which these logs show.
What I’ve tried is adding ActionCable.server.config.logger = Logger.new(nil) to config/application.rb. But I still have the problem.
https://dev.to/xlts/fixing-rails-action-cable-logger-la8#option-2-try-to-do-it-systematically
How can I fix this problem?
Thank you in advance.
I’m using Lograge, so I’ve resolved this problem by adding the following configuration to config/initializers/lograge.rb.
Rails.application.configure do
# ...
# ...
# ...
config.lograge.ignore_actions = [
"ApplicationCable::Connection#connect",
"ApplicationCable::Connection#disconnect"
]
end

Rails Custom Logger Writes To Log Sporadically

I've set up a custom logger in an initializer:
# /config/initializers/logging.rb
log_file = File.open("#{Example::Application.config.root}/log/app.log", "a")
AppLogger = ActiveSupport::BufferedLogger.new(log_file)
AppLogger.level = Logger::DEBUG
AppLogger.auto_flushing = true
AppLogger.debug "App Logger Up"
Although it creates the log file when I start the application, it doesn't write to the log file. Either from AppLogger.debug "App Logger Up" in the initializer or similar code elsewhere in the running application.
However, intermittently I do find log statements in the file, though seemingly without any pattern. It would seem that it is buffering the log messages and dumping them when it feels like it. However I'm setting auto_flushing to true which should cause it to flush the buffer immediately.
What is going on and how can I get it working?
Note: Tailing the log with $ tail -f "log/app.log" also doesn't pick up changes.
I'm using POW, but I've also tried with WEBrick and get the same (lack of) results.
Found the answer in a comment to the accepted answer to on this question:
I added:
log_file.sync = true
And it now works.

Delayed_job is no longer logging

For some reason, my Rails app is no longer logging my DelayedJob gem's activity, either to a separate log (delayed_job.log) or to the main Rails development log. I am also using the Workless gem, should this be relevant.
It used to say things like this:
2013-06-07T19:16:25-0400: [Worker(delayed_job.workless host:MyNames-MacBook-Pro.local pid:28504)] Starting job worker
2013-06-07T19:16:38-0400: [Worker(delayed_job.workless host:MyNames-MacBook-Pro.local pid:28504)] MyApp#scrape completed after 13.0290
2013-06-07T19:16:38-0400: [Worker(delayed_job.workless host:MyNames-MacBook-Pro.local pid:28504)] 1 jobs processed at 0.0761 j/s, 0 failed ...
2013-06-07T19:16:43-0400: [Worker(delayed_job.workless host:MyNames-MacBook-Pro.local pid:28504)] Exiting...
But nothing is appearing anymore in any of the logs.
How can I make sure that DelayedJob activity logs to the main development log? I don't mind if it also logs to a separate log, but the important thing is that I see its activity in my Mac's console.
I have searched for answers to this issue online (such as here and nothing is working.
Here is my delayed_job_config.rb: (in config/initializers)
Delayed::Worker.sleep_delay = 60
Delayed::Worker.max_attempts = 2
Delayed::Worker.max_run_time = 20.minutes
Delayed::Worker.logger = Rails.logger
Delayed::Worker.logger.auto_flushing = true
Please let me know if you'd like more code from my app - I'd be happy to provide it. Much thanks from a Rails newbie.
I finally got this to work. All thanks to Seamus Abshere's answer to the question here. I put what he posted below in an initializer file. This got delayed_job to log to my development.rb file (huzzah!).
However, delayed_job still isn't logging into my console (for reasons I still don't understand). I solved that by opening a new console tab and entering tail -f logs/development.log.
Different from what Seamus wrote, though, auto-flushing=true is deprecated in Rails 4 and my Heroku app crashed. I resolved this by removing it from my initializer file and placing it in my environments/development.rb file as config.autoflush_log = true. However, I found that neither of the two types of flushing were necessary to make this work.
Here is his code (without the auto-flushing):
file_handle = File.open("log/#{Rails.env}_delayed_jobs.log", (File::WRONLY | File::APPEND | File::CREAT))
# Be paranoid about syncing
file_handle.sync = true
# Hack the existing Rails.logger object to use our new file handle
Rails.logger.instance_variable_set :#log, file_handle
# Calls to Rails.logger go to the same object as Delayed::Worker.logger
Delayed::Worker.logger = Rails.logger
If the above code doesn't work, try replacing Rails.logger with RAILS_DEFAULT_LOGGER.

Rails 3, get 500 status code on production environment with no error, and no log

I'm developing an app using event_calendar plugin to build a calendar with some events. Everything works fine in development and test environment but when i try to switch to production env trying to open the calendar page the server returns a 500 error page.
What is really weird for me is that event_calendar worked well until today, and i didn't touch a line of code related to this plugin.
So, that's what i made to debug with no success:
Migrate database and precompile assets on production env.
Opened log/production.log to find some error, but nothing was there, non error logged.
Uncomment this line on production.rb config.log_level = :debug, still no error logged.
Modified the index action of Calendar Controller with a dummy empty index action to make sure that was not the problem.
Opened rails console on production env and try to make a get request to /calendar:
1.9.3-p0 :001 > app.get '/calendar'
=> 500
1.9.3-p0 :002 > app.response
=> #<ActionDispatch::TestResponse:0x007ff53607d298 #body=["<!DOCTYPE html>\n<html>\n<head>\n ... HTML CODE OF 500 ERROR PAGE ... </html>\n"]
,#header={"Content-Type"=>"text/html; charset=utf-8", "Content-Length"=>"643", "X-Request-Id"=>"d47e6d869dd8e215a0741430ee2eacae", "X-Runtime"=>"0.036510"}, #status=500, #sending_file=false, #blank=false, #content_type=text/html, #charset="utf-8", #cache_control={}, #etag=nil>
Apart from this everything works fine, even in production env. So i guess the issue depends on something wrong on event_calendar but i think the problem of ghost error is more general.
I really hope that error is somewhere!
Hope someone can help me! Thanks
Marco

puts doesn't print stuff to console

i'm using POW for local rails development. i don't know why, but i can't print or puts information to my development.log. i want to puts the content of variables to console / log from my controller. any advice?
i read my logs with tail -f logs/development.log
thanks!
Instead of puts, try logger.info(). Logging in Rails is very flexible, but it does mean that you might not be able to use the simplest tools sometimes.
If you're doing debugging and only want to see some messages in the logs you can do the following:
Rails.logger.debug("debug::" + person.name)
and
$ pow logs | grep debug::
now you'll only see logging messages that start with debug::
Another option is to use the rails tagging logger, http://api.rubyonrails.org/classes/ActiveSupport/TaggedLogging.html.
logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
logger.tagged('BCX') { logger.info 'Stuff' } # Logs "[BCX] Stuff"
$ pow logs | grep BCX
For anyone who still can't get it to work, remember that Ruby doesn't use semicolons. They are only used to chain commands. I was adding them at the end due to muscle memory (coming from PHP), so the ruby console thought I was still entering commands:
irb(main):001:0> puts "hi";
irb(main):002:0* puts "hi"
hi
hi
=> nil
Hope this helps someone.

Resources