Skip to content

Commit

Permalink
truncate a log file to 1000 lines if the file size > 1Mb.
Browse files Browse the repository at this point in the history
  • Loading branch information
plashchynski committed Apr 10, 2011
1 parent a0f8ebc commit 5ab199f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.rvmrc

# rcov generated
coverage

Expand Down
2 changes: 1 addition & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= rails_log_autotruncator

Description goes here.
rails_log_autotruncator is a simple patch for ActiveSupport::BufferedLogger that limits a log file size. Use it if you just don't want your rails project development log file becomes large.

== Contributing to rails_log_autotruncator

Expand Down
30 changes: 15 additions & 15 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,33 @@ Jeweler::Tasks.new do |gem|
gem.name = "rails_log_autotruncator"
gem.homepage = "http://github.com/plashchynski/rails_log_autotruncator"
gem.license = "MIT"
gem.summary = %Q{The tool for automatically rotating and truncating a rails log files while keeping the last N lines}
gem.description = %Q{TODO: longer description of your gem}
gem.summary = %Q{The tool for automatically rotating and truncating a rails development log file}
gem.description = %Q{rails_log_autotruncator is a simple patch for ActiveSupport::BufferedLogger that limits a log file size. Use it if you just don't want your rails project development log file becomes large.}
gem.email = "plashchynski@gmail.com"
gem.authors = ["Dmitry Plashchynski"]
# Include your dependencies below. Runtime dependencies are required when using your gem,
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
# gem.add_development_dependency 'rspec', '> 1.2.3'
gem.add_development_dependency "rspec", "~> 2.3.0"
# gem.add_development_dependency "rspec", "~> 2.3.0"
gem.add_development_dependency "bundler", "~> 1.0.0"
gem.add_development_dependency "jeweler", "~> 1.5.2"
gem.add_development_dependency "rcov", ">= 0"
end
Jeweler::RubygemsDotOrgTasks.new

require 'rspec/core'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = FileList['spec/**/*_spec.rb']
end

RSpec::Core::RakeTask.new(:rcov) do |spec|
spec.pattern = 'spec/**/*_spec.rb'
spec.rcov = true
end

task :default => :spec
# require 'rspec/core'
# require 'rspec/core/rake_task'
# RSpec::Core::RakeTask.new(:spec) do |spec|
# spec.pattern = FileList['spec/**/*_spec.rb']
# end
#
# RSpec::Core::RakeTask.new(:rcov) do |spec|
# spec.pattern = 'spec/**/*_spec.rb'
# spec.rcov = true
# end
#
# task :default => :spec

require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc|
Expand Down
1 change: 1 addition & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'rails_log_autotruncator'
28 changes: 28 additions & 0 deletions lib/rails_log_autotruncator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module ActiveSupport
class BufferedLogger
def flush
@guard.synchronize do
unless buffer.empty?
old_buffer = buffer
all_content = StringIO.new
old_buffer.each do |content|
all_content << content
end
@log.write(all_content.string)
end

if @log.size > 1_000_000
log = @log.path
@log.close
File.atomic_write(log) do |file|
file.write(IO.readlines(log)[-1000..-1].join)
end
@log = open(log, (File::WRONLY | File::APPEND | File::CREAT))
@log.sync = true
end

clear_buffer
end
end
end
end

0 comments on commit 5ab199f

Please sign in to comment.