log_sweeper 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4db94e181fdea505a9a543ea7ad4316f06b4d3f396137804ef6db8cc8d38025d
4
- data.tar.gz: 69f722b86d9c989f4ba0dac7408f9e58703ac51ed6bae91a3923d32f8c01b7c2
3
+ metadata.gz: 0bd0748e38c19967141c34eecdc9b748b80542a15f6e863dd2dd4d60869e1069
4
+ data.tar.gz: baac5ec981df3fe90b6f807b1e6d93b3d2af4589b6639d94eea7b2115e9fa554
5
5
  SHA512:
6
- metadata.gz: 14b54d6394d7ddce37db6c7cd233f456fb00a11e513c7993f9b6ff4a7611d85857b5a02a7cf627bcab7c16f8a41e3ad0b5575732b73b1853757b64f46cb7364c
7
- data.tar.gz: 44d078163fdb1dc8bede864a389afb91d9f485359bddb0fc1d08a494aaa541cc0e655c5094f0a451c4839738411770f4562cd7bc2147509e441f6bb11ac03081
6
+ metadata.gz: 3e13eae27b6e80a1b1900736e71f7752781432682e97fe6ab222a6426421901e1af3eafef05271bfc96e8f308b6ffae9eab5d0a7699ecb0edb66f44a6775147a
7
+ data.tar.gz: 5d026bdf9016299687e09e56ab602ffa3abca64210c6e136f9df5ed11a9c2552c5d4fb4c7c9506a1e6cb26172e2283a85689fb1540304e65632984770216eecc
data/.gitignore CHANGED
@@ -6,6 +6,7 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ /spec/log
9
10
 
10
11
  # rspec failure tracking
11
12
  .rspec_status
data/.rubocop.yml CHANGED
@@ -3,4 +3,4 @@ inherit_gem:
3
3
 
4
4
  AllCops:
5
5
  DisplayCopNames: true
6
- TargetRubyVersion: 2.5
6
+ TargetRubyVersion: 2.3
data/README.md CHANGED
@@ -1,39 +1,42 @@
1
- # LogSweeper
1
+ # LogSweeper   [![Gem Version](https://badge.fury.io/rb/log_sweeper.svg)](https://badge.fury.io/rb/log_sweeper) [![Build Status](https://travis-ci.org/umbrellio/log_sweeper.svg?branch=master)](https://travis-ci.org/umbrellio/log_sweeper) [![Coverage Status](https://coveralls.io/repos/github/umbrellio/log_sweeper/badge.svg?branch=master)](https://coveralls.io/github/umbrellio/log_sweeper?branch=master)
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/log_sweeper`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ `LogSweeper` is a simple module for cleaning up log directories.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ It is designed to be used with Ruby logger rotation. By default, it will remove all log files older than 10 days and will gzip all log files that look like rotated log files. For example, `production.log.20190228` will be gzipped and replaced with `production.log.20190228.gz`. It logs what it's doing using the provided logger which defaults to `STDOUT` logger.
6
6
 
7
7
  ## Installation
8
8
 
9
- Add this line to your application's Gemfile:
9
+ Juts add `gem "log_sweeper"` to your Gemfile.
10
10
 
11
- ```ruby
12
- gem 'log_sweeper'
13
- ```
14
-
15
- And then execute:
11
+ ## Examples
16
12
 
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install log_sweeper
13
+ ```ruby
14
+ # Just use the defaults
15
+ LogSweeper.run("log")
22
16
 
23
- ## Usage
17
+ # Customize logs lifetime and logger
18
+ LogSweeper.run("log", logs_lifetime_days_count: 5, logger: Logger.new("/path/to/file.log"))
24
19
 
25
- TODO: Write usage instructions here
20
+ # In case you don't want any logging and deleting any logs
21
+ LogSweeper.run("log", logs_lifetime_days_count: Float::INFINITY, logger: Logger.new(nil))
22
+ ```
26
23
 
27
- ## Development
24
+ The best way to use this module is to run it via cron, for example you can use [whenever](https://github.com/javan/whenever) gem with similar config in `schedule.rb` file:
28
25
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+ ```ruby
27
+ every 1.hour do
28
+ runner "LogRotator.run(Rails.root.join('log'))"
29
+ end
30
+ ```
30
31
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+ ## License
32
33
 
33
- ## Contributing
34
+ Released under MIT License.
34
35
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/tycooon/log_sweeper.
36
+ ## Authors
36
37
 
37
- ## License
38
+ Created by Yuri Smirnov.
38
39
 
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+ <a href="https://github.com/umbrellio/">
41
+ <img style="float: left;" src="https://umbrellio.github.io/Umbrellio/supported_by_umbrellio.svg" alt="Supported by Umbrellio" width="439" height="72">
42
+ </a>
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LogSweeper
4
- VERSION = "1.0.0"
4
+ VERSION = "1.1.0"
5
5
  end
data/lib/log_sweeper.rb CHANGED
@@ -9,18 +9,22 @@ require "logger"
9
9
  module LogSweeper
10
10
  extend self
11
11
 
12
+ # Clean up provided log directory
13
+ # @param path [String, Pathname] path to the log directory to clean
14
+ # @param logs_lifetime_days_count [Numeric] number of days to keep the logs
15
+ # @param logger [Logger] logger to use
12
16
  def run(path, logs_lifetime_days_count: 10, logger: Logger.new(STDOUT))
13
- min_mtime = Time.now - (logs_lifetime_days_count.to_f * 24 * 3600)
17
+ lifetime_threshold = logs_lifetime_days_count * 24 * 3600
14
18
 
15
19
  Pathname.new(path).each_child do |entry|
16
20
  next unless entry.file?
17
21
 
18
22
  filename = entry.basename.to_s
19
23
 
20
- if filename.match?(/\.log\b/) && entry.mtime < min_mtime
24
+ if filename =~ /\.log\b/ && Time.now - entry.mtime > lifetime_threshold
21
25
  logger.info "deleting #{entry}"
22
26
  entry.delete
23
- elsif filename.match?(/\.log\.\d+$/)
27
+ elsif filename =~ /\.log\.\d+$/
24
28
  logger.info "gzipping #{entry}"
25
29
  compress_file(entry)
26
30
  entry.delete
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: log_sweeper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuri Smirnov