logger 1.5.3 → 1.6.5

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: 299d68259bad0228e1e6519f73ddbef94902a8e04f230ad8bbc38f1cb7c45dee
4
- data.tar.gz: 9178b4b52d2e025fefb9dcadebd94ff6a95392d5d381b949f8d2cb454d456bab
3
+ metadata.gz: 46ba87e952de11303f4d8a1801c62441825513ed53de7ab20ec75d21b9524cdb
4
+ data.tar.gz: 985b136ab6b4027a0c28b8980374fc3beed8f3df3b31ee02fe57c0de5ab3cb56
5
5
  SHA512:
6
- metadata.gz: 1ee506486c7af0e574c4c4c58b28ca10c2d0d4a0493d0b405e43af96e93bb0c397dee7b128d0d89e7793724b6f0cb186c8e2d33aa2ff8cfc6e847e830e6c0127
7
- data.tar.gz: 70c4a17f55e4eb5d2323ef5218151de9638a70b8690f6a40180b051554a17bd58b4a09521bbc8ca6a55b60afc9215d51b1c37873c0883e5ba3f42d89e4965a36
6
+ metadata.gz: c3cb3e144eab09fddc21017774faa4b4f4260ddba3bc8d1b0244654c3c2bf13e610debfd3cef36e9bb83b31379dc5853384accadb586f3415deb090ac0fc1fdb
7
+ data.tar.gz: 82b5a284490100bee206a12e7030e3fb6287aca0ce8196e7ccc0673445b9b713df77438514b36f2e711523d5e2d1bdb897b83811ea752b3bbe26940efacee0eb
@@ -0,0 +1,6 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: 'github-actions'
4
+ directory: '/'
5
+ schedule:
6
+ interval: 'weekly'
@@ -0,0 +1,46 @@
1
+ name: Publish gem to rubygems.org
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - 'v*'
7
+
8
+ permissions:
9
+ contents: read
10
+
11
+ jobs:
12
+ push:
13
+ if: github.repository == 'ruby/logger'
14
+ runs-on: ubuntu-latest
15
+
16
+ environment:
17
+ name: rubygems.org
18
+ url: https://rubygems.org/gems/logger
19
+
20
+ permissions:
21
+ contents: write
22
+ id-token: write
23
+
24
+ steps:
25
+ - name: Harden Runner
26
+ uses: step-security/harden-runner@0080882f6c36860b6ba35c610c98ce87d4e2f26f # v2.10.2
27
+ with:
28
+ egress-policy: audit
29
+
30
+ - uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
31
+
32
+ - name: Set up Ruby
33
+ uses: ruby/setup-ruby@a6e6f86333f0a2523ece813039b8b4be04560854 # v1.190.0
34
+ with:
35
+ bundler-cache: true
36
+ ruby-version: ruby
37
+
38
+ - name: Publish to RubyGems
39
+ uses: rubygems/release-gem@a25424ba2ba8b387abc8ef40807c2c85b96cbe32 # v1.1.1
40
+
41
+ - name: Create GitHub release
42
+ run: |
43
+ tag_name="$(git describe --tags --abbrev=0)"
44
+ gh release create "${tag_name}" --verify-tag --generate-notes
45
+ env:
46
+ GITHUB_TOKEN: ${{ secrets.MATZBOT_GITHUB_WORKFLOW_TOKEN }}
@@ -0,0 +1,32 @@
1
+ name: test
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ ruby-versions:
7
+ uses: ruby/actions/.github/workflows/ruby_versions.yml@master
8
+ with:
9
+ engine: cruby
10
+ min_version: 2.5
11
+
12
+ test:
13
+ needs: ruby-versions
14
+ name: build (${{ matrix.ruby }} / ${{ matrix.os }})
15
+ strategy:
16
+ matrix:
17
+ ruby: ${{ fromJson(needs.ruby-versions.outputs.versions) }}
18
+ os: [ ubuntu-latest, macos-latest, windows-latest ]
19
+ exclude:
20
+ - ruby: 2.5
21
+ os: macos-latest
22
+ runs-on: ${{ matrix.os }}
23
+ steps:
24
+ - uses: actions/checkout@v4
25
+ - name: Set up Ruby
26
+ uses: ruby/setup-ruby@v1
27
+ with:
28
+ ruby-version: ${{ matrix.ruby }}
29
+ - name: Install dependencies
30
+ run: bundle install
31
+ - name: Run test
32
+ run: rake test
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
10
+ /html/
11
+ /vendor/
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/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem "bundler"
7
+ gem "rake"
8
+ gem "test-unit"
9
+ gem "test-unit-ruby-core"
10
+ end
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/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ begin
2
+ require "bundler/gem_tasks"
3
+ rescue LoadError
4
+ end
5
+
6
+ require "rake/testtask"
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << "test/lib"
9
+ t.ruby_opts << "-rhelper"
10
+ t.test_files = FileList["test/**/test_*.rb"]
11
+ end
12
+
13
+ require "rdoc/task"
14
+ RDoc::Task.new do |doc|
15
+ doc.main = "README.md"
16
+ doc.title = "Logger -- Ruby Standard Logger"
17
+ doc.rdoc_files = FileList.new %w[README.md lib BSDL COPYING]
18
+ doc.rdoc_dir = "html"
19
+ end
20
+
21
+ task "gh-pages" => :rdoc do
22
+ %x[git checkout gh-pages]
23
+ require "fileutils"
24
+ FileUtils.rm_rf "/tmp/html"
25
+ FileUtils.mv "html", "/tmp"
26
+ FileUtils.rm_rf "*"
27
+ FileUtils.cp_r Dir.glob("/tmp/html/*"), "."
28
+ end
29
+
30
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "logger"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -11,9 +11,10 @@ class Logger
11
11
  attr_reader :filename
