logger 1.5.3 → 1.7.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 +4 -4
- data/.document +4 -0
- data/.rdoc_options +3 -0
- data/BSDL +22 -0
- data/COPYING +56 -0
- data/README.md +104 -0
- data/lib/logger/log_device.rb +126 -68
- data/lib/logger/period.rb +8 -8
- data/lib/logger/severity.rb +19 -0
- data/lib/logger/version.rb +1 -1
- data/lib/logger.rb +70 -26
- metadata +12 -49
- data/logger.gemspec +0 -26
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c84ac2b70e090b47e48cc054906f75f21b26b3648dad0b363f54bba83f37e372
|
|
4
|
+
data.tar.gz: f7b0cbf4e55fd9c5831cb2500e21d27faa6bc02356ddc484d819a4ef806e4dee
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1ffa55991e59e4dadd824e1bcf19b0deee701fc9512ba4dc97f15d45f7450bff12422266bac8b9d041222cb386bd5b70ffbb2379107a2c76cd5a4cdcf418a9cf
|
|
7
|
+
data.tar.gz: 03e58b69f2e3f0eae398506fc1aa417d70fe17c190efa2c4c2ceb558b90bcadee62e088929a774f3d01242c9e72486f365b9555a78419c8553140a3517f2dad3
|
data/.document
ADDED
data/.rdoc_options
ADDED
data/BSDL
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (C) 1993-2013 Yukihiro Matsumoto. All rights reserved.
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without
|
|
4
|
+
modification, are permitted provided that the following conditions
|
|
5
|
+
are met:
|
|
6
|
+
1. Redistributions of source code must retain the above copyright
|
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright
|
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
|
10
|
+
documentation and/or other materials provided with the distribution.
|
|
11
|
+
|
|
12
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
13
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
14
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
15
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
16
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
17
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
18
|
+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
19
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
20
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
21
|
+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
22
|
+
SUCH DAMAGE.
|
data/COPYING
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.jp>.
|
|
2
|
+
You can redistribute it and/or modify it under either the terms of the
|
|
3
|
+
2-clause BSDL (see the file BSDL), or the conditions below:
|
|
4
|
+
|
|
5
|
+
1. You may make and give away verbatim copies of the source form of the
|
|
6
|
+
software without restriction, provided that you duplicate all of the
|
|
7
|
+
original copyright notices and associated disclaimers.
|
|
8
|
+
|
|
9
|
+
2. You may modify your copy of the software in any way, provided that
|
|
10
|
+
you do at least ONE of the following:
|
|
11
|
+
|
|
12
|
+
a. place your modifications in the Public Domain or otherwise
|
|
13
|
+
make them Freely Available, such as by posting said
|
|
14
|
+
modifications to Usenet or an equivalent medium, or by allowing
|
|
15
|
+
the author to include your modifications in the software.
|
|
16
|
+
|
|
17
|
+
b. use the modified software only within your corporation or
|
|
18
|
+
organization.
|
|
19
|
+
|
|
20
|
+
c. give non-standard binaries non-standard names, with
|
|
21
|
+
instructions on where to get the original software distribution.
|
|
22
|
+
|
|
23
|
+
d. make other distribution arrangements with the author.
|
|
24
|
+
|
|
25
|
+
3. You may distribute the software in object code or binary form,
|
|
26
|
+
provided that you do at least ONE of the following:
|
|
27
|
+
|
|
28
|
+
a. distribute the binaries and library files of the software,
|
|
29
|
+
together with instructions (in the manual page or equivalent)
|
|
30
|
+
on where to get the original distribution.
|
|
31
|
+
|
|
32
|
+
b. accompany the distribution with the machine-readable source of
|
|
33
|
+
the software.
|
|
34
|
+
|
|
35
|
+
c. give non-standard binaries non-standard names, with
|
|
36
|
+
instructions on where to get the original software distribution.
|
|
37
|
+
|
|
38
|
+
d. make other distribution arrangements with the author.
|
|
39
|
+
|
|
40
|
+
4. You may modify and include the part of the software into any other
|
|
41
|
+
software (possibly commercial). But some files in the distribution
|
|
42
|
+
are not written by the author, so that they are not under these terms.
|
|
43
|
+
|
|
44
|
+
For the list of those files and their copying conditions, see the
|
|
45
|
+
file LEGAL.
|
|
46
|
+
|
|
47
|
+
5. The scripts and library files supplied as input to or produced as
|
|
48
|
+
output from the software do not automatically fall under the
|
|
49
|
+
copyright of the software, but belong to whomever generated them,
|
|
50
|
+
and may be sold commercially, and may be aggregated with this
|
|
51
|
+
software.
|
|
52
|
+
|
|
53
|
+
6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
|
54
|
+
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
55
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
56
|
+
PURPOSE.
|
data/README.md
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# Logger
|
|
2
|
+
|
|
3
|
+
Logger is a simple but powerful logging utility to output messages in your Ruby program.
|
|
4
|
+
|
|
5
|
+
Logger has the following features:
|
|
6
|
+
|
|
7
|
+
* Print messages to different levels such as `info` and `error`
|
|
8
|
+
* Auto-rolling of log files
|
|
9
|
+
* Setting the format of log messages
|
|
10
|
+
* Specifying a program name in conjunction with the message
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
Add this line to your application's Gemfile:
|
|
15
|
+
|
|
16
|
+
```ruby
|
|
17
|
+
gem 'logger'
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
And then execute:
|
|
21
|
+
|
|
22
|
+
$ bundle
|
|
23
|
+
|
|
24
|
+
Or install it yourself as:
|
|
25
|
+
|
|
26
|
+
$ gem install logger
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
### Simple Example
|
|
31
|
+
|
|
32
|
+
```ruby
|
|
33
|
+
require 'logger'
|
|
34
|
+
|
|
35
|
+
# Create a Logger that prints to STDOUT
|
|
36
|
+
log = Logger.new(STDOUT)
|
|
37
|
+
log.debug("Created Logger")
|
|
38
|
+
|
|
39
|
+
log.info("Program finished")
|
|
40
|
+
|
|
41
|
+
# Create a Logger that prints to STDERR
|
|
42
|
+
error_log = Logger.new(STDERR)
|
|
43
|
+
error_log = error_log.error("fatal error")
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Development
|
|
47
|
+
|
|
48
|
+
After checking out the repo, run the following to install dependencies.
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
$ bin/setup
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Then, run the tests as:
|
|
55
|
+
|
|
56
|
+
```
|
|
57
|
+
$ rake test
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
To install this gem onto your local machine, run
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
$ rake install
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
To release a new version, update the version number in `lib/logger/version.rb`, and then run
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
$ rake release
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
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).
|
|
73
|
+
|
|
74
|
+
## Advanced Development
|
|
75
|
+
|
|
76
|
+
### Run tests of a specific file
|
|
77
|
+
|
|
78
|
+
```
|
|
79
|
+
$ ruby test/logger/test_logger.rb
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Run tests filtering test methods by a name
|
|
83
|
+
|
|
84
|
+
`--name` option is available as:
|
|
85
|
+
|
|
86
|
+
```
|
|
87
|
+
$ ruby test/logger/test_logger.rb --name test_lshift
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Publish documents to GitHub Pages
|
|
91
|
+
|
|
92
|
+
```
|
|
93
|
+
$ rake gh-pages
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Then, git commit and push the generated HTMLs onto `gh-pages` branch.
|
|
97
|
+
|
|
98
|
+
## Contributing
|
|
99
|
+
|
|
100
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/logger.
|
|
101
|
+
|
|
102
|
+
## License
|
|
103
|
+
|
|
104
|
+
The gem is available as open source under the terms of the [BSD-2-Clause](BSDL).
|
data/lib/logger/log_device.rb
CHANGED
|
@@ -11,41 +11,27 @@ class Logger
|
|
|
11
11
|
attr_reader :filename
|
|
12
12
|
include MonitorMixin
|
|
13
13
|
|
|
14
|
-
def initialize(
|
|
14
|
+
def initialize(
|
|
15
|
+
log = nil, shift_age: nil, shift_size: nil, shift_period_suffix: nil,
|
|
16
|
+
binmode: false, reraise_write_errors: [], skip_header: false
|
|
17
|
+
)
|
|
15
18
|
@dev = @filename = @shift_age = @shift_size = @shift_period_suffix = nil
|
|
16
19
|
@binmode = binmode
|
|
20
|
+
@reraise_write_errors = reraise_write_errors
|
|
21
|
+
@skip_header = skip_header
|
|
17
22
|
mon_initialize
|
|
18
23
|
set_dev(log)
|
|
19
|
-
if @filename
|
|
20
|
-
@shift_age = shift_age || 7
|
|
21
|
-
@shift_size = shift_size || 1048576
|
|
22
|
-
@shift_period_suffix = shift_period_suffix || '%Y%m%d'
|
|
23
|
-
|
|
24
|
-
unless @shift_age.is_a?(Integer)
|
|
25
|
-
base_time = @dev.respond_to?(:stat) ? @dev.stat.mtime : Time.now
|
|
26
|
-
@next_rotate_time = next_rotate_time(base_time, @shift_age)
|
|
27
|
-
end
|
|
28
|
-
end
|
|
24
|
+
set_file(shift_age, shift_size, shift_period_suffix) if @filename
|
|
29
25
|
end
|
|
30
26
|
|
|
31
27
|
def write(message)
|
|
32
|
-
|
|
28
|
+
handle_write_errors("writing") do
|
|
33
29
|
synchronize do
|
|
34
30
|
if @shift_age and @dev.respond_to?(:stat)
|
|
35
|
-
|
|
36
|
-
check_shift_log
|
|
37
|
-
rescue
|
|
38
|
-
warn("log shifting failed. #{$!}")
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
begin
|
|
42
|
-
@dev.write(message)
|
|
43
|
-
rescue
|
|
44
|
-
warn("log writing failed. #{$!}")
|
|
31
|
+
handle_write_errors("shifting") {check_shift_log}
|
|
45
32
|
end
|
|
33
|
+
handle_write_errors("writing") {@dev.write(message)}
|
|
46
34
|
end
|
|
47
|
-
rescue Exception => ignored
|
|
48
|
-
warn("log writing failed. #{ignored}")
|
|
49
35
|
end
|
|
50
36
|
end
|
|
51
37
|
|
|
@@ -59,9 +45,10 @@ class Logger
|
|
|
59
45
|
end
|
|
60
46
|
end
|
|
61
47
|
|
|
62
|
-
def reopen(log = nil)
|
|
48
|
+
def reopen(log = nil, shift_age: nil, shift_size: nil, shift_period_suffix: nil, binmode: nil)
|
|
63
49
|
# reopen the same filename if no argument, do nothing for IO
|
|
64
50
|
log ||= @filename if @filename
|
|
51
|
+
@binmode = binmode unless binmode.nil?
|
|
65
52
|
if log
|
|
66
53
|
synchronize do
|
|
67
54
|
if @filename and @dev
|
|
@@ -69,6 +56,7 @@ class Logger
|
|
|
69
56
|
@filename = nil
|
|
70
57
|
end
|
|
71
58
|
set_dev(log)
|
|
59
|
+
set_file(shift_age, shift_size, shift_period_suffix) if @filename
|
|
72
60
|
end
|
|
73
61
|
end
|
|
74
62
|
self
|
|
@@ -76,6 +64,17 @@ class Logger
|
|
|
76
64
|
|
|
77
65
|
private
|
|
78
66
|
|
|
67
|
+
# :stopdoc:
|
|
68
|
+
|
|
69
|
+
MODE = File::WRONLY | File::APPEND
|
|
70
|
+
# TruffleRuby < 24.2 does not have File::SHARE_DELETE
|
|
71
|
+
if File.const_defined? :SHARE_DELETE
|
|
72
|
+
MODE_TO_OPEN = MODE | File::SHARE_DELETE | File::BINARY
|
|
73
|
+
else
|
|
74
|
+
MODE_TO_OPEN = MODE | File::BINARY
|
|
75
|
+
end
|
|
76
|
+
MODE_TO_CREATE = MODE_TO_OPEN | File::CREAT | File::EXCL
|
|
77
|
+
|
|
79
78
|
def set_dev(log)
|
|
80
79
|
if log.respond_to?(:write) and log.respond_to?(:close)
|
|
81
80
|
@dev = log
|
|
@@ -86,34 +85,72 @@ class Logger
|
|
|
86
85
|
end
|
|
87
86
|
else
|
|
88
87
|
@dev = open_logfile(log)
|
|
89
|
-
@dev.sync = true
|
|
90
|
-
@dev.binmode if @binmode
|
|
91
88
|
@filename = log
|
|
92
89
|
end
|
|
93
90
|
end
|
|
94
91
|
|
|
92
|
+
def set_file(shift_age, shift_size, shift_period_suffix)
|
|
93
|
+
@shift_age = shift_age || @shift_age || 7
|
|
94
|
+
@shift_size = shift_size || @shift_size || 1048576
|
|
95
|
+
@shift_period_suffix = shift_period_suffix || @shift_period_suffix || '%Y%m%d'
|
|
96
|
+
|
|
97
|
+
unless @shift_age.is_a?(Integer)
|
|
98
|
+
base_time = @dev.respond_to?(:stat) ? @dev.stat.mtime : Time.now
|
|
99
|
+
@next_rotate_time = next_rotate_time(base_time, @shift_age)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
if MODE_TO_OPEN == MODE
|
|
104
|
+
def fixup_mode(dev)
|
|
105
|
+
dev
|
|
106
|
+
end
|
|
107
|
+
else
|
|
108
|
+
def fixup_mode(dev)
|
|
109
|
+
return dev if @binmode
|
|
110
|
+
dev.autoclose = false
|
|
111
|
+
old_dev = dev
|
|
112
|
+
dev = File.new(dev.fileno, mode: MODE, path: dev.path)
|
|
113
|
+
old_dev.close
|
|
114
|
+
PathAttr.set_path(dev, filename) if defined?(PathAttr)
|
|
115
|
+
dev
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
95
119
|
def open_logfile(filename)
|
|
96
120
|
begin
|
|
97
|
-
File.open(filename,
|
|
121
|
+
dev = File.open(filename, MODE_TO_OPEN)
|
|
98
122
|
rescue Errno::ENOENT
|
|
99
123
|
create_logfile(filename)
|
|
124
|
+
else
|
|
125
|
+
dev = fixup_mode(dev)
|
|
126
|
+
dev.sync = true
|
|
127
|
+
dev.binmode if @binmode
|
|
128
|
+
dev
|
|
100
129
|
end
|
|
101
130
|
end
|
|
102
131
|
|
|
103
132
|
def create_logfile(filename)
|
|
104
133
|
begin
|
|
105
|
-
logdev = File.open(filename,
|
|
134
|
+
logdev = File.open(filename, MODE_TO_CREATE)
|
|
106
135
|
logdev.flock(File::LOCK_EX)
|
|
136
|
+
logdev = fixup_mode(logdev)
|
|
107
137
|
logdev.sync = true
|
|
108
138
|
logdev.binmode if @binmode
|
|
109
|
-
add_log_header(logdev)
|
|
139
|
+
add_log_header(logdev) unless @skip_header
|
|
110
140
|
logdev.flock(File::LOCK_UN)
|
|
141
|
+
logdev
|
|
111
142
|
rescue Errno::EEXIST
|
|
112
143
|
# file is created by another process
|
|
113
|
-
|
|
114
|
-
logdev.sync = true
|
|
144
|
+
open_logfile(filename)
|
|
115
145
|
end
|
|
116
|
-
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def handle_write_errors(mesg)
|
|
149
|
+
yield
|
|
150
|
+
rescue *@reraise_write_errors
|
|
151
|
+
raise
|
|
152
|
+
rescue
|
|
153
|
+
warn("log #{mesg} failed. #{$!}")
|
|
117
154
|
end
|
|
118
155
|
|
|
119
156
|
def add_log_header(file)
|
|
@@ -137,40 +174,34 @@ class Logger
|
|
|
137
174
|
end
|
|
138
175
|
end
|
|
139
176
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
begin
|
|
149
|
-
File.open(@filename, File::WRONLY | File::APPEND) do |lock|
|
|
150
|
-
lock.flock(File::LOCK_EX) # inter-process locking. will be unlocked at closing file
|
|
151
|
-
if File.identical?(@filename, lock) and File.identical?(lock, @dev)
|
|
152
|
-
yield # log shifting
|
|
153
|
-
else
|
|
154
|
-
# log shifted by another process (i-node before locking and i-node after locking are different)
|
|
155
|
-
@dev.close rescue nil
|
|
156
|
-
@dev = open_logfile(@filename)
|
|
157
|
-
@dev.sync = true
|
|
158
|
-
end
|
|
159
|
-
end
|
|
160
|
-
rescue Errno::ENOENT
|
|
161
|
-
# @filename file would not exist right after #rename and before #create_logfile
|
|
162
|
-
if retry_limit <= 0
|
|
163
|
-
warn("log rotation inter-process lock failed. #{$!}")
|
|
177
|
+
def lock_shift_log
|
|
178
|
+
retry_limit = 8
|
|
179
|
+
retry_sleep = 0.1
|
|
180
|
+
begin
|
|
181
|
+
File.open(@filename, MODE_TO_OPEN) do |lock|
|
|
182
|
+
lock.flock(File::LOCK_EX) # inter-process locking. will be unlocked at closing file
|
|
183
|
+
if File.identical?(@filename, lock) and File.identical?(lock, @dev)
|
|
184
|
+
yield # log shifting
|
|
164
185
|
else
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
retry
|
|
186
|
+
# log shifted by another process (i-node before locking and i-node after locking are different)
|
|
187
|
+
@dev.close rescue nil
|
|
188
|
+
@dev = open_logfile(@filename)
|
|
169
189
|
end
|
|
170
190
|
end
|
|
171
|
-
|
|
172
|
-
|
|
191
|
+
true
|
|
192
|
+
rescue Errno::ENOENT
|
|
193
|
+
# @filename file would not exist right after #rename and before #create_logfile
|
|
194
|
+
if retry_limit <= 0
|
|
195
|
+
warn("log rotation inter-process lock failed. #{$!}")
|
|
196
|
+
else
|
|
197
|
+
sleep retry_sleep
|
|
198
|
+
retry_limit -= 1
|
|
199
|
+
retry_sleep *= 2
|
|
200
|
+
retry
|
|
201
|
+
end
|
|
173
202
|
end
|
|
203
|
+
rescue
|
|
204
|
+
warn("log rotation inter-process lock failed. #{$!}")
|
|
174
205
|
end
|
|
175
206
|
|
|
176
207
|
def shift_log_age
|
|
@@ -179,10 +210,7 @@ class Logger
|
|
|
179
210
|
File.rename("#{@filename}.#{i}", "#{@filename}.#{i+1}")
|
|
180
211
|
end
|
|
181
212
|
end
|
|
182
|
-
@
|
|
183
|
-
File.rename("#{@filename}", "#{@filename}.0")
|
|
184
|
-
@dev = create_logfile(@filename)
|
|
185
|
-
return true
|
|
213
|
+
shift_log_file("#{@filename}.0")
|
|
186
214
|
end
|
|
187
215
|
|
|
188
216
|
def shift_log_period(period_end)
|
|
@@ -198,10 +226,40 @@ class Logger
|
|
|
198
226
|
break unless FileTest.exist?(age_file)
|
|
199
227
|
end
|
|
200
228
|
end
|
|
229
|
+
shift_log_file(age_file)
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def shift_log_file(shifted)
|
|
233
|
+
stat = @dev.stat
|
|
201
234
|
@dev.close rescue nil
|
|
202
|
-
File.rename(
|
|
235
|
+
File.rename(@filename, shifted)
|
|
203
236
|
@dev = create_logfile(@filename)
|
|
237
|
+
mode, uid, gid = stat.mode, stat.uid, stat.gid
|
|
238
|
+
begin
|
|
239
|
+
@dev.chmod(mode) if mode
|
|
240
|
+
mode = nil
|
|
241
|
+
@dev.chown(uid, gid)
|
|
242
|
+
rescue Errno::EPERM
|
|
243
|
+
if mode
|
|
244
|
+
# failed to chmod, probably nothing can do more.
|
|
245
|
+
elsif uid
|
|
246
|
+
uid = nil
|
|
247
|
+
retry # to change gid only
|
|
248
|
+
end
|
|
249
|
+
end
|
|
204
250
|
return true
|
|
205
251
|
end
|
|
206
252
|
end
|
|
207
253
|
end
|
|
254
|
+
|
|
255
|
+
File.open(__FILE__) do |f|
|
|
256
|
+
File.new(f.fileno, autoclose: false, path: "").path
|
|
257
|
+
rescue IOError
|
|
258
|
+
module PathAttr # :nodoc:
|
|
259
|
+
attr_reader :path
|
|
260
|
+
|
|
261
|
+
def self.set_path(file, path)
|
|
262
|
+
file.extend(self).instance_variable_set(:@path, path)
|
|
263
|
+
end
|
|
264
|
+
end
|
|
265
|
+
end
|
data/lib/logger/period.rb
CHANGED
|
@@ -8,14 +8,14 @@ class Logger
|
|
|
8
8
|
|
|
9
9
|
def next_rotate_time(now, shift_age)
|
|
10
10
|
case shift_age
|
|
11
|
-
when 'daily'
|
|
11
|
+
when 'daily', :daily
|
|
12
12
|
t = Time.mktime(now.year, now.month, now.mday) + SiD
|
|
13
|
-
when 'weekly'
|
|
13
|
+
when 'weekly', :weekly
|
|
14
14
|
t = Time.mktime(now.year, now.month, now.mday) + SiD * (7 - now.wday)
|
|
15
|
-
when 'monthly'
|
|
15
|
+
when 'monthly', :monthly
|
|
16
16
|
t = Time.mktime(now.year, now.month, 1) + SiD * 32
|
|
17
17
|
return Time.mktime(t.year, t.month, 1)
|
|
18
|
-
when 'now', 'everytime'
|
|
18
|
+
when 'now', 'everytime', :now, :everytime
|
|
19
19
|
return now
|
|
20
20
|
else
|
|
21
21
|
raise ArgumentError, "invalid :shift_age #{shift_age.inspect}, should be daily, weekly, monthly, or everytime"
|
|
@@ -30,13 +30,13 @@ class Logger
|
|
|
30
30
|
|
|
31
31
|
def previous_period_end(now, shift_age)
|
|
32
32
|
case shift_age
|
|
33
|
-
when 'daily'
|
|
33
|
+
when 'daily', :daily
|
|
34
34
|
t = Time.mktime(now.year, now.month, now.mday) - SiD / 2
|
|
35
|
-
when 'weekly'
|
|
35
|
+
when 'weekly', :weekly
|
|
36
36
|
t = Time.mktime(now.year, now.month, now.mday) - (SiD * now.wday + SiD / 2)
|
|
37
|
-
when 'monthly'
|
|
37
|
+
when 'monthly', :monthly
|
|
38
38
|
t = Time.mktime(now.year, now.month, 1) - SiD / 2
|
|
39
|
-
when 'now', 'everytime'
|
|
39
|
+
when 'now', 'everytime', :now, :everytime
|
|
40
40
|
return now
|
|
41
41
|
else
|
|
42
42
|
raise ArgumentError, "invalid :shift_age #{shift_age.inspect}, should be daily, weekly, monthly, or everytime"
|
data/lib/logger/severity.rb
CHANGED
|
@@ -15,5 +15,24 @@ class Logger
|
|
|
15
15
|
FATAL = 4
|
|
16
16
|
# An unknown message that should always be logged.
|
|
17
17
|
UNKNOWN = 5
|
|
18
|
+
|
|
19
|
+
LEVELS = {
|
|
20
|
+
"debug" => DEBUG,
|
|
21
|
+
"info" => INFO,
|
|
22
|
+
"warn" => WARN,
|
|
23
|
+
"error" => ERROR,
|
|
24
|
+
"fatal" => FATAL,
|
|
25
|
+
"unknown" => UNKNOWN,
|
|
26
|
+
}
|
|
27
|
+
private_constant :LEVELS
|
|
28
|
+
|
|
29
|
+
def self.coerce(severity)
|
|
30
|
+
if severity.is_a?(Integer)
|
|
31
|
+
severity
|
|
32
|
+
else
|
|
33
|
+
key = severity.to_s.downcase
|
|
34
|
+
LEVELS[key] || raise(ArgumentError, "invalid log level: #{severity}")
|
|
35
|
+
end
|
|
36
|
+
end
|
|
18
37
|
end
|
|
19
38
|
end
|
data/lib/logger/version.rb
CHANGED
data/lib/logger.rb
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
#
|
|
11
11
|
# A simple system for logging messages. See Logger for more documentation.
|
|
12
12
|
|
|
13
|
+
require 'fiber'
|
|
13
14
|
require 'monitor'
|
|
14
15
|
require 'rbconfig'
|
|
15
16
|
|
|
@@ -264,8 +265,7 @@ require_relative 'logger/errors'
|
|
|
264
265
|
# logger.error! # => 3
|
|
265
266
|
# logger.fatal! # => 4
|
|
266
267
|
#
|
|
267
|
-
# You can retrieve the log level with method
|
|
268
|
-
# {level}[Logger.html#attribute-i-level]:
|
|
268
|
+
# You can retrieve the log level with method #level.
|
|
269
269
|
#
|
|
270
270
|
# logger.level = Logger::ERROR
|
|
271
271
|
# logger.level # => 3
|
|
@@ -380,7 +380,9 @@ class Logger
|
|
|
380
380
|
include Severity
|
|
381
381
|
|
|
382
382
|
# Logging severity threshold (e.g. <tt>Logger::INFO</tt>).
|
|
383
|
-
|
|
383
|
+
def level
|
|
384
|
+
level_override[level_key] || @level
|
|
385
|
+
end
|
|
384
386
|
|
|
385
387
|
# Sets the log level; returns +severity+.
|
|
386
388
|
# See {Log Level}[rdoc-ref:Logger@Log+Level].
|
|
@@ -395,24 +397,23 @@ class Logger
|
|
|
395
397
|
# Logger#sev_threshold= is an alias for Logger#level=.
|
|
396
398
|
#
|
|
397
399
|
def level=(severity)
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
@level = UNKNOWN
|
|
400
|
+
@level = Severity.coerce(severity)
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
# Adjust the log level during the block execution for the current Fiber only
|
|
404
|
+
#
|
|
405
|
+
# logger.with_level(:debug) do
|
|
406
|
+
# logger.debug { "Hello" }
|
|
407
|
+
# end
|
|
408
|
+
def with_level(severity)
|
|
409
|
+
prev, level_override[level_key] = level, Severity.coerce(severity)
|
|
410
|
+
begin
|
|
411
|
+
yield
|
|
412
|
+
ensure
|
|
413
|
+
if prev
|
|
414
|
+
level_override[level_key] = prev
|
|
414
415
|
else
|
|
415
|
-
|
|
416
|
+
level_override.delete(level_key)
|
|
416
417
|
end
|
|
417
418
|
end
|
|
418
419
|
end
|
|
@@ -542,10 +543,20 @@ class Logger
|
|
|
542
543
|
# - A string filepath: entries are to be written
|
|
543
544
|
# to the file at that path; if the file at that path exists,
|
|
544
545
|
# new entries are appended.
|
|
545
|
-
# - An IO stream (typically
|
|
546
|
-
# entries are to be written to the given stream.
|
|
546
|
+
# - An IO stream (typically <tt>$stdout</tt>, <tt>$stderr</tt>. or
|
|
547
|
+
# an open file): entries are to be written to the given stream.
|
|
547
548
|
# - +nil+ or +File::NULL+: no entries are to be written.
|
|
548
549
|
#
|
|
550
|
+
# Argument +shift_age+ must be one of:
|
|
551
|
+
#
|
|
552
|
+
# - The number of log files to be in the rotation.
|
|
553
|
+
# See {Size-Based Rotation}[rdoc-ref:Logger@Size-Based+Rotation].
|
|
554
|
+
# - A string period indicator.
|
|
555
|
+
# See {Periodic Rotation}[rdoc-ref:Logger@Periodic+Rotation].
|
|
556
|
+
#
|
|
557
|
+
# Argument +shift_size+ is the maximum size (in bytes) of each log file.
|
|
558
|
+
# See {Size-Based Rotation}[rdoc-ref:Logger@Size-Based+Rotation].
|
|
559
|
+
#
|
|
549
560
|
# Examples:
|
|
550
561
|
#
|
|
551
562
|
# Logger.new('t.log')
|
|
@@ -565,29 +576,43 @@ class Logger
|
|
|
565
576
|
#
|
|
566
577
|
# - +formatter+: sets the entry formatter; default is +nil+.
|
|
567
578
|
# See {formatter=}[Logger.html#attribute-i-formatter].
|
|
579
|
+
#
|
|
568
580
|
# - +datetime_format+: sets the format for entry timestamp;
|
|
569
581
|
# default is +nil+.
|
|
570
582
|
# See #datetime_format=.
|
|
583
|
+
#
|
|
571
584
|
# - +binmode+: sets whether the logger writes in binary mode;
|
|
572
585
|
# default is +false+.
|
|
586
|
+
#
|
|
573
587
|
# - +shift_period_suffix+: sets the format for the filename suffix
|
|
574
588
|
# for periodic log file rotation; default is <tt>'%Y%m%d'</tt>.
|
|
575
589
|
# See {Periodic Rotation}[rdoc-ref:Logger@Periodic+Rotation].
|
|
576
590
|
#
|
|
591
|
+
# - +reraise_write_errors+: An array of exception classes, which will
|
|
592
|
+
# be reraised if there is an error when writing to the log device.
|
|
593
|
+
# The default is to swallow all exceptions raised.
|
|
594
|
+
# - +skip_header+: If +true+, prevents the logger from writing a header
|
|
595
|
+
# when creating a new log file. The default is +false+, meaning
|
|
596
|
+
# the header will be written as usual.
|
|
597
|
+
#
|
|
577
598
|
def initialize(logdev, shift_age = 0, shift_size = 1048576, level: DEBUG,
|
|
578
599
|
progname: nil, formatter: nil, datetime_format: nil,
|
|
579
|
-
binmode: false, shift_period_suffix: '%Y%m%d'
|
|
600
|
+
binmode: false, shift_period_suffix: '%Y%m%d',
|
|
601
|
+
reraise_write_errors: [], skip_header: false)
|
|
580
602
|
self.level = level
|
|
581
603
|
self.progname = progname
|
|
582
604
|
@default_formatter = Formatter.new
|
|
583
605
|
self.datetime_format = datetime_format
|
|
584
606
|
self.formatter = formatter
|
|
585
607
|
@logdev = nil
|
|
608
|
+
@level_override = {}
|
|
586
609
|
if logdev && logdev != File::NULL
|
|
587
610
|
@logdev = LogDevice.new(logdev, shift_age: shift_age,
|
|
588
611
|
shift_size: shift_size,
|
|
589
612
|
shift_period_suffix: shift_period_suffix,
|
|
590
|
-
binmode: binmode
|
|
613
|
+
binmode: binmode,
|
|
614
|
+
reraise_write_errors: reraise_write_errors,
|
|
615
|
+
skip_header: skip_header)
|
|
591
616
|
end
|
|
592
617
|
end
|
|
593
618
|
|
|
@@ -614,8 +639,9 @@ class Logger
|
|
|
614
639
|
# # "E, [2022-05-12T14:21:27.596726 #22428] ERROR -- : one\n",
|
|
615
640
|
# # "E, [2022-05-12T14:23:05.847241 #22428] ERROR -- : three\n"]
|
|
616
641
|
#
|
|
617
|
-
def reopen(logdev = nil)
|
|
618
|
-
@logdev&.reopen(logdev
|
|
642
|
+
def reopen(logdev = nil, shift_age = nil, shift_size = nil, shift_period_suffix: nil, binmode: nil)
|
|
643
|
+
@logdev&.reopen(logdev, shift_age: shift_age, shift_size: shift_size,
|
|
644
|
+
shift_period_suffix: shift_period_suffix, binmode: binmode)
|
|
619
645
|
self
|
|
620
646
|
end
|
|
621
647
|
|
|
@@ -739,6 +765,24 @@ private
|
|
|
739
765
|
SEV_LABEL[severity] || 'ANY'
|
|
740
766
|
end
|
|
741
767
|
|
|
768
|
+
# Guarantee the existence of this ivar even when subclasses don't call the superclass constructor.
|
|
769
|
+
def level_override
|
|
770
|
+
unless defined?(@level_override)
|
|
771
|
+
bad = self.class.instance_method(:initialize)
|
|
772
|
+
file, line = bad.source_location
|
|
773
|
+
Kernel.warn <<~";;;", uplevel: 2
|
|
774
|
+
Logger not initialized properly
|
|
775
|
+
#{file}:#{line}: info: #{bad.owner}\##{bad.name}: \
|
|
776
|
+
does not call super probably
|
|
777
|
+
;;;
|
|
778
|
+
end
|
|
779
|
+
@level_override ||= {}
|
|
780
|
+
end
|
|
781
|
+
|
|
782
|
+
def level_key
|
|
783
|
+
Fiber.current
|
|
784
|
+
end
|
|
785
|
+
|
|
742
786
|
def format_message(severity, datetime, progname, msg)
|
|
743
787
|
(@formatter || @default_formatter).call(severity, datetime, progname, msg)
|
|
744
788
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: logger
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.7.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Naotoshi Seo
|
|
@@ -9,50 +9,8 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date:
|
|
13
|
-
dependencies:
|
|
14
|
-
- !ruby/object:Gem::Dependency
|
|
15
|
-
name: bundler
|
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
|
17
|
-
requirements:
|
|
18
|
-
- - ">="
|
|
19
|
-
- !ruby/object:Gem::Version
|
|
20
|
-
version: '0'
|
|
21
|
-
type: :development
|
|
22
|
-
prerelease: false
|
|
23
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
24
|
-
requirements:
|
|
25
|
-
- - ">="
|
|
26
|
-
- !ruby/object:Gem::Version
|
|
27
|
-
version: '0'
|
|
28
|
-
- !ruby/object:Gem::Dependency
|
|
29
|
-
name: rake
|
|
30
|
-
requirement: !ruby/object:Gem::Requirement
|
|
31
|
-
requirements:
|
|
32
|
-
- - ">="
|
|
33
|
-
- !ruby/object:Gem::Version
|
|
34
|
-
version: 12.3.3
|
|
35
|
-
type: :development
|
|
36
|
-
prerelease: false
|
|
37
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
38
|
-
requirements:
|
|
39
|
-
- - ">="
|
|
40
|
-
- !ruby/object:Gem::Version
|
|
41
|
-
version: 12.3.3
|
|
42
|
-
- !ruby/object:Gem::Dependency
|
|
43
|
-
name: test-unit
|
|
44
|
-
requirement: !ruby/object:Gem::Requirement
|
|
45
|
-
requirements:
|
|
46
|
-
- - ">="
|
|
47
|
-
- !ruby/object:Gem::Version
|
|
48
|
-
version: '0'
|
|
49
|
-
type: :development
|
|
50
|
-
prerelease: false
|
|
51
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
52
|
-
requirements:
|
|
53
|
-
- - ">="
|
|
54
|
-
- !ruby/object:Gem::Version
|
|
55
|
-
version: '0'
|
|
12
|
+
date: 2025-03-27 00:00:00.000000000 Z
|
|
13
|
+
dependencies: []
|
|
56
14
|
description: Provides a simple logging utility for outputting messages.
|
|
57
15
|
email:
|
|
58
16
|
- sonots@gmail.com
|
|
@@ -61,6 +19,11 @@ executables: []
|
|
|
61
19
|
extensions: []
|
|
62
20
|
extra_rdoc_files: []
|
|
63
21
|
files:
|
|
22
|
+
- ".document"
|
|
23
|
+
- ".rdoc_options"
|
|
24
|
+
- BSDL
|
|
25
|
+
- COPYING
|
|
26
|
+
- README.md
|
|
64
27
|
- lib/logger.rb
|
|
65
28
|
- lib/logger/errors.rb
|
|
66
29
|
- lib/logger/formatter.rb
|
|
@@ -68,12 +31,12 @@ files:
|
|
|
68
31
|
- lib/logger/period.rb
|
|
69
32
|
- lib/logger/severity.rb
|
|
70
33
|
- lib/logger/version.rb
|
|
71
|
-
- logger.gemspec
|
|
72
34
|
homepage: https://github.com/ruby/logger
|
|
73
35
|
licenses:
|
|
74
36
|
- Ruby
|
|
75
37
|
- BSD-2-Clause
|
|
76
|
-
metadata:
|
|
38
|
+
metadata:
|
|
39
|
+
changelog_uri: https://github.com/ruby/logger/releases
|
|
77
40
|
post_install_message:
|
|
78
41
|
rdoc_options: []
|
|
79
42
|
require_paths:
|
|
@@ -82,14 +45,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
82
45
|
requirements:
|
|
83
46
|
- - ">="
|
|
84
47
|
- !ruby/object:Gem::Version
|
|
85
|
-
version: 2.
|
|
48
|
+
version: 2.5.0
|
|
86
49
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
87
50
|
requirements:
|
|
88
51
|
- - ">="
|
|
89
52
|
- !ruby/object:Gem::Version
|
|
90
53
|
version: '0'
|
|
91
54
|
requirements: []
|
|
92
|
-
rubygems_version: 3.
|
|
55
|
+
rubygems_version: 3.5.11
|
|
93
56
|
signing_key:
|
|
94
57
|
specification_version: 4
|
|
95
58
|
summary: Provides a simple logging utility for outputting messages.
|
data/logger.gemspec
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
begin
|
|
2
|
-
require_relative "lib/logger/version"
|
|
3
|
-
rescue LoadError # Fallback to load version file in ruby core repository
|
|
4
|
-
require_relative "version"
|
|
5
|
-
end
|
|
6
|
-
|
|
7
|
-
Gem::Specification.new do |spec|
|
|
8
|
-
spec.name = "logger"
|
|
9
|
-
spec.version = Logger::VERSION
|
|
10
|
-
spec.authors = ["Naotoshi Seo", "SHIBATA Hiroshi"]
|
|
11
|
-
spec.email = ["sonots@gmail.com", "hsbt@ruby-lang.org"]
|
|
12
|
-
|
|
13
|
-
spec.summary = %q{Provides a simple logging utility for outputting messages.}
|
|
14
|
-
spec.description = %q{Provides a simple logging utility for outputting messages.}
|
|
15
|
-
spec.homepage = "https://github.com/ruby/logger"
|
|
16
|
-
spec.licenses = ["Ruby", "BSD-2-Clause"]
|
|
17
|
-
|
|
18
|
-
spec.files = Dir.glob("lib/**/*.rb") + ["logger.gemspec"]
|
|
19
|
-
spec.require_paths = ["lib"]
|
|
20
|
-
|
|
21
|
-
spec.required_ruby_version = ">= 2.3.0"
|
|
22
|
-
|
|
23
|
-
spec.add_development_dependency "bundler", ">= 0"
|
|
24
|
-
spec.add_development_dependency "rake", ">= 12.3.3"
|
|
25
|
-
spec.add_development_dependency "test-unit"
|
|
26
|
-
end
|