console 1.9.1 → 1.10.0

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: ebadd29a9248f8a155c3bc22550a6433e53af806acae2112fe96a3924c92fb8d
4
- data.tar.gz: fd23c8cb36efc9a36f688c2caa0383feb85fe7fdab2992d269239650faa8f92a
3
+ metadata.gz: 640269ce4b1afa8413ce60f657ab3c3ed01a036a32e554c8643aa599a0052422
4
+ data.tar.gz: b5fa60cc24de752c16af2160856fe0cfe3c038f24a409e9d3069d701afbfcb9b
5
5
  SHA512:
6
- metadata.gz: 02b4cecf9844a7d6bb733da31953805d89590b5a43b68e7d6cc379bde7e2660e3064438385d40b147cde4c89b50b7f7f9f4cf3f58aa69e1498e0085f56015a86
7
- data.tar.gz: 3bba83e7c801994f6fdb6a1a248fbac746a8a9987f786d30df73c91ba7d79032226d4f78d3eed9fb1a5d0eb9c339f89864c15fcceb823a2f877e81fe90c977f7
6
+ metadata.gz: 78b6e1c53280b26a315a422a418f36d228a583f095b41a406a33c67808ac2c299ab33399641880d5d3b7753d0ad88262fc536deac6ddd1ef6c8edf970b7d591e
7
+ data.tar.gz: d24238b81168471f70c4404badf944b24f01a92101b841513cb0ced7c6998b4a63efabb88bad49c042e72c174b1c56f2cd51cf385f7814d41d709cde040e1ab3
@@ -21,82 +21,22 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  require_relative 'console/logger'
24
- require_relative 'console/resolver'
25
- require_relative 'console/terminal/logger'
26
24
 
27
25
  module Console
28
- class << self
29
- attr_accessor :logger
30
-
31
- # Set the default log level based on `$DEBUG` and `$VERBOSE`.
32
- # You can also specify CONSOLE_LEVEL=debug or CONSOLE_LEVEL=info in environment.
33
- # https://mislav.net/2011/06/ruby-verbose-mode/ has more details about how it all fits together.
34
- def default_log_level(env = ENV)
35
- if level = (env['CONSOLE_LEVEL'] || env['CONSOLE_LOG_LEVEL'])
36
- Logger::LEVELS[level.to_sym] || Logger.warn
37
- elsif $DEBUG
38
- Logger::DEBUG
39
- elsif $VERBOSE.nil?
40
- Logger::WARN
41
- else
42
- Logger::INFO
43
- end
44
- end
45
-
46
- # You can change the log level for different classes using CONSOLE_<LEVEL> env vars.
47
- #
48
- # e.g. `CONSOLE_WARN=Acorn,Banana CONSOLE_DEBUG=Cat` will set the log level for Acorn and Banana to warn and Cat to
49
- # debug. This overrides the default log level.
50
- #
51
- # @param logger [Logger] A logger instance to set the logging levels on.
52
- # @param env [Hash] Environment to read levels from.
53
- #
54
- # @return [nil] if there were no custom logging levels specified in the environment.
55
- # @return [Resolver] if there were custom logging levels, then the created resolver is returned.
56
- def default_resolver(logger, env = ENV)
57
- # find all CONSOLE_<LEVEL> variables from environment
58
- levels = Logger::LEVELS
59
- .map { |label, level| [level, env["CONSOLE_#{label.to_s.upcase}"]&.split(',')] }
60
- .to_h
61
- .compact
62
-
63
- # if we have any levels, then create a class resolver, and each time a class is resolved, set the log level for
64
- # that class to the specified level
65
- if levels.any?
66
- resolver = Resolver.new
67
- levels.each do |level, names|
68
- resolver.bind(names) do |klass|
69
- logger.enable(klass, level)
70
- end
71
- end
72
- return resolver
73
- end
74
- end
75
-
76
- # Controls verbose output using `$VERBOSE`.
77
- def verbose?
78
- !$VERBOSE.nil?
79
- end
80
-
81
- def build(output, verbose: self.verbose?, level: self.default_log_level)
82
- terminal = Terminal::Logger.new(output, verbose: verbose)
83
-
84
- logger = Logger.new(terminal, verbose: verbose, level: level)
85
-
86
- return logger
87
- end
26
+ def self.logger
27
+ Logger.instance
88
28
  end
89
29
 
