Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixing up examples.

  • Loading branch information...
commit 47be512f98b4d680192a71497890e4d442dc14af 1 parent aa5c4f9
Tim Pease authored
93 examples/custom_log_levels.rb
View
@@ -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 examples/rspec_everywhere.rb
View
@@ -1,20 +0,0 @@
-require 'rspec'
-require 'logging'
-#spec_helper - adjust path, because this is applicable to within the logging source-code. Yours would be
-#require 'logging/rspec/logging_helper'
-require 'rspec/logging_helper'
-
-RSpec.configure do |config|
- include RSpec::LoggingHelper
- config.capture_log_messages
-end
-
-# a spec file
-describe 'Foo' do
- it 'should be able to read a log message' do
- Logging.logger['root'].debug 'foo'
- @log_output.readline.strip.should =~ /foo/
- end
-end
-
-RSpec::Core::Runner.run([File.dirname(__FILE__)], $stderr, $stdout)
38 examples/rspec_integration.rb
View
@@ -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 examples/rspec_only_some_specs.rb
View
@@ -1,27 +0,0 @@
-require 'rspec'
-require 'logging'
-#spec_helper - adjust path, because this is applicable to within the logging source-code. Yours would be
-#require 'logging/rspec/logging_helper'
-require 'rspec/logging_helper'
-
-RSpec.configure do |config|
- config.extend RSpec::LoggingHelper
-end
-
-# a spec file
-describe 'Foo' do
- capture_log_messages
- it 'should be able to read a log message' do
- Logging.logger['root'].debug 'foo'
- @log_output.readline.strip.should =~ /foo/
- end
-end
-
-describe 'Bar' do
- it 'should not be able to read a log message because we did not call capture_log_messages' do
- Logging.logger['root'].debug 'foo'
- @log_output.should be_nil
- end
-end
-
-RSpec::Core::Runner.run([File.dirname(__FILE__)], $stderr, $stdout)
Please sign in to comment.
Something went wrong with that request. Please try again.