console 1.35.0 → 1.35.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: '0498b53c4327442b097e48fac79fdb92b4bcb12df58c80aa2a28490ff5d36738'
4
- data.tar.gz: 7960eed0fca16ba4ce614837ac861925565053cb6694d0a6107d27f89ce9042d
3
+ metadata.gz: e161d51958a96742a44541c862e32c9354c04d3cdae04cfe57ef0a63b2ecfd36
4
+ data.tar.gz: 48afe3e18acbc12cd72b96c22a139fd9de69e852c0ba8e98a4367811ca1e916e
5
5
  SHA512:
6
- metadata.gz: d1248d88a76092de60f134b6deea1cb066edf2abb6d3bb040c8eada3801338e4e5f2139ffec9ca9a12a29a1d4e13a4b3083760979a9b16f1a498742b1d255c70
7
- data.tar.gz: a8bf531686c47214706d623d9169b6718c6fc72f7e7517dffe7d048fcac86db6f0f4aa7f3d97cb9053287f906b401c113bea601b796e94b9ea5b9007e8e287f8
6
+ metadata.gz: 2caf9a3c2520460b7a54489bf4ed68a9f0f37223a45917500884b8f5a1c531ebb602a1dda943994925c716d9af785c11c07f2263704572db3ed343f9858c75a6
7
+ data.tar.gz: aaf33117df4b08e942b8975128bb11a11b653580c85dfe93218d5a4af0a16db69385d4907a37d461a3d3e89c6ef6578ecef4aaca3ec07e3d1d63c56c66111283
checksums.yaml.gz.sig CHANGED
Binary file
@@ -0,0 +1,47 @@
1
+ # Command Line
2
+
3
+ This guide explains how the `console` gem can be controlled using environment variables.
4
+
5
+ ## Environment Variables
6
+
7
+ The following program, `app.rb` will be used to explain the different environmet variables:
8
+
9
+ ~~~ ruby
10
+ #!/usr/bin/env ruby
11
+
12
+ require 'console'
13
+
14
+ class MyApplication
15
+ def do_work
16
+ Console.logger.debug(self, "Doing some work.")
17
+ end
18
+ end
19
+
20
+ Console.logger.debug(self, "Creating the application.")
21
+ application = MyApplication.new
22
+ application.do_work
23
+ ~~~
24
+
25
+ ### `CONSOLE_LEVEL=debug`
26
+
27
+ Control the default logger level. You can set it to any of the supported log levels: `debug`, `info`, `warn`, `error`, `fatal`.
28
+
29
+ By default, the debug level is not enabled, but you can enable it using `CONSOLE_LEVEL=debug`:
30
+
31
+ <pre>&gt; <font color="#00AFFF">CONSOLE_LEVEL</font><font color="#00A6B2">=</font><font color="#00AFFF">debug</font> <font color="#005FD7">./app.rb</font>
32
+ <font color="#00AAAA"> 0.0s debug:</font> <b>Object</b> <font color="#717171">[oid=0x3c] [ec=0x50] [pid=990900] [2022-10-12 17:28:15 +1300]</font>
33
+ | Creating the application.
34
+ <font color="#00AAAA"> 0.0s debug:</font> <b>MyApplication</b> <font color="#717171">[oid=0x64] [ec=0x50] [pid=990900] [2022-10-12 17:28:15 +1300]</font>
35
+ | Doing some work.
36
+ </pre>
37
+
38
+ ### `CONSOLE_DEBUG=MyClass,MyModule::MyClass`
39
+
40
+ Enable debug logging for the specified class names. You can specify one or more class names which will be resolved at runtime. This is specifically related to subject logging.
41
+
42
+ By default, the debug level is not enabled, but you can enable it for the specific `MyApplication` class using `CONSOLE_DEBUG=MyApplication`:
43
+
44
+ <pre>&gt; <font color="#00AFFF">CONSOLE_DEBUG</font><font color="#00A6B2">=</font><font color="#00AFFF">MyApplication</font> <font color="#005FD7">./app.rb</font>
45
+ <font color="#00AAAA"> 0.0s debug:</font> <b>MyApplication</b> <font color="#717171">[oid=0x64] [ec=0x78] [pid=991855] [2022-10-12 17:30:56 +1300]</font>
46
+ | Doing some work.
47
+ </pre>
@@ -0,0 +1,23 @@
1
+ # Configuration
2
+
3
+ This guide explains how to implement per-project configuration for the `console` gem.
4
+
5
+ ## Configuration File
6
+
7
+ The `console` gem can load a configuration file, by default `config/console.rb`. This file is evaluated in an instance of {ruby Console::Config} which allows you to override methods that implement the default behaviour for a given project.
8
+
9
+ Here is an example configuration file:
10
+
11
+ ```ruby
12
+ # config/console.rb
13
+
14
+ # Override the default log level
15
+ def log_level
16
+ :debug
17
+ end
18
+
19
+ # Override the default output
20
+ def make_output
21
+ Console::Output::Datadog.new(Console::Output::Default.new)
22
+ end
23
+ ```
data/context/events.md ADDED
@@ -0,0 +1,71 @@
1
+ # Events
2
+
3
+ This guide explains how to log structured events with a well-defined schema.
4
+
5
+ ## Overview
6
+
7
+ Logs often fall into two categories:
8
+
9
+ - Free-form logs which include some structured data, but are primarily text-based and used for debugging or troubleshooting.
10
+ - Structured logs which are designed to be machine-readable and can be used for monitoring, alerting, auditing and analytics.
11
+
12
+ Events are a type of structured log which are designed to be machine-readable and have a well-defined schema. They are used to represent a specific occurrence within a system, such as a request, a response, an error, or a warning. You can create custom events to represent any structured data you like.
13
+
14
+ ## Core Concepts
15
+
16
+ - {ruby Console::Event::Generic} is the base class for all events.
17
+ - {ruby Console::Terminal::Formatter} includes a collection of formatters for rendering specific events in a human-readable format.
18
+
19
+ ## Emitting Events
20
+
21
+ To emit an event, you can create an instance of a specific event class and call the `#emit` method. For example, to emit a failure event:
22
+
23
+ ```ruby
24
+ def handle_request
25
+ begin
26
+ # ... user code ...
27
+ rescue => error
28
+ Console::Event::Failure.for(error).emit(self, "Failed to handle request!")
29
+ end
30
+ end
31
+ ```
32
+
33
+ This will emit a failure event with the error message and backtrace.
34
+
35
+ ### Emitting Events with Different Severity Levels
36
+
37
+ Events can have different severity levels, such as `:info`, `:warn`, `:error`, and `:fatal`. You can specify the severity level when emitting an event:
38
+
39
+ ```ruby
40
+ Console::Event::Failure.for(error).emit(self, "Failed to handle request!", severity: :debug)
41
+ ```
42
+
43
+ ## Custom Events
44
+
45
+ You can create custom events by subclassing {ruby Console::Event::Generic} and defining the schema for the event. For example, to create a custom event for tracking user logins:
46
+
47
+ ```ruby
48
+ class UserLoginEvent < Console::Event::Generic
49
+ def self.for(request, user)
50
+ self.new(user.id, request.ip)
51
+ end
52
+
53
+ def new(id, ip)
54
+ @id = id
55
+ @ip = ip
56
+ end
57
+
58
+ def to_hash
59
+ {
60
+ # Specifying a type field is recommended:
61
+ type: :login,
62
+
63
+ # Custom fields:
64
+ id: @id,
65
+ ip: @ip
66
+ }
67
+ end
68
+ end
69
+
70
+ UserLoginEvent.for(request, user).emit(self, "User logged in.")
71
+ ```
@@ -0,0 +1,170 @@
1
+ # Getting Started
2
+
3
+ This guide explains how to use `console` for logging.
4
+
5
+ ## Installation
6
+
7
+ Add the gem to your project:
8
+
9
+ ~~~ bash
10
+ $ bundle add console
11
+ ~~~
12
+
13
+ ## Core Concepts
14
+
15
+ `console` has several core concepts:
16
+
17
+ - A log message which consists of a set of arguments and options, which includes a timestamp, severity, and other structured data.
18
+ - The {ruby Console} module which provides an abstract interface for logging.
19
+ - A {ruby Console::Logger} instance which is the main entry point for logging for a specific system and writes data to a given output formatter.
20
+ - An output instance such as {ruby Console::XTerm}, {ruby Console::Serialized::Logger} which formats these log messages and writes them to a specific output device.
21
+ - An event instance, such as {ruby Console::Event::Progress} or {ruby Console::Event::Spawn} which represents a structured event within a system, which can be formatted in a specific way.
22
+
23
+ ## Basic Logging
24
+
25
+ Out of the box, {ruby Console} provides a logger interface that outputs to the current terminal via `stderr`.
26
+
27
+ ~~~ ruby
28
+ require 'console'
29
+
30
+ Console.info("Hello World")
31
+ ~~~
32
+
33
+ <pre>
34
+ <font color="#00AA00"> 0.0s info:</font> <b>Hello World</b> <font color="#717171">[pid=219113] [2020-08-08 12:21:26 +1200]</font>
35
+ </pre>
36
+
37
+ The method name `info` indicates the severity level of the log message. You can filter out severity levels, and by default, `debug` messages are filtered out. Here are some examples of the different log levels:
38
+
39
+ ~~~ ruby
40
+ require 'console'
41
+
42
+ Console.debug("The input voltage has stabilized.")
43
+ Console.info("Making a request to the machine.")
44
+ Console.warn("The machine has detected a temperature anomaly.")
45
+ Console.error("The machine was unable to complete the request!")
46
+ Console.fatal("Depressurisation detected, evacuate the area!")
47
+ ~~~
48
+
49
+ From the terminal, you can control the log level using the `CONSOLE_LEVEL` environment variable. To log all messages including `debug`:
50
+
51
+ ~~~ bash
52
+ $ CONSOLE_LEVEL=debug ./machine
53
+ ~~~
54
+
55
+ Alternatively to restrict log messages to warnings and above:
56
+
57
+ ~~~ bash
58
+ $ CONSOLE_LEVEL=warn ./machine
59
+ ~~~
60
+
61
+ If otherwise unspecified, Ruby's standard `$DEBUG` and `$VERBOSE` global variables will be checked and adjust the log level appropriately.
62
+
63
+ ## Metadata
64
+
65
+ You can add any options you like to a log message and they will be included as part of the log output:
66
+
67
+ ~~~ ruby
68
+ duration = measure{...}
69
+ Console.info("Execution completed!", duration: duration)
70
+ ~~~
71
+
72
+ ## Subject Logging
73
+
74
+ The first argument to the log statement becomes the implicit subject of the log message.
75
+
76
+ ~~~ ruby
77
+ require 'console'
78
+
79
+ class Machine
80
+ def initialize(voltage)
81
+ @voltage = voltage.floor
82
+ Console.info(self, "The input voltage has stabilized.")
83
+ end
84
+ end
85
+
86
+ Machine.new(5.5)
87
+ ~~~
88
+
89
+ The given subject, in this case `self`, is used on the first line, along with associated metadata, while the message itself appears on subsequent lines:
90
+
91
+ <pre>
92
+ <font color="#00AA00"> 0.0s info:</font> <b>Machine</b> <font color="#717171">[oid=0x3c] [pid=219041] [2020-08-08 12:17:33 +1200]</font>
93
+ | The input voltage has stabilized.
94
+ </pre>
95
+
96
+ If you want to disable log messages which come from this particular class, you can execute your command:
97
+
98
+ ~~~ bash
99
+ $ CONSOLE_FATAL=Machine ./machine
100
+ ~~~
101
+
102
+ This will prevent any log message which has a subject of class `Machine` from logging messages of a severity less than `fatal`.
103
+
104
+ ## Exception Logging
105
+
106
+ If your code has an unhandled exception, you may wish to log it. In order to log a full backtrace, you must log the subject followed by the exception.
107
+
108
+ ~~~ ruby
109
+ require 'console'
110
+
111
+ class Cache
112
+ def initialize
113
+ @entries = {}
114
+ end
115
+
116
+ def fetch(key)
117
+ @entries.fetch(key)
118
+ rescue => error
119
+ Console.warn(self, "Cache fetch failure!", error)
120
+ end
121
+ end
122
+
123
+ Cache.new.fetch(:foo)
124
+ ~~~
125
+
126
+ This will produce the following output:
127
+
128
+ <pre><font color="#C01C28"> 0.0s error:</font> <b>Cache</b> <font color="#8B8A88">[oid=0x848] [ec=0x85c] [pid=571936] [2024-05-03 10:55:11 +1200]</font>
129
+ | Cache fetch failure!
130
+ | <font color="#C01C28"><b>KeyError</b></font>: <b>key not found: :foo (</b><u style="text-decoration-style:solid"><b>KeyError</b></u><b>)</b>
131
+ | → <font color="#C01C28">test.rb:15</font> in `fetch&apos;
132
+ | <font color="#C01C28">test.rb:15</font> in `fetch&apos;
133
+ | <font color="#C01C28">test.rb:21</font> in `&lt;top (required)&gt;&apos;
134
+ </pre>
135
+
136
+ ## Program Structure
137
+
138
+ Generally, programs should use the global `Console` interface to log messages.
139
+
140
+ Individual classes should not be catching and logging exceptions. It makes for simpler code if this is handled in a few places near the top of your program. Exceptions should collect enough information such that logging them produces a detailed backtrace leading to the failure.
141
+
142
+ ### Multiple Outputs
143
+
144
+ Use `Console::Split` to log to multiple destinations.
145
+
146
+ ``` ruby
147
+ require "console"
148
+ require "console/output/split"
149
+
150
+ terminal = Console::Output::Terminal.new($stderr)
151
+ serialized = Console::Output::Serialized.new(File.open("log.json", "a"))
152
+ Console.logger = Console::Logger.new(Console::Output::Split[terminal, serialized])
153
+
154
+ Console.info "I can go everywhere!"
155
+ ```
156
+
157
+ ### Custom Log Levels
158
+
159
+ `Console::Filter` implements support for multiple log levels.
160
+
161
+ ``` ruby
162
+ require "console"
163
+
164
+ MyLogger = Console::Filter[noise: 0, stuff: 1, broken: 2]
165
+
166
+ # verbose: true - log severity/name/pid etc.
167
+ logger = MyLogger.new(Console.logger, name: "Java", verbose: true)
168
+
169
+ logger.broken("It's so janky.")
170
+ ```
@@ -0,0 +1,28 @@
1
+ # Automatically generated context index for Utopia::Project guides.
2
+ # Do not edit then files in this directory directly, instead edit the guides and then run `bake utopia:project:agent:context:update`.
3
+ ---
4
+ description: Beautiful logging for Ruby.
5
+ metadata:
6
+ documentation_uri: https://socketry.github.io/console/
7
+ funding_uri: https://github.com/sponsors/ioquatix/
8
+ source_code_uri: https://github.com/socketry/console.git
9
+ files:
10
+ - path: getting-started.md
11
+ title: Getting Started
12
+ description: This guide explains how to use `console` for logging.
13
+ - path: command-line.md
14
+ title: Command Line
15
+ description: This guide explains how the `console` gem can be controlled using environment
16
+ variables.
17
+ - path: configuration.md
18
+ title: Configuration
19
+ description: This guide explains how to implement per-project configuration for
20
+ the `console` gem.
21
+ - path: integration.md
22
+ title: Integration
23
+ description: This guide explains how to integrate the `console` output into different
24
+ systems.
25
+ - path: events.md
26
+ title: Events
27
+ description: This guide explains how to log structured events with a well-defined
28
+ schema.
@@ -0,0 +1,37 @@
1
+ # Integration
2
+
3
+ This guide explains how to integrate the `console` output into different systems.
4
+
5
+ ## Output Redirection
6
+
7
+ The `console` is primarily an interface for logging, with a general purpose implementation which can log to a terminal (human readable) or a file (JSON by default). Output can also be augmented, redirected or manipulated by adding output wrappers. The output wrappers are generally configured using environment variables.
8
+
9
+ ### `Console::Output::Datadog`
10
+
11
+ This output wrapper augments log messages with trace metadata so that the log messages can be correlated with spans. Note that the default output is still used `Console::Output::Default`.
12
+
13
+ ~~~ bash
14
+ $ CONSOLE_OUTPUT='Console::Output::Datadog,Console::Output::Default' ./app.rb
15
+ ~~~
16
+
17
+ The `Console::Output::Datadog` is a wrapper that augments the metadata of the log message with the `trace_id` and `span_id`.
18
+
19
+ ### Custom Output Wrapper
20
+
21
+ It's straight forward to define your own output wrapper:
22
+
23
+ ~~~ ruby
24
+ # Add a happy face to all log messages.
25
+ class HappyLogger
26
+ def call(subject = nil, *arguments, **options, &block)
27
+ arguments << "😊"
28
+
29
+ @output.call(subject, *arguments, **options, &block)
30
+ end
31
+ end
32
+ ~~~
33
+
34
+ ## Adapters
35
+
36
+ - [Console::Adapter::Rails](https://github.com/socketry/console-adapter-rails)
37
+ - [Console::Adapter::Sidekiq](https://github.com/socketry/console-adapter-sidekiq)
@@ -4,5 +4,5 @@
4
4
  # Copyright, 2019-2026, by Samuel Williams.
5
5
 
6
6
  module Console
7
- VERSION = "1.35.0"
7
+ VERSION = "1.35.1"
8
8
  end
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.35.0
4
+ version: 1.35.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -98,6 +98,12 @@ extensions: []
98
98
  extra_rdoc_files: []
99
99
  files:
100
100
  - bake/console.rb
101
+ - context/command-line.md
102
+ - context/configuration.md
103
+ - context/events.md
104
+ - context/getting-started.md
105
+ - context/index.yaml
106
+ - context/integration.md
101
107
  - lib/console.rb
102
108
  - lib/console/adapter.rb
103
109
  - lib/console/capture.rb
@@ -136,11 +142,13 @@ files:
136
142
  - license.md
137
143
  - readme.md
138
144
  - releases.md
139
- homepage: https://socketry.github.io/console
145
+ homepage: https://github.com/socketry/console
140
146
  licenses:
141
147
  - MIT
142
148
  metadata:
143
149
  documentation_uri: https://socketry.github.io/console/
150
+ funding_uri: https://github.com/sponsors/ioquatix/
151
+ source_code_uri: https://github.com/socketry/console.git
144
152
  rdoc_options: []
145
153
  require_paths:
146
154
  - lib
metadata.gz.sig CHANGED
Binary file