Skip to content

Instantly share code, notes, and snippets.

@roobert
Created November 21, 2016 14:44
Show Gist options
  • Save roobert/6e39f71fc3df7dab59ce4305d860f31d to your computer and use it in GitHub Desktop.
Save roobert/6e39f71fc3df7dab59ce4305d860f31d to your computer and use it in GitHub Desktop.
require "awesome_print"
require "logging"
class Log
attr_accessor :log
def initialize
@log = ::Logging.logger["test"]
configure
end
def configure
@log.level = :info
@log.add_appenders(Appenders.stdout)
end
module Appenders
def self.stdout
Logging.appenders.stdout("stdout", layout: Logging.layouts.pattern(pattern: "%m\n"))
end
end
end
module Logger
def log(message, level: :info)
@logger.log.send(level, message)
end
def info(message)
log(message, level: :info)
end
def debug(message)
log(message, level: :debug)
end
def enable_debug
@logger.log.level = :debug
end
def disable_debug
@logger.log.level = :info
end
end
class Test0
include Logger
attr_accessor :logger
def initialize(log_driver:)
@logger = log_driver
end
def run
log "test0"
info "test0 info"
debug "test0 debug"
end
end
class Test1
include Logger
attr_accessor :logger
def initialize(log_driver:)
@logger = log_driver
end
def run
log "test1"
info "test1 info"
enable_debug
debug "test1 debug"
disable_debug
end
end
logger = Log.new
t0 = Test0.new(log_driver: logger)
t1 = Test1.new(log_driver: logger)
t0.run
t1.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment