hourly_logger_rotator 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d8d774882a0a310ba65664a1b1fff0fa20844de51266996a0f3b555faf5f4d87
4
- data.tar.gz: f880ffdc11abbe033de8275f6db0a584196ad1c4fd01a86a9c2d4e5e86067f8e
3
+ metadata.gz: 37ba72e1b0c1b5517b1681a450164b4c5bb64feefd051a1421eaeea3c3d5844c
4
+ data.tar.gz: 4fc662711182b5a7119bdf50074c52d1a6f78e22884acfe6744cd722a941b939
5
5
  SHA512:
6
- metadata.gz: 70aaf82624b3f3a6c56f8a7e04412058d4279c35b7a7ca0e88cc780819b08fe924b798eec69d4fed617ace069ef99968f32747c9787fd1b770d93a37fe0f4e42
7
- data.tar.gz: 3474702dca641a2f2a806b92d63d0fd72f5a21caa4a7ef8dd87afe629636a873fe50a399858704dd698e5e5bc5b85641c9317ad096ee2a324dfc248b2e79d33e
6
+ metadata.gz: 5e03e918c4ee8756e7d4a47e3fe46b15e64a966b46ba75cb78b09477e5e089c3ea206b2c64218892a718352da8b59fd71e40e5641ce92d453131de35ade83d08
7
+ data.tar.gz: 79bd4e3420eb628ddeb953077cbc4328f302de02f3760c7470d18fed666c36b0a89a56a24f5ad13fadb8d68b47aedb826c43a76c4f30ce0b7de46e5097f17b35
data/.gitignore CHANGED
@@ -6,5 +6,6 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ /log
9
10
  .rspec_status
10
11
  .ruby-version
@@ -0,0 +1,6 @@
1
+ inherit_gem:
2
+ rubocop-config-umbrellio: lib/rubocop.yml
3
+
4
+ AllCops:
5
+ DisplayCopNames: true
6
+ TargetRubyVersion: 2.5
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
5
  and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.2.0] - 2018-03-12
8
+ ### Added
9
+ - Ruby 2.3, 2.4, 2.5 support
10
+
7
11
  ## [0.1.0] - 2018-03-05
8
12
  ### Added
9
13
  - Initial release
data/Gemfile CHANGED
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source "https://rubygems.org"
2
4
  gemspec
@@ -1,17 +1,23 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hourly_logger_rotator (0.1.0)
4
+ hourly_logger_rotator (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
+ ast (2.4.0)
9
10
  coderay (1.1.2)
10
11
  diff-lcs (1.3)
11
12
  method_source (0.9.0)
13
+ parallel (1.12.1)
14
+ parser (2.4.0.2)
15
+ ast (~> 2.3)
16
+ powerpack (0.1.1)
12
17
  pry (0.11.3)
13
18
  coderay (~> 1.1.0)
14
19
  method_source (~> 0.9.0)
20
+ rainbow (3.0.0)
15
21
  rake (10.5.0)
16
22
  rspec (3.7.0)
17
23
  rspec-core (~> 3.7.0)
@@ -26,6 +32,21 @@ GEM
26
32
  diff-lcs (>= 1.2.0, < 2.0)
27
33
  rspec-support (~> 3.7.0)
28
34
  rspec-support (3.7.1)
35
+ rubocop (0.52.1)
36
+ parallel (~> 1.10)
37
+ parser (>= 2.4.0.2, < 3.0)
38
+ powerpack (~> 0.1)
39
+ rainbow (>= 2.2.2, < 4.0)
40
+ ruby-progressbar (~> 1.7)
41
+ unicode-display_width (~> 1.0, >= 1.0.1)
42
+ rubocop-config-umbrellio (0.52.1.0)
43
+ rubocop (= 0.52.1)
44
+ rubocop-rspec (= 1.22.1)
45
+ rubocop-rspec (1.22.1)
46
+ rubocop (>= 0.52.1)
47
+ ruby-progressbar (1.9.0)
48
+ timecop (0.9.1)
49
+ unicode-display_width (1.3.0)
29
50
 
30
51
  PLATFORMS
31
52
  ruby
@@ -36,6 +57,8 @@ DEPENDENCIES
36
57
  pry
37
58
  rake
38
59
  rspec
60
+ rubocop-config-umbrellio
61
+ timecop
39
62
 
40
63
  BUNDLED WITH
41
64
  1.16.1
data/README.md CHANGED
@@ -1,3 +1,43 @@
1
1
  # HourlyLoggerRotator
2
2
 
3
- WIP
3
+ Logger class patch for hourly log rotation support
4
+
5
+ ## Requirements
6
+
7
+ Ruby 2.3, 2.4 or 2.5
8
+
9
+ ## Installation
10
+
11
+ - `gem install hourly_logger_rotator` and `require "hourly_logger_rotation"`
12
+ - Or add it to your Gemfile and `bundle`
13
+
14
+ ## Usage
15
+
16
+ Requiring the gem automatically adds an `hourly` rotation period support to Ruby's standard
17
+ Logger class. You can initialize it like this:
18
+
19
+ ```
20
+ Logger.new('some_log_file', 'hourly')
21
+ ```
22
+
23
+ Keep in mind that loggers created before the gem is required will not
24
+ support the hourly rotation period. Specifically, in case of a Rails app,
25
+ require this before Rails initializes; a good place to do that is in `application.rb`
26
+ right after requiring `boot`
27
+
28
+ ### Setting default rotation period
29
+
30
+ `HourlyLoggerRotator.default_rotation_period=(some_period)` will make it so newly created
31
+ loggers have a rotation period of `some_period` unless you provide an explicit
32
+ period in the constructor
33
+
34
+ ## License
35
+ Released under MIT License
36
+
37
+ ## Authors
38
+ Created by Dmitry Gubitskiy
39
+
40
+ <a href="https://github.com/umbrellio/">
41
+ <img style="float: left;" src="https://umbrellio.github.io/Umbrellio/supported_by_umbrellio.svg"
42
+ alt="Supported by Umbrellio" width="439" height="72">
43
+ </a>
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/gem_tasks"
2
4
  require "rspec/core/rake_task"
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
6
- task :default => :spec
8
+ task default: :spec
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "bundler/setup"
4
+ require "pry"
4
5
  require "hourly_logger_rotator"
5
6
 
6
- require "pry"
7
7
  Pry.start
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  lib = File.expand_path("../lib", __FILE__)
2
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
5
  require "hourly_logger_rotator/version"
@@ -9,14 +11,16 @@ Gem::Specification.new do |spec|
9
11
  spec.email = ["d.gubitskiy@gmail.com"]
10
12
 
11
13
  spec.summary = "Ruby Logger patch for hourly log rotation"
12
- spec.homepage = "https://github.com/umbrellio/hourly_log_rotator"
14
+ spec.homepage = "https://github.com/umbrellio/hourly_logger_rotator"
13
15
  spec.license = "MIT"
14
16
 
15
17
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^spec/}) }
16
18
  spec.require_paths = ["lib"]
17
19
 
18
20
  spec.add_development_dependency "bundler"
21
+ spec.add_development_dependency "pry"
19
22
  spec.add_development_dependency "rake"
20
23
  spec.add_development_dependency "rspec"
21
- spec.add_development_dependency "pry"
24
+ spec.add_development_dependency "rubocop-config-umbrellio"
25
+ spec.add_development_dependency "timecop"
22
26
  end
@@ -1,4 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "logger"
1
4
  require "hourly_logger_rotator/version"
2
5
 
6
+ pattern = File.join(File.dirname(__FILE__), "hourly_logger_rotator", "patch", "*.rb")
7
+ Dir[pattern].each { |file| require_relative(file) }
8
+
3
9
  module HourlyLoggerRotator
