Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
87 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,53 @@ | ||
# :stopdoc: | ||
# | ||
# It's useful to define custom log levels that denote success, or otherwise meaningful events that happen to not be negative (more than 50% of the levels | ||
# are given to warn, error, fail - quite a pessimistic view of one's application's | ||
# chances of success, no? ;-) ) | ||
# It's useful to define custom log levels that denote success, or otherwise | ||
# meaningful events that happen to not be negative (more than 50% of the | ||
# levels are given to warn, error, fail - quite a pessimistic view of one's | ||
# application's chances of success, no? ;-) ) | ||
# | ||
# Here, we define two new levels, 'happy' and 'success' and make them soothing colours. | ||
|
||
require 'logging' | ||
|
||
# https://github.com/TwP/logging/blob/master/lib/logging.rb#L250-285 | ||
# The levels run from lowest level to highest level. | ||
# I'm still pessimistic of my programs enough so that my success levels are lower than my failure levels... | ||
Logging.init :debug, :info, :happy, :warn, :success, :error, :fatal | ||
|
||
Logging.color_scheme( 'soothing_ish', | ||
:levels => { | ||
:info => :cyan, | ||
:happy => :green, | ||
:warn => :yellow, | ||
:success => [:blue], | ||
:error => :red, | ||
:fatal => [:white, :on_red] | ||
}, | ||
:date => :cyan, | ||
:logger => :cyan, | ||
:message => :orange | ||
) | ||
|
||
Logging.appenders.stdout( | ||
'stdout', | ||
:layout => Logging.layouts.pattern( | ||
:pattern => '[%d] %-5l %c: %m\n', | ||
:color_scheme => 'soothing_ish' | ||
# Here, we define two new levels, 'happy' and 'success' and make them soothing | ||
# colours. | ||
# | ||
|
||
require 'logging' | ||
|
||
# https://github.com/TwP/logging/blob/master/lib/logging.rb#L250-285 | ||
# The levels run from lowest level to highest level. | ||
|
||
Logging.init :debug, :info, :happy, :warn, :success, :error, :fatal | ||
|
||
Logging.color_scheme( 'soothing_ish', | ||
:levels => { | ||
:info => :cyan, | ||
:happy => :green, | ||
:warn => :yellow, | ||
:success => [:blue], | ||
:error => :red, | ||
:fatal => [:white, :on_red] | ||
}, | ||
:date => :cyan, | ||
:logger => :cyan, | ||
:message => :orange | ||
) | ||
) | ||
|
||
log = Logging.logger['Soothing::Colors'] | ||
log.add_appenders 'stdout' | ||
log.level = :debug | ||
|
||
log.debug "a very nice little debug message" | ||
log.info "things are operating nominally" | ||
log.happy "What a beautiful day" | ||
log.warn "this is your last warning" | ||
log.success "I am INWEENCIBLE!!" | ||
log.error StandardError.new("something went horribly wrong") | ||
log.fatal "I Die!" | ||
|
||
Logging.appenders.stdout( | ||
'stdout', | ||
:layout => Logging.layouts.pattern( | ||
:pattern => '[%d] %-7l %c: %m\n', | ||
:color_scheme => 'soothing_ish' | ||
) | ||
) | ||
|
||
log = Logging.logger['Soothing::Colors'] | ||
log.add_appenders 'stdout' | ||
log.level = :debug | ||
|
||
log.debug 'a very nice little debug message' | ||
log.info 'things are operating nominally' | ||
log.happy 'What a beautiful day' | ||
log.warn 'this is your last warning' | ||
log.success 'I am INWEENCIBLE!!' | ||
log.error StandardError.new('something went horribly wrong') | ||
log.fatal 'I Die!' | ||
|
||
# :startdoc: |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# :stopdoc: | ||
# | ||
# One useful feature of log messages in your code is that they provide a | ||
# convenient instrumentation point for testing. Through log messages you can | ||
# confirm that internal methods were called or that certain code paths were | ||
# executed. This example demonstrates how to capture log output during testing | ||
# for later analysis. | ||
# | ||
# The Logging framework provides an RSpec helper that will direct log output | ||
# to a StringIO appender. Log lines can be read from this IO destination | ||
# during tests. | ||
# | ||
|
||
require 'rspec' | ||
require 'logging' | ||
require 'rspec/logging_helper' | ||
|
||
# Configure RSpec to capture log messages for each test. The output from the | ||
# logs will be stored in the @log_output variable. It is a StringIO instance. | ||
RSpec.configure do |config| | ||
include RSpec::LoggingHelper | ||
config.capture_log_messages | ||
end | ||
|
||
# Now within your specs you can check that various log events were generated. | ||
describe 'SuperLogger' do | ||
it 'should be able to read a log message' do | ||
logger = Logging.logger['SuperLogger'] | ||
|
||
logger.debug 'foo bar' | ||
logger.warn 'just a little warning' | ||
|
||
@log_output.readline.should be == 'DEBUG SuperLogger: foo bar' | ||
@log_output.readline.should be == 'WARN SuperLogger: just a little warning' | ||
end | ||
end | ||
|
||
# :startdoc: |
This file was deleted.
Oops, something went wrong.