Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
module Util
module InternalLogger
class Base
def log; raise NotImplementedError; end
# since we provide a null object that drops all messages on the floor, we need a simple
# way to inquire if the particular active instance is null or not
def null?; raise NotImplementedError; end
def sync; raise NotImplementedError; end
end
class Null < Base
def log(level, message); nil; end
def null?; true; end
def sync; nil; end
end
class LiveFile < Base
def initialize(filename)
open(filename)
end
def open(filename)
cleanup
begin
@filename = filename
@logger = File.open(filename, "a+")
@attempt_reconnect = false
rescue Exception => e
@attempt_reconnect = true
File.open("/tmp/internal_logger_exception", "a+") { |f| f.write("#{e}\nbacktrace = \n#{e.backtrace}") }
end
end
def log(level, message)
if connected?
begin
@logger.write("#{Time.now}|#{level}|#{message}\n")
@logger.fsync
rescue Exception => e
@attempt_reconnect = true
File.open("/tmp/internal_logger_exception", "a+") { |f| f.write("#{e}\nbacktrace = \n#{e.backtrace}") }
end
end
end
def connected?
if @attempt_reconnect
begin
@logger = File.open(@filename, "a+")
@attempt_reconnect = false
rescue Exception => e
@attempt_reconnect= true
File.open("/tmp/internal_logger_exception", "a+") { |f| f.write("#{e}\nbacktrace = \n#{e.backtrace}") }
end
end
!@attempt_reconnect
end
def sync
@logger.fsync
end
def null?; false; end
private
def cleanup
# close the logger if it already has a file open
if @logger
begin
@logger.close unless @logger.closed?
@filename = nil
rescue Exception => e
File.open("/tmp/internal_logger_exception", "a+") { |f| f.write("#{e}\nbacktrace = \n#{e.backtrace}") }
end
end
end
end
class LiveNetwork < Base
def initialize(service, host, port)
@service = service
@host = host
@port = port
begin
@logger = Swiftcore::Analogger::Client.new(@service, @host, @port)
@attempt_reconnect = false
puts "init, connected"
rescue Exception => e
@attempt_reconnect = true
puts "init, not connected"
File.open("/tmp/internal_logger_exception", "a+") { |f| f.write("#{e}\n") }
end
end
def log(level, message)
if connected?
begin
@logger.log(level, message)
rescue Exception => e
@attempt_reconnect = true
end
end
end
def connected?
if @attempt_reconnect
begin
@logger.reconnect
@attempt_reconnect = false
puts "connected?, reconnected"
rescue Exception => e
@attempt_reconnect= true
puts "connected?, failed"
File.open("/tmp/internal_logger_exception", "a+") { |f| f.write("#{e}\n") }
end
end
!@attempt_reconnect
end
def null?; false; end
end
end
module Adapter
class LogAdapter
include Adapter
attr_reader :events
attr_accessor :null_class, :live_class
attr_reader :logger
def initialize
@events = []
@null_class, @live_class = Util::InternalLogger::Null, Util::InternalLogger::LiveFile
@null_obj, @live_obj = nil, nil
@logger = nil
end
def update(event)
if event.respond_to?(:debug)
log(:info, "Debug logging was [#{live?}], event.debug [#{event.debug}]")
(event.debug ? make_live : make_null)
log(:info, "Debug logging now [#{live?}]")
end
end
def log(level, message)
if live?
@logger.log(level, message)
else
@live_obj.log(level, message) if [:info, :warn, :error, :fatal].include?(level)
end
end
def build(options = {}) #(nc = @null_class, lc = @live_class, service = 'default', host = '172.28.55.103', port = 47990)
begin
@live_class = options[:lc] || @live_class
@null_class = options[:nc] || @null_class
@null_obj = @null_class.new
make_directory(options)
open(options[:filename])
rescue Exception => e
File.open("/tmp/new_exception", "a+") { |f| f.write("#{e}\n") }
end
end
def wire
@logger = @null_obj
end
def make_live
@logger = @live_obj
@logger.log(:debug, "LogAdapter: made live")
end
def make_null
@logger = @null_obj
@logger.log(:debug, "LogAdapter: made null")
end
def open(filename)
# let's us open up a new file for logging
@live_obj = @live_class.new(filename)
end
def live?
@logger == @live_obj
end
private
def make_directory(options)
unless options[:filename].nil? || options[:filename].empty?
path = File.dirname(options[:filename])
unless File.exist?(path)
temp = ""
path.split(File::SEPARATOR).each do |piece|
temp += piece + File::SEPARATOR
Dir.mkdir(temp) unless File.exist?(temp)
end
end
end
end
end
end
Delim = '|'.freeze unless defined?(Delim)
EMPTY = ''.freeze unless defined?(EMPTY)
UNKNOWN = 'UNKNOWN_MODULE'.freeze unless defined?(UNKNOWN)
class Logger
# global logger for this module's namespace
@hsh = { :nc => Util::InternalLogger::Null,
:lc => Util::InternalLogger::LiveFile,
:filename => "/usr/orc/log/debug-#{Time.now.strftime("%Y%m%d")}.txt"
}
Instance = Adapter::LogAdapter.new
Instance.build(@hsh)
Instance.wire
#Instance.make_live
class << self
attr_accessor :module_name
def log(level, message)
Instance.log(level, (module_name || UNKNOWN) + Delim + message)
end
def make_live
Instance.make_live
end
def make_null
Instance.make_null
end
def open(filename)
Instance.open(filename)
end
%w(debug info warn error fatal).each do |method_name|
class_eval <<-code
def #{method_name}(message)
log("#{method_name}".to_sym, message)
end
code
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment