views:

1306

answers:

6

How can I setup the automatic cleanup on test.log and development.log in ruby on rails?

Is there a setting to automatically delete dev and test logs on server start and tests run?

A: 

What exactly do you mean by "cleanup"?

cakeforcerberus
Is my quetsion too, what you mean with "cleanup"?
Nathan Campos
+3  A: 

Hello Bogdan,

See here in this tutorial: Rotating Rails Log Files, i don't know if it solves your problem, because i don't take care about my log files.

Hpe i'm helping you!

Nathan Campos
Unfortunetely logrotate is only available for Unix-like systems
Bogdan Gusiev
+1  A: 

rake log:clear is a rake task that truncates all files that match log/*.log to zero bytes. You could call it in your server start and run tests tasks.

Sarah Mei
+1  A: 

All script/server is, is a ruby script, im sure you could modify it to do something like:

#!/usr/bin/env ruby
require 'fileutils'
FileUtils.rm File.join(File.dirname(__FILE__), log, *.log)
require File.dirname(__FILE__) + '/../config/boot'
require 'commands/server'
Omar Qureshi
+6  A: 

The ruby logger is on hand to help you out here - and it has default options for rotation.

Here's what I do:

In environment.rb we define our own logger

new_logger = Logger.new(File.join(RAILS_ROOT, "log", "new_logger_#{RAILS_ENV}.log"), 'daily')
new_logger.formatter = Logger::Formatter.new

This creates our own loggers... with a formatter (so you get timestamps etc), with one per environment, and rotated daily.

Then in the initialization block we ask Rails to use this logger

Rails::Initializer.run do |config|

  config.active_record.logger = new_logger
  config.action_controller.logger = new_logger

  #snip
end

You can obviously see the power here too to have different loggers for active_record and for action_controller - sometimes very useful!

mylescarrick
A: 

In production environment, you really need SyslogLogger ( http://rails-analyzer.rubyforge.org/tools/files/lib/analyzer%5Ftools/syslog%5Flogger%5Frb.html ) , it lets you write to syslogd, which creansup,rotates your files in distributed setup.