10
+ class << self
11
+ def add_hourly_rotation_period_support!
12
+ if hourly_mixin
13
+ Logger::LogDevice.prepend(hourly_mixin)
14
+ else
15
+ warn unsupported_warning
16
+ end
17
+ end
18
+
19
+ def default_rotation_period=(period)
20
+ raise unsupported_warning if period == "hourly" && !hourly_mixin
21
+
22
+ mixin = Module.new do
23
+ define_method(:initialize) do |logdev, shift_age = period, *args|
24
+ super(logdev, shift_age, *args)
25
+ end
26
+ end
27
+
28
+ Logger.prepend(mixin)
29
+ end
30
+
31
+ private
32
+
33
+ def hourly_mixin
34
+ return @mixin if defined?(@mixin)
35
+
36
+ mixin_name = "HourlyLoggerRotator::Patch::Ruby_#{RUBY_VERSION.split('.').first(2).join('_')}"
37
+ @mixin = begin
38
+ Module.const_get(mixin_name)
39
+ rescue NameError
40
+ nil
41
+ end
42
+ end
43
+
44
+ def unsupported_warning
45
+ "Hourly log rotation period is not supported for Ruby #{RUBY_VERSION}"
46
+ end
47
+ end
4
48
  end
49
+
50
+ HourlyLoggerRotator.add_hourly_rotation_period_support!
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "logger"
4
+
5
+ module HourlyLoggerRotator
6
+ module Patch
7
+ # rubocop:disable Naming/ClassAndModuleCamelCase
8
+ module Ruby_2_3
9
+ # rubocop:enable Naming/ClassAndModuleCamelCase
10
+ SiD = Logger::Period::SiD
11
+
12
+ def shift_log_period(period_end)
13
+ format = @shift_age == "hourly" ? "%Y%m%d%H" : "%Y%m%d"
14
+ postfix = period_end.strftime(format)
15
+ age_file = "#{@filename}.#{postfix}"
16
+ if FileTest.exist?(age_file)
17
+ # try to avoid filename crash caused by Timestamp change.
18
+ idx = 0
19
+ # .99 can be overridden; avoid too much file search with 'loop do'
20
+ while idx < 100
21
+ idx += 1
22
+ age_file = "#{@filename}.#{postfix}.#{idx}"
23
+ break unless FileTest.exist?(age_file)
24
+ end
25
+ end
26
+ @dev.close rescue nil
27
+ File.rename(@filename.to_s, age_file)
28
+ @dev = create_logfile(@filename)
29
+ true
30
+ end
31
+
32
+ def next_rotate_time(now, shift_age)
33
+ case shift_age
34
+ when "hourly"
35
+ t = Time.mktime(now.year, now.month, now.mday, now.hour) + SiD / 24
36
+ when "daily"
37
+ t = Time.mktime(now.year, now.month, now.mday) + SiD
38
+ when "weekly"
39
+ t = Time.mktime(now.year, now.month, now.mday) + SiD * (7 - now.wday)
40
+ when "monthly"
41
+ t = Time.mktime(now.year, now.month, 1) + SiD * 32
42
+ return Time.mktime(t.year, t.month, 1)
43
+ else
44
+ return now
45
+ end
46
+ if t.min.nonzero? or t.sec.nonzero?
47
+ min = t.min
48
+ t = Time.mktime(t.year, t.month, t.mday, t.hour)
49
+ t += (SiD / 24) if min > 30
50
+ end
51
+ t
52
+ end
53
+
54
+ def previous_period_end(now, shift_age)
55
+ case shift_age
56
+ when "hourly"
57
+ t = Time.mktime(now.year, now.month, now.mday, now.hour) - 1
58
+ when "daily"
59
+ t = Time.mktime(now.year, now.month, now.mday) - 1
60
+ when "weekly"
61
+ t = Time.mktime(now.year, now.month, now.mday) - (SiD * now.wday + 1)
62
+ when "monthly"
63
+ t = Time.mktime(now.year, now.month, 1) - 1
64
+ else
65
+ return now
66
+ end
67
+ Time.mktime(t.year, t.month, t.mday, t.hour, 59, 59)
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HourlyLoggerRotator
4
+ module Patch
5
+ # rubocop:disable Naming/ClassAndModuleCamelCase
6
+ module Ruby_2_4
7
+ # rubocop:enable Naming/ClassAndModuleCamelCase
8
+ SiD = Logger::Period::SiD
9
+
10
+ def initialize(log = nil, shift_age: nil, shift_size: nil, shift_period_suffix: nil)
11
+ @dev = @filename = @shift_age = @shift_size = @shift_period_suffix = nil
12
+ mon_initialize
13
+ set_dev(log)
14
+ if @filename
15
+ @shift_age = shift_age || 7
16
+ @shift_size = shift_size || 1048576
17
+ suffix = shift_age == "hourly" ? "%Y%m%d%H" : (shift_period_suffix || "%Y%m%d")
18
+ @shift_period_suffix = suffix
19
+
20
+ unless @shift_age.is_a?(Integer)
21
+ base_time = @dev.respond_to?(:stat) ? @dev.stat.mtime : Time.now
22
+ @next_rotate_time = next_rotate_time(base_time, @shift_age)
23
+ end
24
+ end
25
+ end
26
+
27
+ def next_rotate_time(now, shift_age)
28
+ case shift_age
29
+ when "hourly"
30
+ t = Time.mktime(now.year, now.month, now.mday, now.hour) + SiD / 24
31
+ when "daily"
32
+ t = Time.mktime(now.year, now.month, now.mday) + SiD
33
+ when "weekly"
34
+ t = Time.mktime(now.year, now.month, now.mday) + SiD * (7 - now.wday)
35
+ when "monthly"
36
+ t = Time.mktime(now.year, now.month, 1) + SiD * 32
37
+ return Time.mktime(t.year, t.month, 1)
38
+ else
39
+ return now
40
+ end
41
+ if t.min.nonzero? or t.sec.nonzero?
42
+ min = t.min
43
+ t = Time.mktime(t.year, t.month, t.mday, t.hour)
44
+ t += (SiD / 24) if min > 30
45
+ end
46
+ t
47
+ end
48
+
49
+ def previous_period_end(now, shift_age)
50
+ case shift_age
51
+ when "hourly"
52
+ t = Time.mktime(now.year, now.month, now.mday, now.hour) - 1
53
+ when "daily"
54
+ t = Time.mktime(now.year, now.month, now.mday) - 1
55
+ when "weekly"
56
+ t = Time.mktime(now.year, now.month, now.mday) - (SiD * now.wday + 1)
57
+ when "monthly"
58
+ t = Time.mktime(now.year, now.month, 1) - 1
59
+ else
60
+ return now
61
+ end
62
+ Time.mktime(t.year, t.month, t.mday, t.hour, 59, 59)
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "hourly_logger_rotator/patch/ruby_2_4"
4
+
5
+ module HourlyLoggerRotator
6
+ module Patch
7
+ # rubocop:disable Naming/ClassAndModuleCamelCase
8
+ module Ruby_2_5
9
+ # rubocop:enable Naming/ClassAndModuleCamelCase
10
+ include Ruby_2_4
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module HourlyLoggerRotator
2
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
3
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hourly_logger_rotator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Gubitskiy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-05 00:00:00.000000000 Z
11
+ date: 2018-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -53,7 +67,21 @@ dependencies:
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
- name: pry
70
+ name: rubocop-config-umbrellio
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: timecop
57
85
  requirement: !ruby/object:Gem::Requirement
58
86
  requirements:
59
87
  - - ">="
@@ -75,6 +103,7 @@ extra_rdoc_files: []
75
103
  files:
76
104
  - ".gitignore"
77
105
  - ".rspec"
106
+ - ".rubocop.yml"
78
107
  - ".travis.yml"
79
108
  - CHANGELOG.md
80
109
  - CODE_OF_CONDUCT.md
@@ -87,8 +116,11 @@ files:
87
116
  - bin/console
88
117
  - hourly_logger_rotator.gemspec
89
118
  - lib/hourly_logger_rotator.rb
119
+ - lib/hourly_logger_rotator/patch/ruby_2_3.rb
120
+ - lib/hourly_logger_rotator/patch/ruby_2_4.rb
121
+ - lib/hourly_logger_rotator/patch/ruby_2_5.rb
90
122
  - lib/hourly_logger_rotator/version.rb
91
- homepage: https://github.com/umbrellio/hourly_log_rotator
123
+ homepage: https://github.com/umbrellio/hourly_logger_rotator
92
124
  licenses:
93
125
  - MIT
94
126
  metadata: {}