Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add the concept of a Logger #9

Merged
merged 2 commits into from

2 participants

pat brisbin Niko Felger
pat brisbin

I've replaced the STDERR.puts methods for a simple wrapper over ruby's Logger class. This allows users to quiet the API output if they wish. It also leaves the door open to add more logging and error handling at different levels if desired.

I also provided a set method for clients to inject their own logger; useful if you happen to be in a larger application with its own object providing a Logger interface (as I happen to be) and you want the API traffic to be included in the application-wide stream.

Niko Felger nfelger merged commit 71a1e5d into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 17, 2012
  1. pat brisbin

    add a logger class and spec

    pbrisbin authored
  2. pat brisbin
This page is out of date. Refresh to see the latest.
1  lib/rype.rb
View
@@ -4,6 +4,7 @@
require 'rype/chat'
require 'rype/chatmessage'
require 'rype/events'
+require 'rype/logger'
module Rype
Offline = Class.new StandardError
4 lib/rype/api.rb
View
@@ -114,11 +114,11 @@ def callbacks
end
def log_incoming(message)
- STDERR.puts "<- #{message}"
+ Logger.debug "<- #{message}"
end
def log_outgoing(message)
- STDERR.puts "-> #{message}"
+ Logger.debug "-> #{message}"
end
end
end
21 lib/rype/logger.rb
View
@@ -0,0 +1,21 @@
+require 'logger'
+
+module Rype
+ class Logger
+ class << self
+ def method_missing(*args, &block)
+ logger.send(*args, &block)
+ end
+
+ def set(logger)
+ @logger = logger
+ end
+
+ private
+
+ def logger
+ @logger ||= ::Logger.new(STDERR)
+ end
+ end
+ end
+end
27 spec/lib/rype/logger_spec.rb
View
@@ -0,0 +1,27 @@
+require 'spec_helper'
+
+module Rype
+ describe Logger do
+ after do
+ # so other tests get consistent behavior.
+ Logger.instance_variable_set("@logger", nil)
+ end
+
+ it "should delegate to an underlying ::Logger by default" do
+ logger = double("logger")
+ logger.should_receive(:info).with("a message").once
+
+ ::Logger.should_receive(:new).with(STDERR).and_return(logger)
+
+ Logger.info("a message")
+ end
+
+ it "should be able to have an alternate logger set" do
+ logger = double("logger")
+ logger.should_receive(:info).with("a message").once
+
+ Logger.set(logger)
+ Logger.info("a message")
+ end
+ end
+end
Something went wrong with that request. Please try again.