quiet_logger 1.0.0 → 1.0.1

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: 1ae7192146de8bb704c786f4c129dc33b3eece9d1c0b6e0fdb417274c70eb25d
4
- data.tar.gz: c7c8971f79e92aede026a7e36ebfea13ff329062c85598e999017c60e5978ef2
3
+ metadata.gz: 84ec629d6e25d5aef93d890ceab63b5a387c35203709a126f240b68cdea0d779
4
+ data.tar.gz: 8c16d362965905b66dc9cfebe7b73402c325e3119ae6d3dd5c247b061841c072
5
5
  SHA512:
6
- metadata.gz: 7f5057c20db5903f1f97c620e892074ab1cfcf3dc4aa53f88b62c514a41c722593f0fd36205350c62e5e86eadac606d632629d1c62a91e297a300138e2cb8593
7
- data.tar.gz: a2e71f9d7c810866e316f93c0b0dec3f88e95fddaa78be5b80891e627eab4f0989a7908697fe8f7aee6edaca12b354e7b1dcff61b15ac33cd854ad8dfb85a048
6
+ metadata.gz: e9f56d02878ef5d4bc1d50b625e7ac61026d8a81901e63898ed7eecda4a6bf071b5bbee8690a2d205c215cdceda49b634b6c9432dbe932186b7b65943517c1e2
7
+ data.tar.gz: 3c09b747fce326ddb9040c8c4d3fc38c6c6d6688a57fa5c4d03d4d1109772ece4c2f9fdab88c28a1553f6201e5730003f4570f5fa8aee470cce04238b5ff70b4
data/CHANGELOG.md CHANGED
@@ -1,10 +1,31 @@
1
1
  # Changelog
2
+
2
3
  All notable changes to this project will be documented in this file.
3
4
 
4
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
7
 
8
+ ## 1.0.1
9
+
10
+ ### Added
11
+
12
+ - Add explicit dependency on the `logger` gem since it is no longer a default gem as of Ruby 3.5.
13
+ - Add support for the tagged logging API (`tagged`, `push_tags`, `pop_tags`, `clear_tags!`) when the wrapped logger supports it (i.e. `ActiveSupport::TaggedLogging`). Tags are stored on the wrapped logger, so they apply to messages logged with either logger. These methods are only defined when the wrapped logger implements them so that `respond_to?(:tagged)` still reflects what the wrapped logger can do.
14
+ - Add `silence` to temporarily raise the severity level for the duration of a block for compatibility with ActiveSupport loggers.
15
+ - Raise an `ArgumentError` when constructing a `QuietLogger` with an object that does not implement the `Logger` interface instead of failing later when a message is logged.
16
+
17
+ ### Changed
18
+
19
+ - `formatter` now returns the formatter of the wrapped logger, and setting it sets it on the wrapped logger, since that is the logger that formats the messages.
20
+
21
+ ### Fixed
22
+
23
+ - Fix `sev_threshold` returning the level set on the `QuietLogger` rather than the effective level, so it could report a lower level than `level` did.
24
+ - Fix `NoMethodError` when calling `datetime_format=` caused by the `Logger` initializer never being called.
25
+ - Fix `with_level` being silently ignored; the level set for the block is now honored (still limited by the level of the wrapped logger).
26
+
7
27
  ## 1.0.0
8
28
 
9
29
  ### Added
30
+
10
31
  - Add QuietLogger class to wrap other loggers but with a higher severity level to quiet their output.
