class Logdna::Ruby

Attributes

app[RW]
env[RW]
meta[RW]

Public Class Methods

new(key, opts = {}) click to toggle source
Calls superclass method
# File lib/logdna.rb, line 21
def initialize(key, opts = {})
  super(nil, nil, nil)
  @app = opts[:app] || "default"
  @log_level = opts[:level] || "INFO"
  @env = opts[:env]
  @meta = opts[:meta]
  @internal_logger = Logger.new($stdout)
  @internal_logger.level = Logger::DEBUG
  endpoint = opts[:endpoint] || Resources::ENDPOINT
  hostname = opts[:hostname] || Socket.gethostname

  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

  ip =  opts.key?(:ip) ? "&ip=#{opts[:ip]}" : ""
  mac = opts.key?(:mac) ? "&mac=#{opts[:mac]}" : ""
  url = "#{endpoint}?hostname=#{hostname}#{mac}#{ip}"
  uri = URI(url)

  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

Public Instance Methods

<<(msg = nil, opts = {}) click to toggle source
# File lib/logdna.rb, line 107
def <<(msg = nil, opts = {})
  log(msg, opts.merge(
             level: ""
           ))
end
add(*_arg) click to toggle source
# File lib/logdna.rb, line 113
def add(*_arg)
  @internal_logger.debug("add not supported in LogDNA logger")
  false
end
clear() click to toggle source
# File lib/logdna.rb, line 100
def clear
  @app = "default"
  @log_level = "INFO"
  @env = nil
  @meta = nil
end
close() click to toggle source
# File lib/logdna.rb, line 129
def close
  @client&.exitout
end
datetime_format(*_arg) click to toggle source
# File lib/logdna.rb, line 124
def datetime_format(*_arg)
  @internal_logger.debug("datetime_format not supported in LogDNA logger")
  false
end
default_opts() click to toggle source
# File lib/logdna.rb, line 48
def default_opts
  {
    app: @app,
    level: @log_level,
    env: @env,
    meta: @meta,
  }
end
level() click to toggle source
# File lib/logdna.rb, line 57
def level
  @log_level
end
level=(value) click to toggle source
# File lib/logdna.rb, line 61
def level=(value)
  if value.is_a? Numeric
    @log_level = Resources::LOG_LEVELS[value]
    return
  end

  @log_level = value
end
log(message = nil, opts = {}) { || ... } click to toggle source
# File lib/logdna.rb, line 70
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
unknown(msg = nil, opts = {}) click to toggle source
# File lib/logdna.rb, line 118
def unknown(msg = nil, opts = {})
  log(msg, opts.merge(
             level: "UNKNOWN"
           ))
end