How do I log to a new file every day in rails?

Viewed 1660

I am looking for a solution that will allow me to create a new log file for rails application everyday.

The goal is to go from standard production.log to

production.2019-06-01.log

production.2019-06-02.log

...etc

Where the latest log is has today's date.

I am aware of logrotate application but the issue is that production.log is current one, production.log.1 is supposedly yesterdays. So if you want to find something that happened on a specific day few weeks ago, you'll need to eyeball which log file it will be in.

3 Answers

did you try :

logger = Logger.new('production.log', 'daily') # or 'weekly', 'monthly'

To get the desired output use :

t = Time.now()
file = 'production.' + (t.strftime("%m-%d-%y")) + '.log' 
logger = Logger.new(file, 'daily') # or 'weekly', 'monthly'

All great answers! I am currently running on tomcat, so I utilized the default java logging classes to do this.

Related