Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Clear specific logs when using `rake log:clear` #8964

Merged
merged 1 commit into from

2 participants

@mattdbridges

There are times when I want to clear only one of the logs or just two of the logs rather than all of the logs. You can now accomplish this with:

$ LOGS=test rake log:clear

Or multiple:

$ LOGS=test,staging rake log:clear

Wasn't sure the best way to test this. I couldn't find any tests for it, so if there are any suggestions, I am more than willing to add them or provide a sample app with this successfully running in it.

@carlosantoniodasilva

Can you explain your use case a little bit more please? I wasn't even aware that we had a task for clearing the logs, since a simple rm logs/* would do most of the time I think (I know this doesn't rm, but there's no much difference on the result).

@mattdbridges

Use case:

I want to preserve the contents development.log and clear the test.log and stage.log. Right now, the rake log:clear task will completely empty all of the log files. I wanted a way to limit which logs were cleared when running this task.

Yes, there isn't much difference in the result, I just like using rake when I can.

guides/source/command_line.md
@@ -355,7 +355,7 @@ rake assets:clean # Remove compiled assets
rake assets:precompile # Compile all the assets named in config.assets.precompile
rake db:create # Create the database from config/database.yml for the current Rails.env
...
-rake log:clear # Truncates all *.log files in log/ to zero bytes
+rake log:clear # Truncates all *.log files in log/ to zero bytes (Narrow specific files: `LOGS=test,development rake log:clear`

Please use narrow (lowercase).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
railties/lib/rails/tasks/log.rake
@@ -1,9 +1,23 @@
namespace :log do
- desc "Truncates all *.log files in log/ to zero bytes"
+ desc "Truncates all *.log files in log/ to zero bytes (Narrow specific files: `LOGS=test,development rake log:clear`"

How about specify which logs with LOGS=test,development, wdyt?

I like that a lot better. I'll update docs and desc

Wait, is that clear enough that you can specify other environments?

I think so, other ideas?

No, I really like your phrasing better. Just making sure clarity is good enough.

We could also use LOGS=file,names,here, not sure it's necessary though :)

Maybe staging instead of development, but I don't think there's a huge benefit to any specific set of words.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
railties/lib/rails/tasks/log.rake
((10 lines not shown))
end
end
+
+ def log_files
+ if ENV['LOGS'].nil?
+ FileList["log/*.log"]
+ else
+ ENV['LOGS'].split(',')
+ .map { |file| "log/#{file.strip}.log" }
+ .select { |file| File.exists?(file) }
+ end

I think inverting the logic would be simpler. Also fix the indentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@mattdbridges

Would you like me to update the CHANGELOG as well?

@carlosantoniodasilva

Yes, a changelog entry would be good.

@mattdbridges

CHANGELOG update added

@carlosantoniodasilva carlosantoniodasilva merged commit f5aa4d9 into rails:master
@carlosantoniodasilva

Thanks.

@mattdbridges mattdbridges deleted the mattdbridges:specify-log-to-clear branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 16, 2013
  1. @mattdbridges
This page is out of date. Refresh to see the latest.
View
2  guides/source/command_line.md
@@ -355,7 +355,7 @@ rake assets:clean # Remove compiled assets
rake assets:precompile # Compile all the assets named in config.assets.precompile
rake db:create # Create the database from config/database.yml for the current Rails.env
...
-rake log:clear # Truncates all *.log files in log/ to zero bytes
+rake log:clear # Truncates all *.log files in log/ to zero bytes (specify which logs with LOGS=test,development)
rake middleware # Prints out your Rack middleware stack
...
rake tmp:clear # Clear session, cache, and socket files from tmp/ (narrow w/ tmp:sessions:clear, tmp:cache:clear, tmp:sockets:clear)
5 railties/CHANGELOG.md
@@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ##
+* Specify which logs to clear when using the `rake log:clear` task.
+ (e.g. rake log:clear LOGS=test,staging)
+
+ *Matt Bridges*
+
* Allow a `:dirs` key in the `SourceAnnotationExtractor.enumerate` options
to explicitly set the directories to be traversed so it's easier to define
custom rake tasks.
View
22 railties/lib/rails/tasks/log.rake
@@ -1,9 +1,23 @@
namespace :log do
- desc "Truncates all *.log files in log/ to zero bytes"
+ desc "Truncates all *.log files in log/ to zero bytes (specify which logs with LOGS=test,development)"
task :clear do
- FileList["log/*.log"].each do |log_file|
- f = File.open(log_file, "w")
- f.close
+ log_files.each do |file|
+ clear_log_file(file)
end
end
+
+ def log_files
+ if ENV['LOGS']
+ ENV['LOGS'].split(',')
+ .map { |file| "log/#{file.strip}.log" }
+ .select { |file| File.exists?(file) }
+ else
+ FileList["log/*.log"]
+ end
+ end
+
+ def clear_log_file(file)
+ f = File.open(file, "w")
+ f.close
+ end
end
Something went wrong with that request. Please try again.