logger 1.5.3 → 1.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 299d68259bad0228e1e6519f73ddbef94902a8e04f230ad8bbc38f1cb7c45dee
4
- data.tar.gz: 9178b4b52d2e025fefb9dcadebd94ff6a95392d5d381b949f8d2cb454d456bab
3
+ metadata.gz: c6fa7c02974cf7c275bfa19e6225f8b06bdbc76d49f78e6bf75e351dfb14c1e3
4
+ data.tar.gz: d299b1c26a5389316c89a7899359f5d8d1570dae24428bce6c43d9832adc7dc2
5
5
  SHA512:
6
- metadata.gz: 1ee506486c7af0e574c4c4c58b28ca10c2d0d4a0493d0b405e43af96e93bb0c397dee7b128d0d89e7793724b6f0cb186c8e2d33aa2ff8cfc6e847e830e6c0127
7
- data.tar.gz: 70c4a17f55e4eb5d2323ef5218151de9638a70b8690f6a40180b051554a17bd58b4a09521bbc8ca6a55b60afc9215d51b1c37873c0883e5ba3f42d89e4965a36
6
+ metadata.gz: 9b19a5196c89b0c184c5c2ab4a77d5085d26ec6f04c272b7bbd0085beaeaf7d758e9cb0c728c486e07f4498209e70327420ff66f218b4f6baec7d59fe7ec0c44
7
+ data.tar.gz: 1fc54467c631ea277b044c9d2b48895f3d2ed684eec2725f3aa1e475c551e1cfb6420fe9540a5eddf0e597794f74ee8c669c506faad44af5ddb8075cb7c8826e
@@ -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
@@ -34,16 +35,22 @@ class Logger
34
35
  if @shift_age and @dev.respond_to?(:stat)
35
36
  begin
36
37
  check_shift_log
38
+ rescue *@reraise_write_errors
39
+ raise
37
40
  rescue
38
41
  warn("log shifting failed. #{$!}")
39
42
  end
40
43
  end
41
44
  begin
42
45
  @dev.write(message)
46
+ rescue *@reraise_write_errors
47
+ raise
43
48
  rescue
44
49
  warn("log writing failed. #{$!}")
45
50
  end
46
51
  end
52
+ rescue *@reraise_write_errors
53
+ raise
47
54
  rescue Exception => ignored
48
55
  warn("log writing failed. #{ignored}")
49
56
  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.1"
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[Fiber.current] || @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[Fiber.current] = level, Severity.coerce(severity)
410
+ begin
411
+ yield
412
+ ensure
413
+ if prev
414
+ level_override[Fiber.current] = prev
414
415
  else
415
- raise ArgumentError, "invalid log level: #{severity}"
416
+ level_override.delete(Fiber.current)
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,11 @@ 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
+
742
754
  def format_message(severity, datetime, progname, msg)
743
755
  (@formatter || @default_formatter).call(severity, datetime, progname, msg)
744
756
  end
data/logger.gemspec CHANGED
@@ -18,9 +18,5 @@ Gem::Specification.new do |spec|
18
18
  spec.files = Dir.glob("lib/**/*.rb") + ["logger.gemspec"]
19
19
  spec.require_paths = ["lib"]
20
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"
21
+ spec.required_ruby_version = ">= 2.5.0"
26
22
  end
metadata CHANGED
@@ -1,58 +1,15 @@
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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naotoshi Seo
8
8
  - SHIBATA Hiroshi
9
- autorequire:
10
9
  bindir: bin
11
10
  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'
11
+ date: 2024-09-02 00:00:00.000000000 Z
12
+ dependencies: []
56
13
  description: Provides a simple logging utility for outputting messages.
57
14
  email:
58
15
  - sonots@gmail.com
@@ -74,7 +31,6 @@ licenses:
74
31
  - Ruby
75
32
  - BSD-2-Clause
76
33
  metadata: {}
77
- post_install_message:
78
34
  rdoc_options: []
79
35
  require_paths:
80
36
  - lib
@@ -82,15 +38,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
82
38
  requirements:
83
39
  - - ">="
84
40
  - !ruby/object:Gem::Version
85
- version: 2.3.0
41
+ version: 2.5.0
86
42
  required_rubygems_version: !ruby/object:Gem::Requirement
87
43
  requirements:
88
44
  - - ">="
89
45
  - !ruby/object:Gem::Version
90
46
  version: '0'
91
47
  requirements: []
92
- rubygems_version: 3.3.26
93
- signing_key:
48
+ rubygems_version: 3.6.0.dev
94
49
  specification_version: 4
95
50
  summary: Provides a simple logging utility for outputting messages.
96
51
  test_files: []