12
12
  include MonitorMixin
13
13
 
14
- def initialize(log = nil, shift_age: nil, shift_size: nil, shift_period_suffix: nil, binmode: false)
14
+ def initialize(log = nil, shift_age: nil, shift_size: nil, shift_period_suffix: nil, binmode: false, reraise_write_errors: [])
15
15
  @dev = @filename = @shift_age = @shift_size = @shift_period_suffix = nil
16
16
  @binmode = binmode
17
+ @reraise_write_errors = reraise_write_errors
17
18
  mon_initialize
18
19
  set_dev(log)
19
20
  if @filename
@@ -29,23 +30,13 @@ class Logger
29
30
  end
30
31
 
31
32
  def write(message)
32
- begin
33
+ handle_write_errors("writing") do
33
34
  synchronize do
34
35
  if @shift_age and @dev.respond_to?(:stat)
35
- begin
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. #{$!}")
36
+ handle_write_errors("shifting") {check_shift_log}
45
37
  end
38
+ handle_write_errors("writing") {@dev.write(message)}
46
39
  end
47
- rescue Exception => ignored
48
- warn("log writing failed. #{ignored}")
49
40
  end
50
41
  end
51
42
 
@@ -76,6 +67,17 @@ class Logger
76
67
 
77
68
  private
78
69
 
70
+ # :stopdoc:
71
+
72
+ MODE = File::WRONLY | File::APPEND
73
+ # temporary workaround for TruffleRuby
74
+ if File.const_defined? :SHARE_DELETE
75
+ MODE_TO_OPEN = MODE | File::SHARE_DELETE | File::BINARY
76
+ else
77
+ MODE_TO_OPEN = MODE | File::BINARY
78
+ end
79
+ MODE_TO_CREATE = MODE_TO_OPEN | File::CREAT | File::EXCL
80
+
79
81
  def set_dev(log)
80
82
  if log.respond_to?(:write) and log.respond_to?(:close)
81
83
  @dev = log
@@ -86,34 +88,61 @@ class Logger
86
88
  end
87
89
  else
88
90
  @dev = open_logfile(log)
89
- @dev.sync = true
90
- @dev.binmode if @binmode
91
91
  @filename = log
92
92
  end
93
93
  end
94
94
 
95
+ if MODE_TO_OPEN == MODE
96
+ def fixup_mode(dev, filename)
97
+ dev
98
+ end
99
+ else
100
+ def fixup_mode(dev, filename)
101
+ return dev if @binmode
102
+ dev.autoclose = false
103
+ old_dev = dev
104
+ dev = File.new(dev.fileno, mode: MODE, path: filename)
105
+ old_dev.close
106
+ PathAttr.set_path(dev, filename) if defined?(PathAttr)
107
+ dev
108
+ end
109
+ end
110
+
95
111
  def open_logfile(filename)
96
112
  begin
97
- File.open(filename, (File::WRONLY | File::APPEND))
113
+ dev = File.open(filename, MODE_TO_OPEN)
98
114
  rescue Errno::ENOENT
99
115
  create_logfile(filename)
