logbrarian 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +45 -0
- data/bin/console +14 -0
- data/bin/logbrarian +70 -0
- data/bin/setup +8 -0
- data/lib/logbrarian/version.rb +3 -0
- data/lib/logbrarian.rb +4 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1fe41574bc75037b2cc52d8c175e8cbb86f09205
|
4
|
+
data.tar.gz: 07b8e5c3fb1c5b7a19cde632ce14ca81731d54ab
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a88c6b56a8fbc39292857490b31a92d23091357cb986a9301c1402608f6eab3c15d62c60b7cd137ea6ab48dbff617e109092b211e8c768c9b98c13f6e0182e1b
|
7
|
+
data.tar.gz: 2175a6d316649b42119dae94d6ffe309008085cbca1ffff0ca3540ef8bfffeae26ec6d6a6a72522f95b777a04858ce7f28ea0008a0a4c165448a5ec32aa341ec
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# Logbrarian
|
2
|
+
|
3
|
+
Manage and rotate logs
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'logbrarian'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install logbrarian
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
`$ cat file | logbrarian /var/log/cat/cat.log`
|
24
|
+
Outputs
|
25
|
+
```
|
26
|
+
/var/log/cat/cat.log
|
27
|
+
/var/log/cat/cat_11-12-16.log.gz
|
28
|
+
/var/log/cat/cat_11-13-16.log.gz
|
29
|
+
```
|
30
|
+
|
31
|
+
## Development
|
32
|
+
|
33
|
+
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.
|
34
|
+
|
35
|
+
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).
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sleepinginsomniac/logbrarian.
|
40
|
+
|
41
|
+
|
42
|
+
## License
|
43
|
+
|
44
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
45
|
+
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "logbrarian"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/logbrarian
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
lib = File.expand_path('../../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require 'logbrarian'
|
7
|
+
require 'optparse'
|
8
|
+
require 'fileutils'
|
9
|
+
require 'active_support/core_ext/time/calculations'
|
10
|
+
require 'active_support/core_ext/numeric/time'
|
11
|
+
|
12
|
+
$stdout.sync = true
|
13
|
+
$stdin.sync = true
|
14
|
+
|
15
|
+
options = {
|
16
|
+
interval: 1.day,
|
17
|
+
format: "%Y-%m-%d"
|
18
|
+
}
|
19
|
+
OptionParser.new do |opts|
|
20
|
+
opts.banner = "Usage: <stream> | logbrarian /path/to/file.log [options]"
|
21
|
+
|
22
|
+
opts.on("--version", "Show Version") do
|
23
|
+
$stdout.puts Logbrarian::VERSION
|
24
|
+
exit
|
25
|
+
end
|
26
|
+
|
27
|
+
opts.on("-h", "--help") do
|
28
|
+
$stdout.puts opts
|
29
|
+
exit
|
30
|
+
end
|
31
|
+
|
32
|
+
opts.on("-i INTERVAL", "--interval INTERVAL", "Set log rotation interval") do |interval|
|
33
|
+
interval = interval.to_sym
|
34
|
+
if %i[year month week day hour minute second].include?(interval)
|
35
|
+
options[:interval] = 1.send(interval)
|
36
|
+
else
|
37
|
+
throw "Unknown interval: `#{interval}`"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
opts.on("-f FORMAT", "--format FORMAT", "filename format") do |f|
|
42
|
+
options[:format] = f
|
43
|
+
end
|
44
|
+
end.parse!
|
45
|
+
|
46
|
+
current_path = ARGV.shift
|
47
|
+
FileUtils.touch current_path unless File.exist? current_path
|
48
|
+
|
49
|
+
if File.birthtime(current_path) < options[:interval].ago
|
50
|
+
# Rotate older file
|
51
|
+
require 'zlib'
|
52
|
+
dir_path = File.dirname(current_path)
|
53
|
+
current_name = File.basename(current_path)[/[^\.]+/]
|
54
|
+
|
55
|
+
time_stamp = options[:interval].ago.strftime(options[:format])
|
56
|
+
old_log = File.join(dir_path, "#{current_name}_#{time_stamp}.log.gz")
|
57
|
+
Zlib::GzipWriter.open(old_log) do |archive|
|
58
|
+
File.foreach(current_path) do |line|
|
59
|
+
archive.write line
|
60
|
+
end
|
61
|
+
end
|
62
|
+
FileUtils.rm current_path
|
63
|
+
FileUtils.touch current_path
|
64
|
+
end
|
65
|
+
|
66
|
+
File.open(current_path, 'a') do |log|
|
67
|
+
ARGF.each do |line|
|
68
|
+
log << line
|
69
|
+
end
|
70
|
+
end
|
data/bin/setup
ADDED
data/lib/logbrarian.rb
ADDED
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: logbrarian
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex Clink
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Useful for archiving old logs
|
70
|
+
email:
|
71
|
+
- code@alexclink.com
|
72
|
+
executables:
|
73
|
+
- logbrarian
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- README.md
|
78
|
+
- bin/console
|
79
|
+
- bin/logbrarian
|
80
|
+
- bin/setup
|
81
|
+
- lib/logbrarian.rb
|
82
|
+
- lib/logbrarian/version.rb
|
83
|
+
homepage: https://pixelfaucet.com/gems/logbrarian
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata: {}
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 2.4.8
|
104
|
+
signing_key:
|
105
|
+
specification_version: 4
|
106
|
+
summary: Manage / Rotate logs
|
107
|
+
test_files: []
|