Log rotation for custom log files in ruby on rails - ruby-on-rails

I would like to know how we can rotate custom log files in ruby on rails. I have searched and found ways to rotate the environment log files(development.log, production.log). But my requirement is to rotate every log files in the application.
This can be done with the logrotate tool. But, I would like to handle log rotation from the application itself.
How is this possible with the rails logger class?
Is there a generic way we can do this(may be in the application.rb) instead of specifying the shift age and file size wherever creating each of these application logs.
Any help would be greatly appreciated.

Related

Ruby on Rails Runtime Console

I am trying to find a way to get a rails console for a program during runtime with all instantiated variables. The normal rails console does not have access to any instantiated variables of its respective running application. For example, when a rails application crashes during runtime, a webpage will load with the error listed in red text, a snippet of the code where the error was raised, and a console at the bottom with access to variables instantiated during runtime.
See image below for console im talking about
The best thing I could find was a gem called pry, which seems to allow you to access a console during runtime by adding the line 'binding.pry' in your code at the point where you want access to the console. I would be fine with this, but seeing as how rails already gives you access to this when your app crashes, I would think there is a "vanilla" way to do this. Unfortunately I can't find anything online about this feature in rails. This seems like such a valuable tool for debugging I can't see why rails doesn't implement this. Is there a better way to debug during runtime? a better gem?
I will suggest you to use Better Errors
Better Errors replaces the standard Rails error page with a much better and more useful error page. It is also usable outside of Rails in any Rack app as Rack middleware.

Rails Have a custom Logger in Heroku

I need to have a custom Logger that logs specific actions that occur in my deployed application in Heroku. This log is for the user to view (that's why the default logger is no good here).
While testing on development on local everything is fine. I have
logger = Logger.new(PATH)
logger.info("...")
PATH is set on environment variables.
Now I want to deploy to Heroku but I find myself with a problem: how can the user view this log? Where would I need to save it?
On heroku filesystem is read-only, you should log to stdout. Default log can be directed there by rails_12factor gem (or configure by youself, but the former is easier)
But for custom log, that is going to be viewed by the user it's better to write to db, because probability of future access control being needed is high
Heroku has an ephemeral filesystem meaning whatever file you upload would be deleted shortly afterwards.
If you need a custom logger on Heroku, I'd say you should probably use something that is more persistent. You may use something like Redis.
Look at this answer for more info.

Best way to rotate Rails log files daily in 2015

What is the best way to rotate Ruby on Rails log files daily?
I saw posts in the past. They recommended using logrotate and enable copytruncate option to avoid restarting Rails.
However, logs are lost at small time slice between copying the file and truncating it.
My project requirement does not lose logs, so we chose cronolog with ruby's stdlib Logger.
The code like the following:
config.logger = Logger.new(IO.popen("/usr/sbin/cronolog #{config.paths['log'].first}.%Y%m%d", "w"))
It's mostly works correctly, but logs are mixed when logging data size more than PIPE_BUF that because using PIPE.
So what is recommended way to rotate logs in such situation?

Working with production.log rails

I got my production log file working. But wanted to get some suggestions onto how to work with it. I currently look at it view console and doing either
tail log/*
tail -f log/production.log
to look at it. I would like to either 1. find a better way to be able to look at the logs and perhaps with colors. 2. Can the production log file be outputted in a view where an admin user can view it?
Lastly, is there a gem that's great for emailing me when an error occurred and what the error was / how do you currently keep track of when errors happening in production?
Thanks!
For just having a nicer alternative to tail which shows the colours instead of those ESC[… codes, try less -R production.log. If you want to follow the end of the file like tail -f, use:
less -R +F production.log
There's nothing built-in for getting the log into a view as far as I know, but I suppose you could write a controller which shows (some of) the log in a view. Make sure it's secure with authentication though; you don't want to be exposing your log to the internet! Using an external service should remove this concern and it will have a load of other features. For example, I haven't tried papertrail (https://papertrailapp.com) but it's been on my to-try list for a while. It looks good and they're free for up to 100MB of logs a month. I've used airbrake (https://airbrake.io/) in the past for exception notifications but I don't think they have a free tier - just a free trial.

rails - hide 'source of your encoding' from terminal

I'm new to ROR and I"m having trouble with all the text that rails spits to the terminal window. Primarily, the html from my web pages get repeated in the terminal window and I'm really just wanting to see the important stuff like sql queries and error messages. I'm wasting a lot of time scrolling throughout the terminal window trying to find what I need b/c of all the HTML that fills up the screen.
Is there an option to disable the 'source of your encoding' output?
Thanks.
You could raise your log level
The rails guide gives a good example
http://guides.rubyonrails.org/debugging_rails_applications.html#log-levels
Make sure if you are in development to change it in config/environments/development.rb
I don't know if this will really solve your problem though because I am unsure of exactly what output you are looking for. A higher log level may throw out the baby with the bathwater.

Resources