116
+ else
117
+ dev = fixup_mode(dev, filename)
118
+ dev.sync = true
119
+ dev.binmode if @binmode
120
+ dev
100
121
  end
101
122
  end
102
123
 
103
124
  def create_logfile(filename)
104
125
  begin
105
- logdev = File.open(filename, (File::WRONLY | File::APPEND | File::CREAT | File::EXCL))
126
+ logdev = File.open(filename, MODE_TO_CREATE)
106
127
  logdev.flock(File::LOCK_EX)
128
+ logdev = fixup_mode(logdev, filename)
107
129
  logdev.sync = true
108
130
  logdev.binmode if @binmode
109
131
  add_log_header(logdev)
110
132
  logdev.flock(File::LOCK_UN)
133
+ logdev
111
134
  rescue Errno::EEXIST
112
135
  # file is created by another process
113
- logdev = open_logfile(filename)
114
- logdev.sync = true
136
+ open_logfile(filename)
115
137
  end
116
- logdev
138
+ end
139
+
140
+ def handle_write_errors(mesg)
141
+ yield
142
+ rescue *@reraise_write_errors
143
+ raise
144
+ rescue
145
+ warn("log #{mesg} failed. #{$!}")
117
146
  end
118
147
 
119
148
  def add_log_header(file)
@@ -137,40 +166,33 @@ class Logger
137
166
  end
138
167
  end
139
168
 
140
- if /mswin|mingw|cygwin/ =~ RbConfig::CONFIG['host_os']
141
- def lock_shift_log
142
- yield
143
- end
144
- else
145
- def lock_shift_log
146
- retry_limit = 8
147
- retry_sleep = 0.1
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. #{$!}")
169
+ def lock_shift_log
170
+ retry_limit = 8
171
+ retry_sleep = 0.1
172
+ begin
173
+ File.open(@filename, MODE_TO_OPEN) do |lock|
174
+ lock.flock(File::LOCK_EX) # inter-process locking. will be unlocked at closing file
175
+ if File.identical?(@filename, lock) and File.identical?(lock, @dev)
176
+ yield # log shifting
164
177
  else
165
- sleep retry_sleep
166
- retry_limit -= 1
167
- retry_sleep *= 2
168
- retry
178
+ # log shifted by another process (i-node before locking and i-node after locking are different)
179
+ @dev.close rescue nil
180
+ @dev = open_logfile(@filename)
169
181
  end
170
182
  end
171
- rescue
172
- warn("log rotation inter-process lock failed. #{$!}")
183
+ rescue Errno::ENOENT
184
+ # @filename file would not exist right after #rename and before #create_logfile
185
+ if retry_limit <= 0
186
+ warn("log rotation inter-process lock failed. #{$!}")
187
+ else
188
+ sleep retry_sleep
189
+ retry_limit -= 1
190
+ retry_sleep *= 2
191
+ retry
192
+ end
173
193
  end
194
+ rescue
195
+ warn("log rotation inter-process lock failed. #{$!}")
174
196
  end
175
197
 
176
198
  def shift_log_age
@@ -205,3 +227,15 @@ class Logger
205
227
  end
206
228
  end
207
229
  end
230
+
231
+ File.open(__FILE__) do |f|
232
+ File.new(f.fileno, autoclose: false, path: "").path
233
+ rescue IOError
234
+ module PathAttr # :nodoc:
235
+ attr_reader :path
236
+
237
+ def self.set_path(file, path)
238
+ file.extend(self).instance_variable_set(:@path, path)
239
+ end
240
+ end
241
+ 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"
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Logger
4
- VERSION = "1.5.3"
4
+ VERSION = "1.6.5"
5
5
  end
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
- attr_reader :level
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
- if severity.is_a?(Integer)
399
- @level = severity
400
- else
401
- case severity.to_s.downcase
402
- when 'debug'
403
- @level = DEBUG
404
- when 'info'
405
- @level = INFO
406
- when 'warn'
407
- @level = WARN
408
- when 'error'
409
- @level = ERROR
410
- when 'fatal'
411
- @level = FATAL
412
- when 'unknown'
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
- raise ArgumentError, "invalid log level: #{severity}"
416
+ level_override.delete(level_key)
416
417
  end
417
418
  end
418
419
  end
@@ -573,21 +574,27 @@ class Logger
573
574
  # - +shift_period_suffix+: sets the format for the filename suffix
574
575
  # for periodic log file rotation; default is <tt>'%Y%m%d'</tt>.
575
576
  # See {Periodic Rotation}[rdoc-ref:Logger@Periodic+Rotation].
