lib/logdna.rb in logdna-1.0.9 vs lib/logdna.rb in logdna-1.1.0

- old
+ new

@@ -1,205 +1,158 @@ #!/usr/bin/env ruby # encoding: utf-8 +# require 'singleton' require 'socket' +require 'uri' require_relative 'logdna/client.rb' require_relative 'logdna/resources.rb' module Logdna - class Ruby < ::Logger - Logger::TRACE = 5 - attr_accessor :level, :app, :env, :meta - @level = nil - @app = nil - @env = nil - @meta = nil + class ValidURLRequired < ArgumentError; end + class MaxLengthExceeded < ArgumentError; end - def initialize(key, opts={}) - @@client = Logdna::Client.new(key, opts) - sleep 0.01 + class Ruby < ::Logger + # uncomment line below and line 3 to enforce singleton + # include Singleton + Logger::TRACE = 5 + attr_accessor :level, :app, :env, :meta - if @@client[:value] === Resources::LOGGER_NOT_CREATED - @@client = nil - puts "LogDNA logger not created" - return - end - end + def initialize(key, opts={}) + @app = opts[:app] || 'default' + @level = opts[:level] || 'INFO' + @env = opts[:env] + @meta = opts[:meta] + @@client = nil unless defined? @@client - def log(msg=nil, opts={}) - loggerExist? - optionChanged? - @response = @@client.tobuffer(msg, opts) - 'Saved' - end + hostname = opts[:hostname] || Socket.gethostname + ip = opts.key?(:ip) ? "&ip=#{opts[:ip]}" : '' + mac = opts.key?(:mac) ? "&mac=#{opts[:mac]}" : '' + url = "#{Resources::ENDPOINT}?hostname=#{hostname}#{mac}#{ip}" - def trace(msg=nil, opts={}) - opts[:level] = "TRACE" - loggerExist? - optionChanged? - @response = @@client.tobuffer(msg, opts) - 'Saved' + begin + if (hostname.size > Resources::MAX_INPUT_LENGTH || @app.size > Resources::MAX_INPUT_LENGTH ) + raise MaxLengthExceeded.new end + rescue MaxLengthExceeded => e + puts "Hostname or Appname is over #{Resources::MAX_INPUT_LENGTH} characters" + handle_exception(e) + return + end - def debug(msg=nil, opts={}) - opts[:level] = "DEBUG" - loggerExist? - optionChanged? - @response = @@client.tobuffer(msg, opts) - 'Saved' - end + begin + uri = URI(url) + rescue URI::ValidURIRequired => e + puts "Invalid URL Endpoint: #{url}" + handle_exception(e) + return + end - def info(msg=nil, opts={}) - opts[:level] = "INFO" - loggerExist? - optionChanged? - @response = @@client.tobuffer(msg, opts) - 'Saved' - end + begin + request = Net::HTTP::Post.new(uri.request_uri, 'Content-Type' => 'application/json') + request.basic_auth 'username', key + rescue => e + handle_exception(e) + return + end - def warn(msg=nil, opts={}) - opts[:level] = "WARN" - loggerExist? - optionChanged? - @response = @@client.tobuffer(msg, opts) - 'Saved' - end + @@client = Logdna::Client.new(request, uri, opts) + end - def error(msg=nil, opts={}) - opts[:level] = "ERROR" - loggerExist? - optionChanged? - @response = @@client.tobuffer(msg, opts) - 'Saved' - end + def handle_exception(e) + exception_message = e.message + exception_backtrace = e.backtrace + # NOTE: should log with Ruby logger? + puts exception_message + end - def fatal(msg=nil, opts={}) - opts[:level] = "FATAL" - loggerExist? - optionChanged? - @response = @@client.tobuffer(msg, opts) - 'Saved' - end + def default_opts + { + app: @app, + level: @level, + env: @env, + meta: @meta, + } + end - def trace? - loggerExist? - unless @level - return 'TRACE' == @@client.getLevel - end - logLevel('TRACE') - end + def level=(value) + if value.is_a? Numeric + @level = Resources::LOG_LEVELS[value] + return + end - def debug? - loggerExist? - unless @level - return 'DEBUG' == @@client.getLevel - end - logLevel('DEBUG') - end + @level = value + end - def info? - loggerExist? - unless @level - return 'INFO' == @@client.getLevel - end - logLevel('INFO') - end + def log(msg=nil, opts={}) + loggerExist? + @response = @@client.buffer(msg, default_opts.merge(opts).merge({ + timestamp: (Time.now.to_f * 1000).to_i + })) + 'Saved' + end - def warn? - loggerExist? - unless @level - return 'WARN' == @@client.getLevel - end - logLevel('WARN') - end + Resources::LOG_LEVELS.each do |lvl| + name = lvl.downcase - def error? - loggerExist? - unless @level - return 'ERROR' == @@client.getLevel - end - logLevel('ERROR') - end + define_method name do |msg=nil, opts={}| + self.log(msg, opts.merge({ + level: lvl, + })) + end - def fatal? - loggerExist? - unless @level - return 'FATAL' == @@client.getLevel - end - logLevel('FATAL') - end + define_method "#{name}?" do + return Resources::LOG_LEVELS[self.level] == lvl if self.level.is_a? Numeric + self.level == lvl + end + end - def clear - loggerExist? - @@client.clear() - @level = nil - @app = nil - @env = nil - @meta = nil - return true - end + def clear + @app = 'default' + @level = 'INFO' + @env = nil + @meta = nil + end - def loggerExist? - if @@client.nil? - puts "Logger Not Initialized Yet" - close - end - end + def loggerExist? + if @@client.nil? + puts "Logger Not Initialized Yet" + close + end + end - def optionChanged? - if @level || @app || @env || @meta - @@client.change(@level, @app, @env, @meta) - @level = nil - @app = nil - @env = nil - @meta = nil - end - end + def <<(msg=nil, opts={}) + self.log(msg, opts.merge({ + level: '', + })) + end - def logLevel(comparedTo) - if @level.is_a? Numeric - @level = Resources::LOG_LEVELS[@level] - end - return comparedTo == @level.upcase - end + def add(*arg) + puts "add not supported in LogDNA logger" + return false + end - def <<(msg=nil, opts={}) - opts[:level] = "" - loggerExist? - optionChanged? - @response = @@client.tobuffer(msg, opts) - 'Saved' - end + def unknown(msg=nil, opts={}) + self.log(msg, opts.merge({ + level: 'UNKNOWN', + })) + end - def add(*arg) - puts "add not supported in LogDNA logger" - return false - end + def datetime_format(*arg) + puts "datetime_format not supported in LogDNA logger" + return false + end - def unknown(msg=nil, opts={}) - opts[:level] = "UNKNOWN" - loggerExist? - optionChanged? - @response = @@client.tobuffer(msg, opts) - 'Saved' - end - def datetime_format(*arg) - puts "datetime_format not supported in LogDNA logger" - return false - end + def close + if defined? @@client and !@@client.nil? + @@client.exitout() + end + exit! + end - - def close - if defined? @@client - @@client.exitout() - end - exit! - end - - at_exit do - if defined? @@client - @@client.exitout() - end - exit! - end + at_exit do + if defined? @@client and !@@client.nil? + @@client.exitout() + end + exit! end + end end -