data/README.md CHANGED
@@ -1,7 +1,8 @@
1
+ # QuietLogger
2
+
1
3
  [![Continuous Integration](https://github.com/bdurand/quiet_logger/actions/workflows/continuous_integration.yml/badge.svg)](https://github.com/bdurand/quiet_logger/actions/workflows/continuous_integration.yml)
2
4
  [![Ruby Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://github.com/testdouble/standard)
3
-
4
- # QuietLogger
5
+ [![Gem Version](https://badge.fury.io/rb/quiet_logger.svg)](https://badge.fury.io/rb/quiet_logger)
5
6
 
6
7
  This gem provides a mechanism for wrapping a instance of `Logger` with one that produces a less verbose output.
7
8
 
@@ -30,6 +31,19 @@ quiet_logger.warn("one more time") # This is logged since the level is warn
30
31
  even_quieter_logger = QuietLogger.new(verbose_logger, level: :error)
31
32
  ```
32
33
 
34
+ The severity level is the only thing a `QuietLogger` controls. Messages are written by the logger being wrapped, so they are sent to the same output device and rendered with the same formatter.
35
+
36
+ If the wrapped logger supports tagged logging (i.e. `ActiveSupport::TaggedLogging`), then the tagging methods are available on the `QuietLogger` as well. Tags are stored on the wrapped logger, so they apply to messages logged with either logger.
37
+
38
+ ```ruby
39
+ quiet_logger = QuietLogger.new(Rails.logger, level: :warn)
40
+
41
+ quiet_logger.tagged("ActionCable") do
42
+ quiet_logger.info("hello") # This is not logged
43
+ quiet_logger.warn("hello") # This is logged as "[ActionCable] hello"
44
+ end
45
+ ```
46
+
33
47
  ## Installation
34
48
 
35
49
  Add this line to your application's Gemfile:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.0.1
data/lib/quiet_logger.rb CHANGED
@@ -24,16 +24,102 @@ require "logger"
24
24
  # The log level on the `QuietLogger` can only be made more restrictive than the level on the
25
25
  # underlying logger. So, if the underlying logger has a level of "info", you cannot set the
26
26
  # `QuietLogger` to "debug".
27
+ #
28
+ # The severity level is the only thing a `QuietLogger` controls. Messages are written by the
29
+ # underlying logger, so the output device and the formatter used to render messages are the ones
30
+ # on that logger. Reading or setting `formatter` therefore reads or sets it on the wrapped logger.
31
+ # The progname and datetime format used for output are the wrapped logger's as well, but setting
32
+ # them on the `QuietLogger` has no effect on the output.
33
+ #
34
+ # If the wrapped logger supports tagged logging (i.e. `ActiveSupport::TaggedLogging`), then the
35
+ # tagging methods are available on the `QuietLogger` as well. Tags are stored on the wrapped
36
+ # logger, so they apply to messages logged with either logger.
37
+ #
38
+ # ```
39
+ # quiet_logger = QuietLogger.new(Rails.logger)
40
+ # quiet_logger.tagged("ActionCable") { quiet_logger.warn("Other stuff") }
41
+ # ```
27
42
  class QuietLogger < Logger
43
+ # Delegates the tagged logging API to the wrapped logger. This is only mixed into instances
44
+ # that wrap a logger which supports tagging so that `respond_to?(:tagged)` continues to
45
+ # advertise what the wrapped logger can actually do.
46
+ #
47
+ # @api private
48
+ module Tagging
49
+ # Add tags to the messages logged inside the block. Tags are stored on the wrapped logger,
50
+ # so they are applied to messages logged with either logger.
51
+ def tagged(*tags)
52
+ if block_given?
53
+ @logger.tagged(*tags) { yield self }
54
+ else
55
+ self.class.new(@logger.tagged(*tags), level: @level)
56
+ end
57
+ end
58
+
59
+ def push_tags(*tags)
60
+ @logger.push_tags(*tags)
61
+ end
62
+
63
+ def pop_tags(count = 1)
64
+ @logger.pop_tags(count)
65
+ end
66
+
67
+ def clear_tags!
68
+ @logger.clear_tags!
69
+ end
70
+ end
71
+
28
72
  # Wrap a logger to have a higher log level.
73
+ #
74
+ # @raise [ArgumentError] If the logger does not implement the `Logger` interface used to
75
+ # filter and forward messages.
29
76
  def initialize(logger, level: Logger::WARN)
77
+ unless logger.respond_to?(:add) && logger.respond_to?(:level)
78
+ raise ArgumentError, "logger must respond to :add and :level; got #{logger.inspect}"
79
+ end
80
+
81
+ # Passing a nil log device sets up the rest of the Logger internals without opening one of
82
+ # our own; messages are always written by the wrapped logger. The wrapped logger is not
83
+ # set until after this call so that the attributes the constructor initializes cannot be
84
+ # written through to it.
85
+ super(nil, level: level)
86
+
30
87
  @logger = logger
31
- self.level = level
88
+ extend(Tagging) if defined?(ActiveSupport::TaggedLogging) && logger.is_a?(ActiveSupport::TaggedLogging)
32
89
  end
33
90
 
34
91
  # @return [Integer] Returns the log level
35
92
  def level
36
- [@level, @logger.level].max
93
+ [super, @logger.level].max
94
+ end
95
+
96
+ # Logger aliases this to its own implementation of `level`, so it has to be re-aliased here
97
+ # to pick up the level from the wrapped logger.
98
+ alias_method :sev_threshold, :level
99
+
100
+ # A `QuietLogger` never formats messages itself; the wrapped logger does. The tagged logging
101
+ # API is built on top of the formatter, so this has to report the wrapped logger's formatter.
102
+ #
103
+ # @return [Logger::Formatter] The formatter of the wrapped logger.
104
+ def formatter
105
+ @logger.formatter
106
+ end
107
+
108
+ # Set the formatter on the wrapped logger since that is the logger that formats the messages.
109
+ def formatter=(value)
110
+ # The wrapped logger is not set yet when this is called from the Logger constructor.
111
+ @logger.formatter = value if @logger
112
+ end
113
+
114
+ if Logger.method_defined?(:with_level)
115
+ # Silence the logger for the duration of the block by temporarily raising the severity
116
+ # level. Provided for compatibility with ActiveSupport loggers. The level is still
117
+ # constrained by the level of the wrapped logger.
118
+ #
119
+ # Only defined if the logger gem supports `with_level` (logger >= 1.5).
120
+ def silence(severity = Logger::ERROR)
121
+ with_level(severity) { yield self }
122
+ end
37
123
  end
38
124
 
39
125
  # @api private
data/quiet_logger.gemspec CHANGED
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
26
26
 
27
27
  spec.require_paths = ["lib"]
28
28
 
29
- spec.add_development_dependency "bundler"
29
+ spec.add_dependency "logger"
30
30
 
31
- spec.required_ruby_version = ">= 2.5"
31
+ spec.required_ruby_version = ">= 2.6"
32
32
  end
metadata CHANGED
@@ -1,30 +1,28 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quiet_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Durand
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2022-05-06 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
- name: bundler
13
+ name: logger
15
14
  requirement: !ruby/object:Gem::Requirement
16
15
  requirements:
17
16
  - - ">="
18
17
  - !ruby/object:Gem::Version
19
18
  version: '0'
20
- type: :development
19
+ type: :runtime
21
20
  prerelease: false
22
21
  version_requirements: !ruby/object:Gem::Requirement
23
22
  requirements:
24
23
  - - ">="
25
24
  - !ruby/object:Gem::Version
26
25
  version: '0'
27
- description:
28
26
  email:
29
27
  - bbdurand@gmail.com
30
28
  executables: []
@@ -32,7 +30,7 @@ extensions: []
32
30
  extra_rdoc_files: []
33
31
  files:
34
32
  - CHANGELOG.md
35
- - MIT-LICENSE
33
+ - MIT-LICENSE.txt
36
34
  - README.md
37
35
  - VERSION
38
36
  - lib/quiet_logger.rb
@@ -41,7 +39,6 @@ homepage: https://github.com/bdurand/quiet_logger
41
39
  licenses:
42
40
  - MIT
43
41
  metadata: {}
44
- post_install_message:
45
42
  rdoc_options: []
46
43
  require_paths:
47
44
  - lib
@@ -49,15 +46,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
49
46
  requirements:
50
47
  - - ">="
51
48
  - !ruby/object:Gem::Version
52
- version: '2.5'
49
+ version: '2.6'
53
50
  required_rubygems_version: !ruby/object:Gem::Requirement
54
51
  requirements:
55
52
  - - ">="
56
53
  - !ruby/object:Gem::Version
57
54
  version: '0'
58
55
  requirements: []
59
- rubygems_version: 3.0.3
60
- signing_key:
56
+ rubygems_version: 4.0.3
61
57
  specification_version: 4
62
58
  summary: A logger implementation that can wrap another logger to increase the log
63
59
  level to reduce noise in the logs.
File without changes