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 +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +1 -1
- data/README.md +26 -23
- data/lib/log_sweeper/version.rb +1 -1
- data/lib/log_sweeper.rb +7 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0bd0748e38c19967141c34eecdc9b748b80542a15f6e863dd2dd4d60869e1069
|
4
|
+
data.tar.gz: baac5ec981df3fe90b6f807b1e6d93b3d2af4589b6639d94eea7b2115e9fa554
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e13eae27b6e80a1b1900736e71f7752781432682e97fe6ab222a6426421901e1af3eafef05271bfc96e8f308b6ffae9eab5d0a7699ecb0edb66f44a6775147a
|
7
|
+
data.tar.gz: 5d026bdf9016299687e09e56ab602ffa3abca64210c6e136f9df5ed11a9c2552c5d4fb4c7c9506a1e6cb26172e2283a85689fb1540304e65632984770216eecc
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
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
|
-
|
3
|
+
`LogSweeper` is a simple module for cleaning up log directories.
|
4
4
|
|
5
|
-
|
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
|
-
|
9
|
+
Juts add `gem "log_sweeper"` to your Gemfile.
|
10
10
|
|
11
|
-
|
12
|
-
gem 'log_sweeper'
|
13
|
-
```
|
14
|
-
|
15
|
-
And then execute:
|
11
|
+
## Examples
|
16
12
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
$ gem install log_sweeper
|
13
|
+
```ruby
|
14
|
+
# Just use the defaults
|
15
|
+
LogSweeper.run("log")
|
22
16
|
|
23
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
26
|
+
```ruby
|
27
|
+
every 1.hour do
|
28
|
+
runner "LogRotator.run(Rails.root.join('log'))"
|
29
|
+
end
|
30
|
+
```
|
30
31
|
|
31
|
-
|
32
|
+
## License
|
32
33
|
|
33
|
-
|
34
|
+
Released under MIT License.
|
34
35
|
|
35
|
-
|
36
|
+
## Authors
|
36
37
|
|
37
|
-
|
38
|
+
Created by Yuri Smirnov.
|
38
39
|
|
39
|
-
|
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>
|
data/lib/log_sweeper/version.rb
CHANGED
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
|
-
|
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
|
24
|
+
if filename =~ /\.log\b/ && Time.now - entry.mtime > lifetime_threshold
|
21
25
|
logger.info "deleting #{entry}"
|
22
26
|
entry.delete
|
23
|
-
elsif filename
|
27
|
+
elsif filename =~ /\.log\.\d+$/
|
24
28
|
logger.info "gzipping #{entry}"
|
25
29
|
compress_file(entry)
|
26
30
|
entry.delete
|