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.



18
19
20
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
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/logdna.rb', line 18

def initialize(key, opts={})
  @app = opts[:app] || 'default'
  @level = opts[:level] || 'INFO'
  @env = opts[:env]
  @meta = opts[:meta]
  @@client = nil unless defined? @@client

  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}"

  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

  begin
    uri = URI(url)
  rescue URI::ValidURIRequired => e
    puts "Invalid URL Endpoint: #{url}"
    handle_exception(e)
    return
  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

  @@client = Logdna::Client.new(request, uri, opts)
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



16
17
18
# File 'lib/logdna.rb', line 16

def app
  @app
end

#envObject

Returns the value of attribute env.



16
17
18
# File 'lib/logdna.rb', line 16

def env
  @env
end

#levelObject

Returns the value of attribute level.



16
17
18
# File 'lib/logdna.rb', line 16

def level
  @level
end

#metaObject

Returns the value of attribute meta.



16
17
18
# File 'lib/logdna.rb', line 16

def meta
  @meta
end

Instance Method Details

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



121
122
123
124
125
# File 'lib/logdna.rb', line 121

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

#add(*arg) ⇒ Object



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

def add(*arg)
  puts "add not supported in LogDNA logger"
  return false
end

#clearObject



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

def clear
  @app = 'default'
  @level = 'INFO'
  @env = nil
  @meta = nil
end

#closeObject



144
145
146
147
148
149
# File 'lib/logdna.rb', line 144

def close
  if defined? @@client and !@@client.nil?
      @@client.exitout()
  end
  exit!
end

#datetime_format(*arg) ⇒ Object



138
139
140
141
# File 'lib/logdna.rb', line 138

def datetime_format(*arg)
  puts "datetime_format not supported in LogDNA logger"
  return false
end

#default_optsObject



66
67
68
69
70
71
72
73
# File 'lib/logdna.rb', line 66

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

#handle_exception(e) ⇒ Object



59
60
61
62
63
64
# File 'lib/logdna.rb', line 59

def handle_exception(e)
  exception_message = e.message
  exception_backtrace = e.backtrace
  # NOTE: should log with Ruby logger?
  puts exception_message
end

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



84
85
86
87
88
89
90
# File 'lib/logdna.rb', line 84

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

#loggerExist?Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
# File 'lib/logdna.rb', line 114

def loggerExist?
  if @@client.nil?
    puts "Logger Not Initialized Yet"
    close
  end
end

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



132
133
134
135
136
# File 'lib/logdna.rb', line 132

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