90
- # Create the logger instance:
91
- @logger = self.build($stderr)
92
- @resolver = self.default_resolver(@logger)
30
+ def self.logger= instance
31
+ Logger.instance= instance
32
+ end
93
33
 
94
34
  def logger= logger
95
35
  @logger = logger
96
36
  end
97
37
 
98
38
  def logger
99
- @logger || Console.logger
39
+ @logger || Logger.instance
100
40
  end
101
41
 
102
42
  def self.extended(klass)
@@ -21,8 +21,48 @@
21
21
  require_relative 'filter'
22
22
  require_relative 'progress'
23
23
 
24
+ require_relative 'resolver'
25
+ require_relative 'terminal/logger'
26
+
27
+ require 'fiber/local'
28
+
24
29
  module Console
25
30
  class Logger < Filter[debug: 0, info: 1, warn: 2, error: 3, fatal: 4]
31
+ extend Fiber::Local
32
+
33
+ # Set the default log level based on `$DEBUG` and `$VERBOSE`.
34
+ # You can also specify CONSOLE_LEVEL=debug or CONSOLE_LEVEL=info in environment.
35
+ # https://mislav.net/2011/06/ruby-verbose-mode/ has more details about how it all fits together.
36
+ def self.default_log_level(env = ENV)
37
+ if level = (env['CONSOLE_LEVEL'] || env['CONSOLE_LOG_LEVEL'])
38
+ LEVELS[level.to_sym] || level.to_i
39
+ elsif $DEBUG
40
+ DEBUG
41
+ elsif $VERBOSE.nil?
42
+ WARN
43
+ else
44
+ INFO
45
+ end
46
+ end
47
+
48
+ # Controls verbose output using `$VERBOSE`.
49
+ def self.verbose?(env = ENV)
50
+ !$VERBOSE.nil? || env['CONSOLE_VERBOSE']
51
+ end
52
+
53
+ def self.default_logger(output, verbose: self.verbose?, level: self.default_log_level)
54
+ terminal = Terminal::Logger.new(output, verbose: verbose)
55
+
56
+ logger = self.new(terminal, verbose: verbose, level: level)
57
+ resolver = Resolver.default_resolver(logger)
58
+
59
+ return logger
60
+ end
61
+
62
+ def self.local
63
+ self.default_logger($stderr)
64
+ end
65
+
26
66
  DEFAULT_LEVEL = 1
27
67
 
28
68
  def initialize(output, **options)
@@ -22,6 +22,36 @@ require_relative 'filter'
22
22
 
23
23
  module Console
24
24
  class Resolver
25
+ # You can change the log level for different classes using CONSOLE_<LEVEL> env vars.
26
+ #
27
+ # e.g. `CONSOLE_WARN=Acorn,Banana CONSOLE_DEBUG=Cat` will set the log level for the classes Acorn and Banana to `warn` and Cat to `debug`. This overrides the default log level.
28
+ #
29
+ # @parameter logger [Logger] A logger instance to set the logging levels on.
30
+ # @parameter env [Hash] The environment to read levels from.
31
+ #
32
+ # @returns [Nil] If there were no custom logging levels specified in the environment.
33
+ # @returns [Resolver] If there were custom logging levels, then the created resolver is returned.
34
+ def self.default_resolver(logger, env = ENV)
35
+ # Find all CONSOLE_<LEVEL> variables from environment:
36
+ levels = Logger::LEVELS
37
+ .map{|label, level| [level, env["CONSOLE_#{label.upcase}"]&.split(',')]}
38
+ .to_h
39
+ .compact
40
+
41
+ # If we have any levels, then create a class resolver, and each time a class is resolved, set the log level for that class to the specified level:
42
+ if levels.any?
43
+ resolver = Resolver.new
44
+
45
+ levels.each do |level, names|
46
+ resolver.bind(names) do |klass|
47
+ logger.enable(klass, level)
48
+ end
49
+ end
50
+
51
+ return resolver
52
+ end
53
+ end
54
+
25
55
  def initialize
26
56
  @names = {}
27
57
 
@@ -19,5 +19,5 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  module Console
22
- VERSION = "1.9.1"
22
+ VERSION = "1.10.0"
23
23
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: console
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.1
4
+ version: 1.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-28 00:00:00.000000000 Z
11
+ date: 2020-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fiber-local
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bake
15
29
  requirement: !ruby/object:Gem::Requirement