Skip to content

Instantly share code, notes, and snippets.

@prodis
Created April 30, 2012 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prodis/2557515 to your computer and use it in GitHub Desktop.
Save prodis/2557515 to your computer and use it in GitHub Desktop.
Ruby Fundamental - Módulos para definir métodos globais
require 'logger'
module CoolLog
def logger
@cool_log_logger ||= ::Logger.new(STDOUT)
end
def level
@cool_log_level ||= :info
end
def level=(value)
@cool_log_level = value
end
def log(message)
logger.send(level, message)
end
end
class MyClass1
include CoolLog
end
m1 = MyClass1.new
m1.level # => :info
m1.log "Informando..." # => I, [2012-04-30T08:25:22.436474 #2601] INFO -- : Informando...
m1.level = :debug
m1.log "Debugando..." # => D, [2012-04-30T08:25:22.437045 #2601] DEBUG -- : Debugando...
class MyClass2
extend CoolLog
end
MyClass2.level # => :info
MyClass2.log "Informando..." # => I, [2012-04-30T08:43:37.252694 #2654] INFO -- : Informando...
MyClass2.level = :debug
MyClass2.log "Debugando..." # => D, [2012-04-30T08:43:37.252736 #2654] DEBUG -- : Debugando...
class MyClass3
extend CoolLog
end
class MyClass4
extend CoolLog
end
MyClass3.level # => :info
MyClass4.level # => :info
MyClass3.level = :debug
MyClass3.level # => :debug
MyClass4.level # => :info
module MyModule
extend CoolLog
end
MyModule.level # => :info
MyModule.log "Informando..." # => I, [2012-04-30T09:07:20.251521 #2740] INFO -- : Informando...
MyModule.level = :debug
MyModule.log "Debugando..." # => D, [2012-04-30T09:07:20.251561 #2740] DEBUG -- : Debugando...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment