1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
## Motivation
When Ruby decided to reverse the order of exception backtraces, I finally gave up using the built in logging and decided restore sanity to the output of my programs once and for all\!
## Installation
Add this line to your application's Gemfile:
``` ruby
gem 'console'
```
And then execute:
$ bundle
## Usage
As your code executes, it generates interesting events which you want to know about. The general approach is to use an `Console::Logger` which outputs text to the terminal. The text is generated by inspecting the console that occurred.
Capturing structured information allows it to be used in different ways. These events can be sent to a logger or some other system (e.g. web browser, syslog) and analysed in more detail.
### Default Logger
Generally speaking, use `Console.logger` which is suitable for logging to the user's terminal.
### Environment Variables
#### `CONSOLE_LEVEL=debug`
Control the default logger level. You can set it to any of the supported log levels: `debug`, `info`, `warn`, `error`, `fatal`.
#### `CONSOLE_DEBUG=MyClass,MyModule::MyClass`
Enable debug logging for the specified class names. You can specify one or more class names which will be resolved at runtime.
### Module Integration
``` ruby
require 'console'
# Set the log level:
Console.logger.debug!
module MyModule
extend Console
def self.test_logger
logger.debug "GOTO LINE 1."
logger.info "5 things your doctor won't tell you!"
logger.warn "Something didn't work as expected!"
logger.error "The matrix has two cats!"
end
test_logger
end
```
### Class Integration
``` ruby
require 'console'
# Set the log level:
Console.logger.debug!
class MyObject
include Console
def test_logger
logger.debug "GOTO LINE 1."
logger.info "5 things your doctor won't tell you!"
logger.warn "Something didn't work as expected!"
logger.error "The matrix has two cats!"
end
end
MyObject.new.test_logger
```
### Subject Logging
The first argument to the log method is the subject.
``` ruby
class Thing
def call
Console.logger.info(self) {"Something is going on"}
end
end
```
Using this approach, you can turn on and off specific subjects by using the class name:
``` ruby
$ CONSOLE_DEBUG=Thing ./script.rb
```
This will conditionally enable all log statements which have a subject of class `Thing`.
### Console Formatting
Console classes are used to wrap data which can generate structured log messages:
``` ruby
require 'console'
class MyConsole < Console::Generic
def format(output, terminal, verbose)
output.puts "My console text!"
end
end
Console.logger.info("My Console", MyConsole.new)
```
#### Failure Events
`Console::Event::Failure` represents an exception and will log the message and backtrace recursively.
#### Spawn Events
`Console::Event::Spawn` represents the execution of a command, and will log the environment, arguments and options used to execute it.
### Custom Log Levels
`Console::Filter` implements support for multiple log levels.
``` ruby
require 'console'
MyLogger = Console::Filter[noise: 0, stuff: 1, broken: 2]
# verbose: true - log severity/name/pid etc.
logger = MyLogger.new(Console.logger, name: "Java", verbose: true)
logger.broken("It's so janky.")
```
### Multiple Outputs
Use `Console::Split` to log to multiple destinations.
``` ruby
require 'console/terminal'
require 'console/serialized/logger'
require 'console/logger'
require 'console/split'
terminal = Console::Terminal::Logger.new
file = Console::Serialized::Logger.new(File.open("log.json", "a"))
logger = Console::Logger.new(Console::Split[terminal, file])
logger.info "I can go everywhere!"
```
### Custom Logger Output
`Console::Logger` provides a default interface which is a drop in replacemnet for `Logger` and other similar implementations. The only method required for output is `#call(*arguments, **options, &block)`.
``` ruby
require 'console/logger'
output = proc do |*arguments, **options, &block|
puts "arguments: #{arguments.inspect} options: #{options.inspect} block: #{block.call}"
end
logger = Console::Logger.new(output)
logger.info("Hello World!", meta: "data") {"block"}
# => arguments: ["Hello World!"] options: {:severity=>:info, :meta=>"data"} block: block
```
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
|