chrono_logger 0.0.5 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/ruby.yml +36 -0
- data/CHANGELOG.md +15 -0
- data/README.md +30 -8
- data/chrono_logger.gemspec +2 -3
- data/lib/chrono_logger/version.rb +1 -1
- data/lib/chrono_logger.rb +14 -7
- data/test/from_ruby_repo/test_logger.rb +4 -2
- data/test/helper.rb +2 -2
- data/test/test_chrono_logger.rb +42 -0
- metadata +13 -27
- data/.travis.yml +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ee8232f6e93fdb6766c7685404c008954c26570cc08ee7d9a26df0492b721ad3
|
4
|
+
data.tar.gz: 8c362f4ef826b32830a4326ee6929fc6731579cb0a55f871757fcc4233338ea2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 60f8897ee73f13eedc17b65de6c0c64339827c6abd6b8892ed95a81ad24c50f3b346b5a4667f721a2c3183cefcd9384ca0f293623c1805befb1b863695976525
|
7
|
+
data.tar.gz: e4676e13ab6e54452249fa1e089d96e1d19a5d76c935ff8cc9945592b5f47a421ce6ecf7e833363dc3f408a94975bc178442dc97182e00908c792d491fec2455
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# This workflow uses actions that are not certified by GitHub.
|
2
|
+
# They are provided by a third-party and are governed by
|
3
|
+
# separate terms of service, privacy policy, and support
|
4
|
+
# documentation.
|
5
|
+
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
|
6
|
+
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
|
7
|
+
|
8
|
+
name: Ruby
|
9
|
+
|
10
|
+
on:
|
11
|
+
push:
|
12
|
+
branches: [ master ]
|
13
|
+
pull_request:
|
14
|
+
|
15
|
+
jobs:
|
16
|
+
test:
|
17
|
+
|
18
|
+
runs-on: ubuntu-latest
|
19
|
+
strategy:
|
20
|
+
matrix:
|
21
|
+
ruby: [ '2.6', '2.7', '3.0', '3.1' ]
|
22
|
+
|
23
|
+
steps:
|
24
|
+
- uses: actions/checkout@v2
|
25
|
+
- name: Set up Ruby
|
26
|
+
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
|
27
|
+
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
28
|
+
uses: ruby/setup-ruby@v1
|
29
|
+
with:
|
30
|
+
ruby-version: ${{ matrix.ruby }}
|
31
|
+
- name: Install dependencies
|
32
|
+
run: |
|
33
|
+
bundle config path vendor/bundle
|
34
|
+
bundle install --jobs 4 --retry 3
|
35
|
+
- name: Run tests
|
36
|
+
run: bundle exec rake
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -1,11 +1,8 @@
|
|
1
1
|
# ChronoLogger
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/chrono_logger.svg)](http://badge.fury.io/rb/chrono_logger)
|
4
|
-
[![Build Status](https://
|
4
|
+
[![Build Status](https://github.com/ma2gedev/chrono_logger/workflows/Ruby/badge.svg?branch=master)](https://github.com/ma2gedev/chrono_logger/actions?query=workflow%3ARuby)
|
5
5
|
[![Code Climate](https://codeclimate.com/github/ma2gedev/chrono_logger/badges/gpa.svg)](https://codeclimate.com/github/ma2gedev/chrono_logger)
|
6
|
-
[![Coverage Status](https://coveralls.io/repos/ma2gedev/chrono_logger/badge.svg)](https://coveralls.io/r/ma2gedev/chrono_logger)
|
7
|
-
[![Inline docs](http://inch-ci.org/github/ma2gedev/chrono_logger.svg?branch=master)](http://inch-ci.org/github/ma2gedev/chrono_logger)
|
8
|
-
[![endorse](https://api.coderwall.com/ma2gedev/endorsecount.png)](https://coderwall.com/ma2gedev)
|
9
6
|
|
10
7
|
A lock-free logger with timebased file rotation.
|
11
8
|
|
@@ -33,8 +30,8 @@ File.exist?('/log/production.log.20150127')
|
|
33
30
|
Current my projects uses `::Logger` with cronolog. So
|
34
31
|
|
35
32
|
- Reduce dependency such as cronolog
|
36
|
-
- Remove mutexes in ruby world because os already does
|
37
|
-
- Support time based rotation without renaming file because file renaming
|
33
|
+
- Remove mutexes in ruby world because os already does when some environments (ex: ext4 file system)
|
34
|
+
- Support time based rotation without renaming file because file renaming sometime makes problem
|
38
35
|
|
39
36
|
## Installation
|
40
37
|
|
@@ -56,7 +53,7 @@ Or install it yourself as:
|
|
56
53
|
|
57
54
|
Same interfaces ruby's stdlib `Logger` except for `new` method.
|
58
55
|
|
59
|
-
```
|
56
|
+
```ruby
|
60
57
|
require 'chrono_logger'
|
61
58
|
|
62
59
|
# specify path with `Time#strftime` format
|
@@ -68,9 +65,29 @@ logger.info("Enjoy")
|
|
68
65
|
logger.debug("programming!")
|
69
66
|
```
|
70
67
|
|
68
|
+
With Rails:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
# in config/environments/{development,production}.rb
|
72
|
+
|
73
|
+
config.logger = ChronoLogger.new("#{config.paths['log'].first}.%Y%m%d")
|
74
|
+
```
|
75
|
+
|
76
|
+
## Migrating from `::Logger` with cronolog
|
77
|
+
|
78
|
+
You only change `Logger.new` into `ChronoLogger.new`:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
# for instance your setup is like the following
|
82
|
+
Logger.new(IO.popen("/usr/sbin/cronolog production.%Y%m%d", "w"))
|
83
|
+
|
84
|
+
# turns into
|
85
|
+
ChronoLogger.new('production.%Y%m%d')
|
86
|
+
```
|
87
|
+
|
71
88
|
## Limitation
|
72
89
|
|
73
|
-
- High performance only daily based time formatting path for example `'%Y%m%d'`. You can create pull request if you need other time period.
|
90
|
+
- High performance logging only daily based time formatting path for example `'%Y%m%d'`. You can create pull request if you need other time period.
|
74
91
|
|
75
92
|
## Contributing
|
76
93
|
|
@@ -83,3 +100,8 @@ logger.debug("programming!")
|
|
83
100
|
## License
|
84
101
|
|
85
102
|
MIT. See [LICENSE.txt](LICENSE.txt) for more details.
|
103
|
+
|
104
|
+
## Resources
|
105
|
+
|
106
|
+
- [ChronoLogger logging is 1.5x faster than ruby's stdlib Logger](https://coderwall.com/p/vjjszq/chronologger-logging-is-1-5x-faster-than-ruby-s-stdlib-logger)
|
107
|
+
|
data/chrono_logger.gemspec
CHANGED
@@ -18,13 +18,12 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_development_dependency "
|
22
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
21
|
+
spec.add_development_dependency "rake", ">= 0"
|
23
22
|
spec.add_development_dependency "test-unit"
|
24
23
|
spec.add_development_dependency "pry"
|
25
24
|
spec.add_development_dependency "delorean"
|
26
25
|
spec.add_development_dependency "parallel"
|
27
|
-
spec.add_development_dependency "
|
26
|
+
spec.add_development_dependency "simplecov"
|
28
27
|
|
29
28
|
# for performance check
|
30
29
|
spec.add_development_dependency "mono_logger"
|
data/lib/chrono_logger.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "chrono_logger/version"
|
2
2
|
require 'logger'
|
3
|
+
require 'pathname'
|
3
4
|
|
4
5
|
# A lock-free logger with timebased file rotation.
|
5
6
|
class ChronoLogger < Logger
|
@@ -53,8 +54,13 @@ class ChronoLogger < Logger
|
|
53
54
|
DELAY_SECOND_TO_CLOSE_FILE = 5
|
54
55
|
|
55
56
|
def initialize(log = nil, opt = {})
|
56
|
-
@dev = @filename = nil
|
57
|
-
|
57
|
+
@dev = @filename = @pattern = nil
|
58
|
+
if defined?(LogDeviceMutex) # Ruby < 2.3
|
59
|
+
@mutex = LogDeviceMutex.new
|
60
|
+
else
|
61
|
+
mon_initialize
|
62
|
+
@mutex = self
|
63
|
+
end
|
58
64
|
if log.respond_to?(:write) and log.respond_to?(:close)
|
59
65
|
@dev = log
|
60
66
|
else
|
@@ -69,7 +75,7 @@ class ChronoLogger < Logger
|
|
69
75
|
end
|
70
76
|
|
71
77
|
def write(message)
|
72
|
-
|
78
|
+
check_and_shift_log if @pattern
|
73
79
|
@dev.write(message)
|
74
80
|
rescue
|
75
81
|
warn("log writing failed. #{$!}")
|
@@ -91,6 +97,7 @@ class ChronoLogger < Logger
|
|
91
97
|
|
92
98
|
def create_logfile(filename)
|
93
99
|
begin
|
100
|
+
Pathname(filename).dirname.mkpath
|
94
101
|
logdev = open(filename, (File::WRONLY | File::APPEND | File::CREAT | File::EXCL))
|
95
102
|
logdev.sync = true
|
96
103
|
rescue Errno::EEXIST
|
@@ -101,12 +108,12 @@ class ChronoLogger < Logger
|
|
101
108
|
logdev
|
102
109
|
end
|
103
110
|
|
104
|
-
def
|
111
|
+
def check_and_shift_log
|
105
112
|
if next_period?(Time.now)
|
106
113
|
now = Time.now
|
107
114
|
new_filename = now.strftime(@pattern)
|
108
115
|
next_start_period = next_start_period(now, @period)
|
109
|
-
|
116
|
+
shift_log(new_filename)
|
110
117
|
@filename = new_filename
|
111
118
|
@next_start_period = next_start_period
|
112
119
|
end
|
@@ -120,7 +127,7 @@ class ChronoLogger < Logger
|
|
120
127
|
end
|
121
128
|
end
|
122
129
|
|
123
|
-
def
|
130
|
+
def shift_log(filename)
|
124
131
|
begin
|
125
132
|
@mutex.synchronize do
|
126
133
|
tmp_dev = @dev
|
@@ -131,7 +138,7 @@ class ChronoLogger < Logger
|
|
131
138
|
end
|
132
139
|
end
|
133
140
|
rescue Exception => ignored
|
134
|
-
warn("log shifting failed. #{
|
141
|
+
warn("log shifting failed. #{ignored}")
|
135
142
|
end
|
136
143
|
end
|
137
144
|
end
|
@@ -12,6 +12,7 @@ class TestLogger < Test::Unit::TestCase
|
|
12
12
|
class Log
|
13
13
|
attr_reader :label, :datetime, :pid, :severity, :progname, :msg
|
14
14
|
def initialize(line)
|
15
|
+
# NOTE: adapt the following pattern ruby's original test after support only ruby 3.1 or higher
|
15
16
|
/\A(\w+), \[([^#]*)#(\d+)\]\s+(\w+) -- (\w*): ([\x0-\xff]*)/ =~ line
|
16
17
|
@label, @datetime, @pid, @severity, @progname, @msg = $1, $2, $3, $4, $5, $6
|
17
18
|
end
|
@@ -82,10 +83,11 @@ class TestLogger < Test::Unit::TestCase
|
|
82
83
|
assert_match(/^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\s*\d+ $/, log.datetime)
|
83
84
|
logger.datetime_format = "%d%b%Y@%H:%M:%S"
|
84
85
|
log = log_add(logger, INFO, "foo")
|
85
|
-
|
86
|
+
# NOTE: ` ?` contained in the following will be removed after support only ruby 3.1 or higher
|
87
|
+
assert_match(/^\d\d\w\w\w\d\d\d\d@\d\d:\d\d:\d\d ?$/, log.datetime)
|
86
88
|
logger.datetime_format = ""
|
87
89
|
log = log_add(logger, INFO, "foo")
|
88
|
-
assert_match(
|
90
|
+
assert_match(/^ ?$/, log.datetime)
|
89
91
|
end
|
90
92
|
|
91
93
|
def test_formatter
|
data/test/helper.rb
CHANGED
data/test/test_chrono_logger.rb
CHANGED
@@ -29,6 +29,31 @@ class TestChronoLogger < Test::Unit::TestCase
|
|
29
29
|
confirm_daily_rotation(in_processes: 2)
|
30
30
|
end
|
31
31
|
|
32
|
+
def test_exception_on_shifting_log
|
33
|
+
old_log = [@tempfile.path, '20220101'].join
|
34
|
+
new_log = [@tempfile.path, '20220102'].join
|
35
|
+
$stderr, stderr = StringIO.new, $stderr
|
36
|
+
begin
|
37
|
+
Delorean.time_travel_to '2022-01-01 23:59:59.990'
|
38
|
+
logger = ChronoLogger.new(@format)
|
39
|
+
old_logdev = logger.instance_variable_get('@logdev')
|
40
|
+
def old_logdev.create_logfile(filename)
|
41
|
+
raise 'override create_logfile method'
|
42
|
+
end
|
43
|
+
sleep 1 # waiting for shift log file
|
44
|
+
|
45
|
+
logger.info 'shift log'
|
46
|
+
|
47
|
+
assert_match(/log shifting failed\. override create_logfile method/, $stderr.string)
|
48
|
+
assert { File.exist?(old_log) }
|
49
|
+
assert { !File.exist?(new_log) }
|
50
|
+
ensure
|
51
|
+
$stderr, stderr = stderr, $stderr
|
52
|
+
Delorean.back_to_the_present
|
53
|
+
File.unlink(old_log)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
32
57
|
def test_rotation_per_second
|
33
58
|
Dir.mktmpdir do |tmpdir|
|
34
59
|
begin
|
@@ -46,6 +71,23 @@ class TestChronoLogger < Test::Unit::TestCase
|
|
46
71
|
end
|
47
72
|
end
|
48
73
|
|
74
|
+
def test_rotation_per_day_and_create_dir
|
75
|
+
Dir.mktmpdir do |tmpdir|
|
76
|
+
begin
|
77
|
+
Delorean.time_travel_to '2015-08-01 23:59:50'
|
78
|
+
logger = ChronoLogger.new([tmpdir, '/%Y/%m/%d/test.log'].join)
|
79
|
+
logger.debug 'rotation'
|
80
|
+
Delorean.time_travel_to '2015-08-02 00:00:01'
|
81
|
+
logger.debug 'new dir'
|
82
|
+
|
83
|
+
assert { File.exist?([tmpdir, '/2015/08/01/test.log'].join) }
|
84
|
+
assert { File.exist?([tmpdir, '/2015/08/02/test.log'].join) }
|
85
|
+
ensure
|
86
|
+
Delorean.back_to_the_present
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
49
91
|
class PeriodTest
|
50
92
|
include ChronoLogger::Period
|
51
93
|
end
|
metadata
CHANGED
@@ -1,43 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chrono_logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Takayuki Matsubara
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-01-01 00:00:00.000000000 Z
|
12
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.7'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '1.7'
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: rake
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
30
16
|
requirements:
|
31
|
-
- - "
|
17
|
+
- - ">="
|
32
18
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
19
|
+
version: '0'
|
34
20
|
type: :development
|
35
21
|
prerelease: false
|
36
22
|
version_requirements: !ruby/object:Gem::Requirement
|
37
23
|
requirements:
|
38
|
-
- - "
|
24
|
+
- - ">="
|
39
25
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
26
|
+
version: '0'
|
41
27
|
- !ruby/object:Gem::Dependency
|
42
28
|
name: test-unit
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -95,7 +81,7 @@ dependencies:
|
|
95
81
|
- !ruby/object:Gem::Version
|
96
82
|
version: '0'
|
97
83
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
84
|
+
name: simplecov
|
99
85
|
requirement: !ruby/object:Gem::Requirement
|
100
86
|
requirements:
|
101
87
|
- - ">="
|
@@ -143,8 +129,9 @@ executables: []
|
|
143
129
|
extensions: []
|
144
130
|
extra_rdoc_files: []
|
145
131
|
files:
|
132
|
+
- ".github/workflows/ruby.yml"
|
146
133
|
- ".gitignore"
|
147
|
-
-
|
134
|
+
- CHANGELOG.md
|
148
135
|
- Gemfile
|
149
136
|
- LICENSE.txt
|
150
137
|
- README.md
|
@@ -161,7 +148,7 @@ homepage: https://github.com/ma2gedev/chrono_logger
|
|
161
148
|
licenses:
|
162
149
|
- MIT
|
163
150
|
metadata: {}
|
164
|
-
post_install_message:
|
151
|
+
post_install_message:
|
165
152
|
rdoc_options: []
|
166
153
|
require_paths:
|
167
154
|
- lib
|
@@ -176,9 +163,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
176
163
|
- !ruby/object:Gem::Version
|
177
164
|
version: '0'
|
178
165
|
requirements: []
|
179
|
-
|
180
|
-
|
181
|
-
signing_key:
|
166
|
+
rubygems_version: 3.2.3
|
167
|
+
signing_key:
|
182
168
|
specification_version: 4
|
183
169
|
summary: A lock-free logger with timebased file rotation.
|
184
170
|
test_files:
|