console 1.30.2 → 1.31.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4a3a941ea83d20a38ee9478245b0a7c7a0845b7aac0cc64311f081bef9fbc809
4
- data.tar.gz: d2619ea91b4599616c5129c1880468e5d28250fc4756445b778a65b114a27b6d
3
+ metadata.gz: 356c92cbef3eb3269b7dada97f281c832304e0cad9885102e63c7d6725196df2
4
+ data.tar.gz: 1f957d68282bea484c6abddd507dcb886930c727d148f9dfa4f02f482724c234
5
5
  SHA512:
6
- metadata.gz: dc3f9bee5ec9205e1a144606188da0629688a53ec59a82d5ed90987f78dffdb833d2ccee63cbf091c8e4c06d3d1ea535b1ebfc6c35314a624d7f01232704fbce
7
- data.tar.gz: 0e65d355fceba6fc1ae152d5aa0a71e314fc21c5d558c9e23ee211fd3f62c764b9060d2d7f0b1fc90b3856a41a2f2d211b3531e90aa9e91d49df27cdbdc7ad9c
6
+ metadata.gz: c7400992949682404ecd931375c098c103105c5d513897c933f56be31ac7b8d96e81ea12eb85ad012b91358ee14309684f3321a7704dd25717f581c04cb51fdc
7
+ data.tar.gz: 02a3052cb5169303820e55bbe7c13bc6d9564ffd1df810b8a0329814fd2b84a0a1da3b12c008569cb4133cdd2e7e63d08153ec24ee72f786487c049f60432893
checksums.yaml.gz.sig CHANGED
Binary file
@@ -30,12 +30,15 @@ module Console
30
30
  # Load the default configuration.
31
31
  # @returns [Config] The default configuration.
32
32
  def self.default
33
- @default ||= self.load(PATH)
33
+ @default ||= self.load(PATH).freeze
34
34
  end
35
35
 
36
36
  # Set the default log level based on `$DEBUG` and `$VERBOSE`.
37
37
  # You can also specify CONSOLE_LEVEL=debug or CONSOLE_LEVEL=info in environment.
38
38
  # https://mislav.net/2011/06/ruby-verbose-mode/ has more details about how it all fits together.
39
+ #
40
+ # @parameter env [Hash] The environment to read the log level from.
41
+ # @returns [Integer | Symbol] The default log level.
39
42
  def log_level(env = ENV)
40
43
  Logger.default_log_level(env)
41
44
  end
@@ -7,6 +7,13 @@ module Console
7
7
  module Event
8
8
  # A generic event which can be used to represent structured data.
9
9
  class Generic
10
+ # Convert the event to a hash suitable for JSON serialization.
11
+ #
12
+ # @returns [Hash] The hash representation of the event.
13
+ def to_hash
14
+ {}
15
+ end
16
+
10
17
  # Convert the event to a hash suitable for JSON serialization.
11
18
  #
12
19
  # @returns [Hash] The hash representation of the event.
@@ -70,10 +70,16 @@ module Console
70
70
  # @parameter verbose [Boolean] Enable verbose output.
71
71
  # @parameter level [Integer] The log level.
72
72
  # @parameter options [Hash] Additional options.
73
- def initialize(output, verbose: true, level: self.class::DEFAULT_LEVEL, **options)
73
+ def initialize(output, verbose: true, level: nil, **options)
74
74
  @output = output
75
75
  @verbose = verbose
76
- @level = level
76
+
77
+ # Set the log level using the behaviour implemented in `level=`:
78
+ if level
79
+ self.level = level
80
+ else
81
+ @level = self.class::DEFAULT_LEVEL
82
+ end
77
83
 
78
84
  @subjects = {}
79
85
 
@@ -156,7 +162,7 @@ module Console
156
162
  #
157
163
  # You can enable and disable logging for classes. This function checks if logging for a given subject is enabled.
158
164
  #
159
- # @parameter subject [Module] The subject to check.
165
+ # @parameter subject [Module | Object] The subject to check.
160
166
  # @parameter level [Integer] The log level.
161
167
  # @returns [Boolean] Whether logging is enabled.
162
168
  def enabled?(subject, level = self.class::MINIMUM_LEVEL)
@@ -14,12 +14,13 @@ module Console
14
14
  # Create a new output format based on the given stream.
15
15
  #
16
16
  # @parameter io [IO] The output stream.
17
+ # @parameter env [Hash] Environment variables (defaults to ENV for testing).
17
18
  # @parameter options [Hash] Additional options to customize the output.
18
19
  # @returns [Console::Output::Terminal | Console::Output::Serialized] The output instance, depending on whether the `io` is a terminal or not.
19
- def self.new(stream, **options)
20
+ def self.new(stream, env: ENV, **options)
20
21
  stream ||= $stderr
21
22
 
22
- if stream.tty?
23
+ if stream.tty? || mail?(env)
23
24
  output = Terminal.new(stream, **options)
24
25
  else
25
26
  output = Serialized.new(stream, **options)
@@ -27,6 +28,17 @@ module Console
27
28
 
28
29
  return output
29
30
  end
31
+
32
+ private
33
+
34
+ # Detect if we're running in a cron job or mail context where human-readable output is preferred.
35
+ # Cron jobs often have MAILTO set and lack TERM, or have minimal TERM values.
36
+ def self.mail?(env = ENV)
37
+ # Check for common cron environment indicators
38
+ return true if env.key?("MAILTO") && !env["MAILTO"].empty?
39
+
40
+ false
41
+ end
30
42
  end
31
43
  end
32
44
  end
@@ -78,15 +78,19 @@ module Console
78
78
  end
79
79
  end
80
80
 
81
- # Write the given arguments to the output stream using the given style. The reset sequence is automatically appended.
81
+ # Write the given arguments to the output stream using the given style. The reset sequence is automatically
82
+ # appended at the end of each line.
82
83
  #
83
84
  # @parameter arguments [Array] The arguments to write, each on a new line.
84
85
  # @parameter style [Symbol] The style to apply.
85
86
  def puts(*arguments, style: nil)
86
87
  if style and prefix = self[style]
87
- @stream.write(prefix)
88
- @stream.puts(*arguments)
89
- @stream.write(self.reset)
88
+ arguments.each do |argument|
89
+ argument.to_s.lines.each do |line|
90
+ @stream.write(prefix, line.chomp)
91
+ @stream.puts(self.reset)
92
+ end
93
+ end
90
94
  else
91
95
  @stream.puts(*arguments)
92
96
  end
@@ -4,5 +4,5 @@
4
4
  # Copyright, 2019-2025, by Samuel Williams.
5
5
 
6
6
  module Console
7
- VERSION = "1.30.2"
7
+ VERSION = "1.31.0"
8
8
  end
data/readme.md CHANGED
@@ -34,6 +34,12 @@ Please see the [project documentation](https://socketry.github.io/console/) for
34
34
 
35
35
  Please see the [project releases](https://socketry.github.io/console/releases/index) for all releases.
36
36
 
37
+ ### v1.31.0
38
+
39
+ - [Ractor compatibility.](https://socketry.github.io/console/releases/index#ractor-compatibility.)
40
+ - [Symbol log level compatibility.](https://socketry.github.io/console/releases/index#symbol-log-level-compatibility.)
41
+ - [Improved output format selection for cron jobs and email contexts.](https://socketry.github.io/console/releases/index#improved-output-format-selection-for-cron-jobs-and-email-contexts.)
42
+
37
43
  ### v1.30.0
38
44
 
39
45
  - [Introduce `Console::Config` for fine grained configuration.](https://socketry.github.io/console/releases/index#introduce-console::config-for-fine-grained-configuration.)
data/releases.md CHANGED
@@ -1,5 +1,60 @@
1
1
  # Releases
2
2
 
3
+ ## v1.31.0
4
+
5
+ ### Ractor compatibility.
6
+
7
+ The console library now works correctly with Ruby's Ractor concurrency model. Previously, attempting to use console logging within Ractors would fail with errors about non-shareable objects. This has been fixed by ensuring the default configuration is properly frozen.
8
+
9
+ ``` ruby
10
+ # This now works without errors:
11
+ ractor = Ractor.new do
12
+ require 'console'
13
+ Console.info('Hello from Ractor!')
14
+ 'Ractor completed successfully'
15
+ end
16
+
17
+ result = ractor.take
18
+ puts result # => 'Ractor completed successfully'
19
+ ```
20
+
21
+ The fix is minimal and maintains full backward compatibility while enabling safe parallel logging across multiple Ractors.
22
+
23
+ ### Symbol log level compatibility.
24
+
25
+ Previously, returning symbols from custom `log_level` methods in configuration files would cause runtime errors like "comparison of Integer with :debug failed". This has been fixed to properly convert symbols to their corresponding integer values.
26
+
27
+ ``` ruby
28
+ # config/console.rb - This now works correctly:
29
+ def log_level(env = ENV)
30
+ :debug # Automatically converted to Console::Logger::LEVELS[:debug]
31
+ end
32
+ ```
33
+
34
+ While this fix maintains backward compatibility, the recommended approach is still to use integer values directly:
35
+
36
+ ``` ruby
37
+ # config/console.rb - Recommended approach:
38
+ def log_level(env = ENV)
39
+ Console::Logger::LEVELS[:debug] # Returns 0
40
+ end
41
+ ```
42
+
43
+ ### Improved output format selection for cron jobs and email contexts.
44
+
45
+ When `MAILTO` environment variable is set (typically in cron jobs), the console library now prefers human-readable terminal output instead of JSON serialized output, even when the output stream is not a TTY. This ensures that cron job output sent via email is formatted in a readable way for administrators.
46
+
47
+ ``` ruby
48
+ # Previously in cron jobs (non-TTY), this would output JSON:
49
+ # {"time":"2025-06-07T10:30:00Z","severity":"info","subject":"CronJob","message":["Task completed"]}
50
+
51
+ # Now with MAILTO set, it outputs human-readable format:
52
+ # 0.1s info: CronJob
53
+ # | Task completed
54
+ ```
55
+
56
+ This change is conservative and only affects environments where `MAILTO` is explicitly set, ensuring compatibility with existing deployments.
57
+
3
58
  ## v1.30.0
4
59
 
5
60
  ### Introduce `Console::Config` for fine grained configuration.
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: console
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.30.2
4
+ version: 1.31.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -47,7 +47,7 @@ cert_chain:
47
47
  Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
48
48
  voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
49
49
  -----END CERTIFICATE-----
50
- date: 2025-03-10 00:00:00.000000000 Z
50
+ date: 1980-01-02 00:00:00.000000000 Z
51
51
  dependencies:
52
52
  - !ruby/object:Gem::Dependency
53
53
  name: fiber-annotation
@@ -146,14 +146,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
146
146
  requirements:
147
147
  - - ">="
148
148
  - !ruby/object:Gem::Version
149
- version: '3.1'
149
+ version: '3.2'
150
150
  required_rubygems_version: !ruby/object:Gem::Requirement
151
151
  requirements:
152
152
  - - ">="
153
153
  - !ruby/object:Gem::Version
154
154
  version: '0'
155
155
  requirements: []
156
- rubygems_version: 3.6.2
156
+ rubygems_version: 3.6.7
157
157
  specification_version: 4
158
158
  summary: Beautiful logging for Ruby.
159
159
  test_files: []
metadata.gz.sig CHANGED
Binary file