lib/logdna.rb in logdna-1.3.0 vs lib/logdna.rb in logdna-1.4.0

- old
+ new

@@ -1,159 +1,131 @@ -#!/usr/bin/env ruby -# encoding: utf-8 -# require 'singleton' -require 'socket' -require 'uri' -require_relative 'logdna/client.rb' -require_relative 'logdna/resources.rb' +# frozen_string_literal: true + +require "logger" +require "socket" +require "uri" +require_relative "logdna/client.rb" +require_relative "logdna/resources.rb" +require_relative "logdna/version.rb" + module Logdna class ValidURLRequired < ArgumentError; end class MaxLengthExceeded < ArgumentError; end class Ruby < ::Logger # uncomment line below and line 3 to enforce singleton # include Singleton Logger::TRACE = 5 - attr_accessor :level, :app, :env, :meta + attr_accessor :app, :env, :meta - def initialize(key, opts={}) - @app = opts[:app] || 'default' - @level = opts[:level] || 'INFO' + def initialize(key, opts = {}) + @app = opts[:app] || "default" + @log_level = opts[:level] || "INFO" @env = opts[:env] @meta = opts[:meta] - @@client = nil unless defined? @@client - + @internal_logger = Logger.new(STDOUT) + @internal_logger.level = Logger::DEBUG endpoint = opts[:endpoint] || Resources::ENDPOINT hostname = opts[:hostname] || Socket.gethostname - ip = opts.key?(:ip) ? "&ip=#{opts[:ip]}" : '' - mac = opts.key?(:mac) ? "&mac=#{opts[:mac]}" : '' - url = "#{endpoint}?hostname=#{hostname}#{mac}#{ip}" - 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) + if hostname.size > Resources::MAX_INPUT_LENGTH || @app.size > Resources::MAX_INPUT_LENGTH + @internal_logger.debug("Hostname or Appname is over #{Resources::MAX_INPUT_LENGTH} characters") return end - begin - uri = URI(url) - rescue URI::ValidURIRequired => e - puts "Invalid URL Endpoint: #{url}" - handle_exception(e) - return - end + ip = opts.key?(:ip) ? "&ip=#{opts[:ip]}" : "" + mac = opts.key?(:mac) ? "&mac=#{opts[:mac]}" : "" + url = "#{endpoint}?hostname=#{hostname}#{mac}#{ip}" + uri = URI(url) - 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 - - @@client = Logdna::Client.new(request, uri, opts) + request = Net::HTTP::Post.new(uri.request_uri, "Content-Type" => "application/json") + request.basic_auth("username", key) + request[:'user-agent'] = opts[:'user-agent'] || "ruby/#{LogDNA::VERSION}" + @client = Logdna::Client.new(request, uri, opts) end - def handle_exception(e) - exception_message = e.message - exception_backtrace = e.backtrace - # NOTE: should log with Ruby logger? - puts exception_message - end - def default_opts { app: @app, - level: @level, + level: @log_level, env: @env, meta: @meta, } end def level=(value) if value.is_a? Numeric - @level = Resources::LOG_LEVELS[value] + @log_level = Resources::LOG_LEVELS[value] return end - @level = value + @log_level = value end - def log(msg=nil, opts={}) - loggerExist? - message = msg - message = yield if msg.nil? && block_given? - @response = @@client.buffer(message, default_opts.merge(opts).merge({ - timestamp: (Time.now.to_f * 1000).to_i - })) - 'Saved' + def log(message = nil, opts = {}) + if message.nil? && block_given? + message = yield + end + if message.nil? + @internal_logger.debug("provide either a message or block") + return + end + message = message.to_s.encode("UTF-8") + @client.write_to_buffer(message, default_opts.merge(opts).merge( + timestamp: (Time.now.to_f * 1000).to_i + )) end Resources::LOG_LEVELS.each do |lvl| name = lvl.downcase - define_method name do |msg=nil, opts={}, &block| - self.log(msg, opts.merge({ - level: lvl, - }), &block) + define_method name do |msg = nil, opts = {}, &block| + self.log(msg, opts.merge( + level: lvl + ), &block) end define_method "#{name}?" do - return Resources::LOG_LEVELS[self.level] == lvl if self.level.is_a? Numeric + return Resources::LOG_LEVELS[self.level] == lvl if level.is_a? Numeric + self.level == lvl end end def clear - @app = 'default' - @level = 'INFO' + @app = "default" + @log_level = "INFO" @env = nil @meta = nil end - def loggerExist? - if @@client.nil? - puts "Logger Not Initialized Yet" - close - end + def <<(msg = nil, opts = {}) + log(msg, opts.merge( + level: "" + )) end - def <<(msg=nil, opts={}) - self.log(msg, opts.merge({ - level: '', - })) + def add(*_arg) + @internal_logger.debug("add not supported in LogDNA logger") + false end - def add(*arg) - puts "add not supported in LogDNA logger" - return false + def unknown(msg = nil, opts = {}) + log(msg, opts.merge( + level: "UNKNOWN" + )) end - def unknown(msg=nil, opts={}) - self.log(msg, opts.merge({ - level: 'UNKNOWN', - })) + def datetime_format(*_arg) + @internal_logger.debug("datetime_format not supported in LogDNA logger") + false 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 + @client&.exitout end at_exit do - if defined? @@client and !@@client.nil? - @@client.exitout() - end + @client&.exitout end end end