577
+ # - +reraise_write_errors+: An array of exception classes, which will
578
+ # be reraised if there is an error when writing to the log device.
579
+ # The default is to swallow all exceptions raised.
576
580
  #
577
581
  def initialize(logdev, shift_age = 0, shift_size = 1048576, level: DEBUG,
578
582
  progname: nil, formatter: nil, datetime_format: nil,
579
- binmode: false, shift_period_suffix: '%Y%m%d')
583
+ binmode: false, shift_period_suffix: '%Y%m%d',
584
+ reraise_write_errors: [])
580
585
  self.level = level
581
586
  self.progname = progname
582
587
  @default_formatter = Formatter.new
583
588
  self.datetime_format = datetime_format
584
589
  self.formatter = formatter
585
590
  @logdev = nil
591
+ @level_override = {}
586
592
  if logdev && logdev != File::NULL
587
593
  @logdev = LogDevice.new(logdev, shift_age: shift_age,
588
594
  shift_size: shift_size,
589
595
  shift_period_suffix: shift_period_suffix,
590
- binmode: binmode)
596
+ binmode: binmode,
597
+ reraise_write_errors: reraise_write_errors)
591
598
  end
592
599
  end
593
600
 
@@ -739,6 +746,15 @@ private
739
746
  SEV_LABEL[severity] || 'ANY'
740
747
  end
741
748
 
749
+ # Guarantee the existence of this ivar even when subclasses don't call the superclass constructor.
750
+ def level_override
751
+ @level_override ||= {}
752
+ end
753
+
754
+ def level_key
755
+ Fiber.current
756
+ end
757
+
742
758
  def format_message(severity, datetime, progname, msg)
743
759
  (@formatter || @default_formatter).call(severity, datetime, progname, msg)
744
760
  end
data/logger.gemspec CHANGED
@@ -15,12 +15,14 @@ Gem::Specification.new do |spec|
15
15
  spec.homepage = "https://github.com/ruby/logger"
16
16
  spec.licenses = ["Ruby", "BSD-2-Clause"]
17
17
 
18
- spec.files = Dir.glob("lib/**/*.rb") + ["logger.gemspec"]
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z 2>#{IO::NULL}`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
19
23
  spec.require_paths = ["lib"]
20
24
 
21
- spec.required_ruby_version = ">= 2.3.0"
25
+ spec.required_ruby_version = ">= 2.5.0"
22
26
 
23
- spec.add_development_dependency "bundler", ">= 0"
24
- spec.add_development_dependency "rake", ">= 12.3.3"
25
- spec.add_development_dependency "test-unit"
27
+ spec.metadata["changelog_uri"] = spec.homepage + "/releases"
26
28
  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.5.3
4
+ version: 1.6.5
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: 2022-12-16 00:00:00.000000000 Z
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-01-10 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,17 @@ executables: []
61
19
  extensions: []
62
20
  extra_rdoc_files: []
63
21
  files:
22
+ - ".github/dependabot.yml"
23
+ - ".github/workflows/push_gem.yml"
24
+ - ".github/workflows/test.yml"
25
+ - ".gitignore"
26
+ - BSDL
27
+ - COPYING
28
+ - Gemfile
29
+ - README.md
30
+ - Rakefile
31
+ - bin/console
32
+ - bin/setup
64
33
  - lib/logger.rb
65
34
  - lib/logger/errors.rb
66
35
  - lib/logger/formatter.rb
@@ -73,7 +42,8 @@ homepage: https://github.com/ruby/logger
73
42
  licenses:
74
43
  - Ruby
75
44
  - BSD-2-Clause
76
- metadata: {}
45
+ metadata:
46
+ changelog_uri: https://github.com/ruby/logger/releases
77
47
  post_install_message:
78
48
  rdoc_options: []
79
49
  require_paths:
@@ -82,14 +52,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
82
52
  requirements:
83
53
  - - ">="
84
54
  - !ruby/object:Gem::Version
85
- version: 2.3.0
55
+ version: 2.5.0
86
56
  required_rubygems_version: !ruby/object:Gem::Requirement
87
57
  requirements:
88
58
  - - ">="
89
59
  - !ruby/object:Gem::Version
90
60
  version: '0'
91
61
  requirements: []
92
- rubygems_version: 3.3.26
62
+ rubygems_version: 3.5.11
93
63
  signing_key:
94
64
  specification_version: 4
95
65
  summary: Provides a simple logging utility for outputting messages.