Class: Logdna::Ruby
- Inherits:
-
Logger
- Object
- Logger
- Logdna::Ruby
- Defined in:
- lib/logdna.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
Returns the value of attribute app.
-
#env ⇒ Object
Returns the value of attribute env.
-
#meta ⇒ Object
Returns the value of attribute meta.
Instance Method Summary collapse
- #<<(msg = nil, opts = {}) ⇒ Object
- #add(*_arg) ⇒ Object
- #clear ⇒ Object
- #close ⇒ Object
- #datetime_format(*_arg) ⇒ Object
- #default_opts ⇒ Object
-
#initialize(key, opts = {}) ⇒ Ruby
constructor
A new instance of Ruby.
- #level ⇒ Object
- #level=(value) ⇒ Object
- #log(message = nil, opts = {}) ⇒ Object
- #unknown(msg = nil, opts = {}) ⇒ Object
Constructor Details
#initialize(key, opts = {}) ⇒ Ruby
Returns a new instance of Ruby.
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 |
# File 'lib/logdna.rb', line 20 def initialize(key, opts = {}) @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
#app ⇒ Object
Returns the value of attribute app.
18 19 20 |
# File 'lib/logdna.rb', line 18 def app @app end |
#env ⇒ Object
Returns the value of attribute env.
18 19 20 |
# File 'lib/logdna.rb', line 18 def env @env end |
#meta ⇒ Object
Returns the value of attribute meta.
18 19 20 |
# File 'lib/logdna.rb', line 18 def @meta end |
Instance Method Details
#<<(msg = nil, opts = {}) ⇒ Object
105 106 107 108 109 |
# File 'lib/logdna.rb', line 105 def <<(msg = nil, opts = {}) log(msg, opts.merge( level: "" )) end |
#add(*_arg) ⇒ Object
111 112 113 114 |
# File 'lib/logdna.rb', line 111 def add(*_arg) @internal_logger.debug("add not supported in LogDNA logger") false end |
#clear ⇒ Object
98 99 100 101 102 103 |
# File 'lib/logdna.rb', line 98 def clear @app = "default" @log_level = "INFO" @env = nil @meta = nil end |
#close ⇒ Object
127 128 129 |
# File 'lib/logdna.rb', line 127 def close @client&.exitout end |
#datetime_format(*_arg) ⇒ Object
122 123 124 125 |
# File 'lib/logdna.rb', line 122 def datetime_format(*_arg) @internal_logger.debug("datetime_format not supported in LogDNA logger") false end |
#default_opts ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/logdna.rb', line 46 def default_opts { app: @app, level: @log_level, env: @env, meta: @meta, } end |
#level ⇒ Object
55 56 57 |
# File 'lib/logdna.rb', line 55 def level @log_level end |
#level=(value) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/logdna.rb', line 59 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
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/logdna.rb', line 68 def log( = nil, opts = {}) if .nil? && block_given? = yield end if .nil? @internal_logger.debug("provide either a message or block") return end = .to_s.encode("UTF-8") @client.write_to_buffer(, default_opts.merge(opts).merge( timestamp: (Time.now.to_f * 1000).to_i )) end |
#unknown(msg = nil, opts = {}) ⇒ Object
116 117 118 119 120 |
# File 'lib/logdna.rb', line 116 def unknown(msg = nil, opts = {}) log(msg, opts.merge( level: "UNKNOWN" )) end |