Unknown logs interrupt me while `binding.pry` on Rails - ruby-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

Related

How to run heroku restart from inside of a rails app?

I understand that from the console I can run heroku restart. What I'd like to do is to have a button in my app (admin console), where pushing that button runs a heroku restart. Does anyone know how to do that and if it's possible? So the code would look something like this:
<button id="heroku_restart">Restart</button>
$("#heroku_restart").click(function() {
$.post('/restart', {}).done(function(response) {
alert(response)
})
})
class AdminsController
# this is the action mapped to the route /restart
def restart
# code for heroku restart
end
end
So per #vpibano, as of this writing, doing it with the platform-api is a breeze. Here's the action POSTed to by a button on my website:
def restart
heroku = PlatformAPI.connect_oauth(ENV["heroku_oauth_token"])
heroku.dyno.restart_all("lastmingear")
render nothing: true
end
As per the description mentioned in the post, the one way of doing it is :
1) First locate the server.pid file
pid_file = Rails.root.join("tmp", "pids", "server.opid")
2) Now, truncate the contents of the file
File.open(pid_file, "w") {|f| f.truncate(0)}
3) Finally, run the server using Kernel module:
Kernel.exec("rails s")
Note: As rightly, mentioned by #vpibano you will need authentication to access your app.
This is not a working model but a way to achieve the requirement.
Hope it helps!!

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.

Send errors to another log - 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)

Use cache money only for a single model?

I want to use cache-money but I don't want to start automatically caching everything (I'm working with a large production app, terabytes of data etc). How do I use it for only the models that I specify? Right now I've got:
# initializers/cache_money.rb
require 'cache_money'
config = (cfg = YAML.load(IO.read(File.join(RAILS_ROOT, "config", "memcached.yml"))))[RAILS_ENV] || cfg["defaults"]
$memcache = MemCache.new(config)
$memcache.servers = config['servers']
$local = Cash::Local.new($memcache)
$lock = Cash::Lock.new($memcache)
$cache = Cash::Transactional.new($local, $lock)
and then in the model I want to cache with cache-money:
# my_model.rb
class MyModel < ActiveRecord::Base
is_cached :repository => $cache
# ...
end
But this doesn't work; the call to is_cached gives the following error: NoMethodError: undefined method `create' for Config:Module
Any ideas? Failing that, is there anywhere I can go for help with cache-money? I couldn't find a mailing list or anything.
I think this is a bug in the cache_money code.
There are forks available on github that fix this bug, eg:
http://github.com/quake/cache-money
The fix can be seen with this commit:
http://github.com/quake/cache-money/commit/54c3d12789f31f2904d1fe85c102d7dbe5829590
I've just experienced the same problem trying to deploy an application. Running on my development machine it was fine, but it failed with this error on the production machine.
Apart from the architecture (OSX vs CentOS) the only difference i could see was that the ruby versions were different (1.8.6 p114 vs 1.8.6 p0). After upgrading the server to the latest 1.8 version (1.8.7 p160) this error went away.

Resources