Created
January 14, 2010 19:10
-
-
Save techpeace/277403 to your computer and use it in GitHub Desktop.
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
module Thin | |
# To be included in classes to allow some basic logging | |
# that can be silenced (<tt>Logging.silent=</tt>) or made | |
# more verbose. | |
# <tt>Logging.debug=</tt>: log all error backtrace and messages | |
# logged with +debug+. | |
# <tt>Logging.trace=</tt>: log all raw request and response and | |
# messages logged with +trace+. | |
module Logging | |
class << self | |
attr_writer :trace, :debug, :silent | |
def trace?; !@silent && @trace end | |
def debug?; !@silent && @debug end | |
def silent?; @silent end | |
end | |
# Global silencer methods | |
def silent | |
Logging.silent? | |
end | |
def silent=(value) | |
Logging.silent = value | |
end | |
module_function | |
public | |
# Log a message to the console | |
def log(msg) | |
puts msg unless Logging.silent? | |
end | |
# Log a message to the console if tracing is activated | |
def trace(msg=nil) | |
log msg || yield if Logging.trace? | |
end | |
# Log a message to the console if debugging is activated | |
def debug(msg=nil) | |
log msg || yield if Logging.debug? | |
end | |
# Log an error backtrace if debugging is activated | |
def log_error(e=$!) | |
debug "#{e}\n\t" + e.backtrace.join("\n\t") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment