Skip to content

Commit

Permalink
Fixing up examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
TwP committed Oct 8, 2012
1 parent aa5c4f9 commit 47be512
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 91 deletions.
93 changes: 49 additions & 44 deletions examples/custom_log_levels.rb
@@ -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:
20 changes: 0 additions & 20 deletions examples/rspec_everywhere.rb

This file was deleted.

38 changes: 38 additions & 0 deletions examples/rspec_integration.rb
@@ -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:
27 changes: 0 additions & 27 deletions examples/rspec_only_some_specs.rb

This file was deleted.

0 comments on commit 47be512

Please sign in to comment.