Class: Logdna::Ruby

Inherits:
Logger
  • Object
show all
Defined in:
lib/logdna.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, opts = {}) ⇒ Ruby

Returns a new instance of Ruby.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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

Instance Attribute Details

#appObject

Returns the value of attribute app.



19
20
21
# File 'lib/logdna.rb', line 19

def app
  @app
end

#envObject

Returns the value of attribute env.



19
20
21
# File 'lib/logdna.rb', line 19

def env
  @env
end

#metaObject

Returns the value of attribute meta.



19
20
21
# File 'lib/logdna.rb', line 19

def meta
  @meta
end

Instance Method Details

#<<(msg = nil, opts = {}) ⇒ Object



107
108
109
110
111
# File 'lib/logdna.rb', line 107

def <<(msg = nil, opts = {})
  log(msg, opts.merge(
             level: ""
           ))
end

#add(*_arg) ⇒ Object



113
114
115
116
# File 'lib/logdna.rb', line 113

def add(*_arg)
  @internal_logger.debug("add not supported in LogDNA logger")
  false
end

#clearObject



100
101
102
103
104
105
# File 'lib/logdna.rb', line 100

def clear
  @app = "default"
  @log_level = "INFO"
  @env = nil
  @meta = nil
end

#closeObject



129
130
131
# File 'lib/logdna.rb', line 129

def close
  @client&.exitout
end

#datetime_format(*_arg) ⇒ Object



124
125
126
127
# File 'lib/logdna.rb', line 124

def datetime_format(*_arg)
  @internal_logger.debug("datetime_format not supported in LogDNA logger")
  false
end

#default_optsObject



48
49
50
51
52
53
54
55
# File 'lib/logdna.rb', line 48

def default_opts
  {
    app: @app,
    level: @log_level,
    env: @env,
    meta: @meta,
  }
end

#levelObject



57
58
59
# File 'lib/logdna.rb', line 57

def level
  @log_level
end

#level=(value) ⇒ Object



61
62
63
64
65
66
67
68
# 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 = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# 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 = {}) ⇒ Object



118
119
120
121
122
# File 'lib/logdna.rb', line 118

def unknown(msg = nil, opts = {})
  log(msg, opts.merge(
             level: "UNKNOWN"
           ))
end